preset scraper added

This commit is contained in:
2024-09-10 15:57:48 +03:00
parent bfdfc634e4
commit 0ecf0ddec1
38 changed files with 790 additions and 167 deletions

View File

@@ -5,16 +5,15 @@ import (
"github.com/spf13/viper"
"git.amok.space/yevhen/resource-scraper/internal/config"
"git.amok.space/yevhen/resource-scraper/internal/db"
"git.amok.space/yevhen/resource-scraper/pkg/handler"
"git.amok.space/yevhen/resource-scraper/pkg/repository"
"git.amok.space/yevhen/resource-scraper/pkg/service"
"git.amok.space/yevhen/resource-scraper/types"
"git.amok.space/yevhen/resource-scraper/types/constant"
)
func Bootstrap() {
config.New()
//https://ahmadrosid.com/blog/how-to-query-html-dom-in-golang
dbase := db.New()
repos := repository.New(dbase)
@@ -22,10 +21,12 @@ func Bootstrap() {
handlers := handler.New(services)
switch viper.GetString("role") {
case types.RoleConsole:
case constant.RoleConsole:
fmt.Printf("init console console: %s\n", handlers.InitConsole())
break
case constant.RoleWeb:
fmt.Printf("who: %s\n", handlers.InitRoutes())
///http.Run()
break
}
///http.Run()
}

View File

@@ -3,32 +3,62 @@ package config
import (
"fmt"
"log"
"log/slog"
"path/filepath"
"slices"
"github.com/spf13/viper"
"git.amok.space/yevhen/resource-scraper/types/constant"
)
func New() {
configFilePath := viper.GetString("config-file")
configDir := "./" + filepath.Dir(configFilePath)
configFilePath := viper.GetString(constant.FlagConfigFile)
configName := filepath.Base(configFilePath)
dir := filepath.Dir(configFilePath)
if dir == configName && dir == "." {
configName = "default"
}
configDir, _ := filepath.Abs(dir)
viper.SetConfigName(filepath.Base(configFilePath))
viper.AddConfigPath(configDir)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
viper.SetConfigName(configName)
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
viper.SetDefault("ConfigDir", configDir)
scope := viper.GetString("scope")
if scope != "" {
viper.SetConfigName(scope)
viper.AddConfigPath(configDir)
err := viper.MergeInConfig()
if err != nil {
log.Fatalf("fatal error config file: %v", err)
return
viper.SetConfigName("secret")
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error secret file: %w", err))
}
slog.Info("using config", "path", filepath.Join(configDir, configName))
viper.SetDefault(constant.CfgKeyConfigDir, configDir)
// Scopes validating
scope := viper.GetString(constant.CfgKeyScopeEnable)
scopesAllowed := viper.GetStringSlice("scope.allow")
if !slices.Contains(scopesAllowed, scope) {
scope = viper.GetString("scope.default")
if scope == "" {
scope = constant.ScopeInfo
}
}
fmt.Println("scope", scope)
viper.SetConfigName(scope)
viper.AddConfigPath(filepath.Join(configDir, "scope"))
err = viper.MergeInConfig()
if err == nil {
viper.SetDefault(constant.CfgKeyScopeEnable, scope)
} else {
log.Fatalf("fatal error config file: %v", err)
}
}

View File

@@ -8,6 +8,8 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
"git.amok.space/yevhen/resource-scraper/types/constant"
)
const usage = `Music Database (MDB) server/cli craftware'
@@ -25,19 +27,18 @@ The Go runtime version: %s
Report bugs to https://git.amok.space/yevhen/resource-scraper/issues`
const (
maxPasswordLength = 20
version = "0.1"
defaultConfigPath = "./config/default"
defaultConfigPath = "config/default"
)
func ParseFlags() {
flag.Bool("h", false, "")
flag.Bool("help", false, "")
flag.Bool("v", false, "")
flag.Bool("version", false, "")
flag.Bool("debug", false, "")
flag.String("config-file", defaultConfigPath, "config file location used for the program")
flag.String("scope", "", "")
flag.Bool(constant.FlagHelpShort, false, "")
flag.Bool(constant.FlagHelp, false, "")
flag.Bool(constant.FlagVersionShort, false, "")
flag.Bool(constant.FlagVersion, false, "")
flag.Bool(constant.FlagDebug, false, "")
flag.String(constant.FlagConfigFile, defaultConfigPath, "config file location used for the program")
flag.String(constant.FlagScopeEnable, "", "")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
@@ -46,10 +47,10 @@ func ParseFlags() {
log.Fatalf("[ERR] Failed to bind flags to config: %s", err)
}
if viper.GetBool("h") || viper.GetBool("help") {
if viper.GetBool(constant.FlagHelpShort) || viper.GetBool(constant.FlagHelp) {
msg := fmt.Sprintf(usage, defaultConfigPath, runtime.Version())
fmt.Println(msg)
} else if viper.GetBool("v") || viper.GetBool("version") {
} else if viper.GetBool(constant.FlagVersionShort) || viper.GetBool(constant.FlagVersion) {
fmt.Println("MDB version", version)
}
}