From 406f5147c818c6059e84cb6b95672c33837584fe Mon Sep 17 00:00:00 2001 From: Steven Tang Date: Mon, 3 Apr 2023 22:40:57 +1000 Subject: [PATCH] feat: -version flag --- config/config.go | 20 +++++++----------- config/jobqueue.go | 7 +++++-- main.go | 49 +++++++++++++++++++++++++++------------------ models/user_test.go | 7 ++++--- 4 files changed, 45 insertions(+), 38 deletions(-) diff --git a/config/config.go b/config/config.go index c48f941..b912a37 100644 --- a/config/config.go +++ b/config/config.go @@ -2,11 +2,9 @@ package config import ( "encoding/json" - "flag" "fmt" - "github.com/robfig/cron/v3" - "io/ioutil" "net/http" + "os" "regexp" "strconv" "strings" @@ -17,11 +15,12 @@ import ( "github.com/jinzhu/configor" "github.com/muety/wakapi/data" "github.com/muety/wakapi/utils" + "github.com/robfig/cron/v3" uuid "github.com/satori/go.uuid" ) const ( - defaultConfigPath = "config.yml" + DefaultConfigPath = "config.yml" SQLDialectMysql = "mysql" SQLDialectPostgres = "postgres" @@ -65,7 +64,6 @@ var emailProviders = []string{ } var cfg *Config -var cFlag = flag.String("config", defaultConfigPath, "config file location") var env string type appConfig struct { @@ -344,7 +342,7 @@ func readColors() map[string]map[string]string { raw := data.ColorsFile if IsDev(env) { - raw, _ = ioutil.ReadFile("data/colors.json") + raw, _ = os.ReadFile("data/colors.json") } var colors = make(map[string]map[string]string) @@ -376,12 +374,10 @@ func Get() *Config { return cfg } -func Load(version string) *Config { +func Load(configFlag string, version string) *Config { config := &Config{} - flag.Parse() - - if err := configor.New(&configor.Config{}).Load(config, *cFlag); err != nil { + if err := configor.New(&configor.Config{}).Load(config, configFlag); err != nil { logbuch.Fatal("failed to read config: %v", err) } @@ -410,9 +406,7 @@ func Load(version string) *Config { config.Security.SecureCookie = securecookie.New(hashKey, blockKey) config.Security.SessionKey = sessionKey - if strings.HasSuffix(config.Server.BasePath, "/") { - config.Server.BasePath = config.Server.BasePath[:len(config.Server.BasePath)-1] - } + config.Server.BasePath = strings.TrimSuffix(config.Server.BasePath, "/") for k, v := range config.App.CustomLanguages { if v == "" { diff --git a/config/jobqueue.go b/config/jobqueue.go index 1fdb69f..a3b05d0 100644 --- a/config/jobqueue.go +++ b/config/jobqueue.go @@ -2,10 +2,11 @@ package config import ( "fmt" - "github.com/emvi/logbuch" - "github.com/muety/artifex/v2" "math" "runtime" + + "github.com/emvi/logbuch" + "github.com/muety/artifex/v2" ) var jobQueues map[string]*artifex.Dispatcher @@ -28,7 +29,9 @@ type JobQueueMetrics struct { func init() { jobQueues = make(map[string]*artifex.Dispatcher) +} +func StartJobs() { InitQueue(QueueDefault, 1) InitQueue(QueueProcessing, halfCPUs()) InitQueue(QueueReports, 1) diff --git a/main.go b/main.go index fe44597..ea037d9 100644 --- a/main.go +++ b/main.go @@ -2,15 +2,7 @@ package main import ( "embed" - "github.com/go-chi/chi/v5" - middleware "github.com/go-chi/chi/v5/middleware" - "github.com/lpar/gzipped/v2" - "github.com/muety/wakapi/middlewares" - shieldsV1Routes "github.com/muety/wakapi/routes/compat/shields/v1" - wtV1Routes "github.com/muety/wakapi/routes/compat/wakatime/v1" - "github.com/muety/wakapi/routes/relay" - fsutils "github.com/muety/wakapi/utils/fs" - httpSwagger "github.com/swaggo/http-swagger" + "flag" "io/fs" "log" "net" @@ -19,23 +11,30 @@ import ( "strconv" "time" - "github.com/muety/wakapi/static/docs" - "github.com/emvi/logbuch" - conf "github.com/muety/wakapi/config" - "github.com/muety/wakapi/migrations" - "github.com/muety/wakapi/repositories" - "github.com/muety/wakapi/routes" - "github.com/muety/wakapi/routes/api" - "github.com/muety/wakapi/services" - "github.com/muety/wakapi/services/mail" + "github.com/go-chi/chi/v5" + middleware "github.com/go-chi/chi/v5/middleware" + "github.com/lpar/gzipped/v2" + httpSwagger "github.com/swaggo/http-swagger" _ "gorm.io/driver/mysql" _ "gorm.io/driver/postgres" _ "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" - _ "github.com/muety/wakapi/static/docs" + conf "github.com/muety/wakapi/config" + "github.com/muety/wakapi/middlewares" + "github.com/muety/wakapi/migrations" + "github.com/muety/wakapi/repositories" + "github.com/muety/wakapi/routes" + "github.com/muety/wakapi/routes/api" + shieldsV1Routes "github.com/muety/wakapi/routes/compat/shields/v1" + wtV1Routes "github.com/muety/wakapi/routes/compat/wakatime/v1" + "github.com/muety/wakapi/routes/relay" + "github.com/muety/wakapi/services" + "github.com/muety/wakapi/services/mail" + docs "github.com/muety/wakapi/static/docs" + fsutils "github.com/muety/wakapi/utils/fs" ) // Embed version.txt @@ -106,7 +105,15 @@ var ( // @name Authorization func main() { - config = conf.Load(version) + var versionFlag = flag.Bool("version", false, "print version") + var configFlag = flag.String("config", conf.DefaultConfigPath, "config file location") + flag.Parse() + + if *versionFlag { + print(version) + os.Exit(0) + } + config = conf.Load(*configFlag, version) // Configure Swagger docs docs.SwaggerInfo.BasePath = config.Server.BasePath + "/api" @@ -117,6 +124,7 @@ func main() { } else { logbuch.SetLevel(logbuch.LevelInfo) } + logbuch.Info("Wakapi " + version) // Set up GORM gormLogger := logger.New( @@ -184,6 +192,7 @@ func main() { miscService = services.NewMiscService(userService, heartbeatService, summaryService, keyValueService, mailService) // Schedule background tasks + go conf.StartJobs() go aggregationService.Schedule() go leaderboardService.Schedule() go reportService.Schedule() diff --git a/models/user_test.go b/models/user_test.go index 840aa80..d35c70a 100644 --- a/models/user_test.go +++ b/models/user_test.go @@ -1,10 +1,11 @@ package models import ( - conf "github.com/muety/wakapi/config" - "github.com/stretchr/testify/assert" "testing" "time" + + conf "github.com/muety/wakapi/config" + "github.com/stretchr/testify/assert" ) func TestUser_TZ(t *testing.T) { @@ -21,7 +22,7 @@ func TestUser_TZ(t *testing.T) { } func TestUser_MinDataAge(t *testing.T) { - c := conf.Load("") + c := conf.Load("", "") var sut *User