setup parser

This commit is contained in:
Gitea
2023-12-09 00:33:12 +03:00
parent 20107503e0
commit 3733278d8c
24 changed files with 986 additions and 100 deletions

41
cmd/background/config.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"encoding/json"
"fmt"
"os"
"git.loyso.art/frx/kurious/internal/common/config"
)
type Config struct {
Log config.Log `json:"log"`
YDB config.YDB `json:"ydb"`
SyncSravniCron string `json:"sync_sravni_cron"`
DebugHTTP bool `json:"debug_http"`
}
func readFromFile(path string, defaultConfigF func() Config) (Config, error) {
out := defaultConfigF()
payload, err := os.ReadFile(path)
if err != nil {
return out, fmt.Errorf("opening file: %w", err)
}
err = json.Unmarshal(payload, &out)
if err != nil {
return out, fmt.Errorf("decoding as json: %w", err)
}
return out, nil
}
func defaultConfig() Config {
return Config{
Log: config.Log{
Level: config.LogLevelInfo,
Format: config.LogFormatText,
},
}
}

94
cmd/background/main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"golang.org/x/sync/errgroup"
"git.loyso.art/frx/kurious/internal/common/client/sravni"
"git.loyso.art/frx/kurious/internal/common/config"
"git.loyso.art/frx/kurious/internal/common/xcontext"
"git.loyso.art/frx/kurious/internal/common/xlog"
"git.loyso.art/frx/kurious/internal/kurious/ports"
"git.loyso.art/frx/kurious/internal/kurious/service"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := app(ctx)
if err != nil {
println(err.Error())
os.Exit(1)
}
}
func app(ctx context.Context) error {
var cfgpath string
if len(os.Args) > 1 {
cfgpath = os.Args[1]
} else {
cfgpath = "config.json"
}
cfg, err := readFromFile(cfgpath, defaultConfig)
if err != nil {
return fmt.Errorf("reading config from file: %w", err)
}
log := config.NewSLogger(cfg.Log)
app, err := service.NewApplication(ctx, service.ApplicationConfig{
LogConfig: cfg.Log,
YDB: cfg.YDB,
})
if err != nil {
return fmt.Errorf("making new application: %w", err)
}
sravniClient, err := sravni.NewClient(ctx, log, cfg.DebugHTTP)
if err != nil {
return fmt.Errorf("making sravni client: %w", err)
}
bgProcess := ports.NewBackgroundProcess(ctx, log)
err = bgProcess.RegisterSyncSravniHandler(ctx, app, sravniClient, cfg.SyncSravniCron)
if err != nil {
return fmt.Errorf("registering sync sravni handler: %w", err)
}
xcontext.LogInfo(ctx, log, "schedule", xlog.StringerAttr("untils", bgProcess.GetNextRunStats()))
eg, egctx := errgroup.WithContext(ctx)
xcontext.LogInfo(ctx, log, "running routines")
eg.Go(func() error {
xcontext.LogInfo(ctx, log, "running bgprocess")
defer xcontext.LogInfo(ctx, log, "finished bprocess")
bgProcess.Run()
return nil
})
eg.Go(func() error {
xcontext.LogInfo(ctx, log, "running cancelation waiter")
defer xcontext.LogInfo(ctx, log, "finished cancelation waiter")
<-egctx.Done()
sdctx, sdcancel := context.WithTimeout(context.Background(), time.Second*15)
defer sdcancel()
return bgProcess.Shutdown(sdctx)
})
err = eg.Wait()
if err != nil {
return err
}
return nil
}

View File

@ -13,8 +13,8 @@ import (
)
const (
learningTypeOptName = "learning_type"
courseThematicOptName = "course_thematic"
learningTypeOptName = "learning-type"
courseThematicOptName = "course-thematic"
)
func setupAPICommand(ctx context.Context) cli.Command {
@ -98,13 +98,11 @@ func (a *listProductsAction) parse(args []string, options map[string]string) err
a.params.learningType, ok = options[learningTypeOptName]
if !ok {
return errors.SimpleError("learning_type is empty")
}
a.params.courseThematic, ok = options[courseThematicOptName]
if !ok {
return errors.SimpleError("course_thematic is empty")
return errors.SimpleError(learningTypeOptName + " is empty")
}
a.params.courseThematic = options[courseThematicOptName]
if value, ok := options[limitOption.Key()]; ok {
a.params.limit, _ = strconv.Atoi(value)
}