package scraper import ( "fmt" "log/slog" "github.com/iancoleman/strcase" "github.com/jmoiron/sqlx" "github.com/logrusorgru/aurora/v4" "github.com/spf13/viper" "golang.org/x/exp/slices" "git.kplus.net.ua/yevhen/resource-scraper/internal/db" "git.kplus.net.ua/yevhen/resource-scraper/pkg/handler" "git.kplus.net.ua/yevhen/resource-scraper/pkg/repository" "git.kplus.net.ua/yevhen/resource-scraper/pkg/service" "git.kplus.net.ua/yevhen/resource-scraper/types/constant" ) func isAllowScope() bool { scopesAllow := viper.GetStringSlice("scope.allow") scopeEnabled := viper.GetString(constant.FlagScopeEnable) return slices.Contains(scopesAllow, scopeEnabled) } func init() { strcase.ConfigureAcronym("stb", "STB") } // Bootstrap runs the selected scope and returns the process exit code: zero on // success, non-zero as soon as the scope reports a failure. func Bootstrap() int { if !isAllowScope() { fmt.Printf("%s You are in not allowed scope, check %s config file\n", aurora.BgMagenta("[WARN]"), aurora.Magenta("default.yaml")) return 2 } dbase := db.New() // The SQLite handle is opened only for the srrdb scope, so that a missing // or unreadable srrdb.sqlite does not break the other sources. var srrdb *sqlx.DB if viper.GetString(constant.FlagScopeEnable) == constant.ScopeSrrdb { srrdb = db.NewSrrdb() } repos := repository.New(dbase, srrdb) services := service.New(repos) handlers := handler.New(services) switch viper.GetString("role") { case constant.RoleConsole: result, err := handlers.InitConsole() fmt.Printf("init console console: %s\n", result) if err != nil { slog.Error("console command failed", "scope", viper.GetString(constant.FlagScopeEnable), "err", err) return 1 } case constant.RoleWeb: result, err := handlers.InitRoutes() fmt.Printf("who: %s\n", result) ///http.Run() if err != nil { slog.Error("web init failed", "err", err) return 1 } } return 0 }