feat: -version flag

This commit is contained in:
Steven Tang 2023-04-03 22:40:57 +10:00
parent d061a4ef1b
commit 406f5147c8
No known key found for this signature in database
GPG Key ID: 1597520C734BAE66
4 changed files with 45 additions and 38 deletions

View File

@ -2,11 +2,9 @@ package config
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"github.com/robfig/cron/v3"
"io/ioutil"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -17,11 +15,12 @@ import (
"github.com/jinzhu/configor" "github.com/jinzhu/configor"
"github.com/muety/wakapi/data" "github.com/muety/wakapi/data"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/utils"
"github.com/robfig/cron/v3"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
) )
const ( const (
defaultConfigPath = "config.yml" DefaultConfigPath = "config.yml"
SQLDialectMysql = "mysql" SQLDialectMysql = "mysql"
SQLDialectPostgres = "postgres" SQLDialectPostgres = "postgres"
@ -65,7 +64,6 @@ var emailProviders = []string{
} }
var cfg *Config var cfg *Config
var cFlag = flag.String("config", defaultConfigPath, "config file location")
var env string var env string
type appConfig struct { type appConfig struct {
@ -344,7 +342,7 @@ func readColors() map[string]map[string]string {
raw := data.ColorsFile raw := data.ColorsFile
if IsDev(env) { if IsDev(env) {
raw, _ = ioutil.ReadFile("data/colors.json") raw, _ = os.ReadFile("data/colors.json")
} }
var colors = make(map[string]map[string]string) var colors = make(map[string]map[string]string)
@ -376,12 +374,10 @@ func Get() *Config {
return cfg return cfg
} }
func Load(version string) *Config { func Load(configFlag string, version string) *Config {
config := &Config{} config := &Config{}
flag.Parse() if err := configor.New(&configor.Config{}).Load(config, configFlag); err != nil {
if err := configor.New(&configor.Config{}).Load(config, *cFlag); err != nil {
logbuch.Fatal("failed to read config: %v", err) 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.SecureCookie = securecookie.New(hashKey, blockKey)
config.Security.SessionKey = sessionKey config.Security.SessionKey = sessionKey
if strings.HasSuffix(config.Server.BasePath, "/") { config.Server.BasePath = strings.TrimSuffix(config.Server.BasePath, "/")
config.Server.BasePath = config.Server.BasePath[:len(config.Server.BasePath)-1]
}
for k, v := range config.App.CustomLanguages { for k, v := range config.App.CustomLanguages {
if v == "" { if v == "" {

View File

@ -2,10 +2,11 @@ package config
import ( import (
"fmt" "fmt"
"github.com/emvi/logbuch"
"github.com/muety/artifex/v2"
"math" "math"
"runtime" "runtime"
"github.com/emvi/logbuch"
"github.com/muety/artifex/v2"
) )
var jobQueues map[string]*artifex.Dispatcher var jobQueues map[string]*artifex.Dispatcher
@ -28,7 +29,9 @@ type JobQueueMetrics struct {
func init() { func init() {
jobQueues = make(map[string]*artifex.Dispatcher) jobQueues = make(map[string]*artifex.Dispatcher)
}
func StartJobs() {
InitQueue(QueueDefault, 1) InitQueue(QueueDefault, 1)
InitQueue(QueueProcessing, halfCPUs()) InitQueue(QueueProcessing, halfCPUs())
InitQueue(QueueReports, 1) InitQueue(QueueReports, 1)

49
main.go
View File

@ -2,15 +2,7 @@ package main
import ( import (
"embed" "embed"
"github.com/go-chi/chi/v5" "flag"
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"
"io/fs" "io/fs"
"log" "log"
"net" "net"
@ -19,23 +11,30 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/muety/wakapi/static/docs"
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
conf "github.com/muety/wakapi/config" "github.com/go-chi/chi/v5"
"github.com/muety/wakapi/migrations" middleware "github.com/go-chi/chi/v5/middleware"
"github.com/muety/wakapi/repositories" "github.com/lpar/gzipped/v2"
"github.com/muety/wakapi/routes" httpSwagger "github.com/swaggo/http-swagger"
"github.com/muety/wakapi/routes/api"
"github.com/muety/wakapi/services"
"github.com/muety/wakapi/services/mail"
_ "gorm.io/driver/mysql" _ "gorm.io/driver/mysql"
_ "gorm.io/driver/postgres" _ "gorm.io/driver/postgres"
_ "gorm.io/driver/sqlite" _ "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger" "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 // Embed version.txt
@ -106,7 +105,15 @@ var (
// @name Authorization // @name Authorization
func main() { 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 // Configure Swagger docs
docs.SwaggerInfo.BasePath = config.Server.BasePath + "/api" docs.SwaggerInfo.BasePath = config.Server.BasePath + "/api"
@ -117,6 +124,7 @@ func main() {
} else { } else {
logbuch.SetLevel(logbuch.LevelInfo) logbuch.SetLevel(logbuch.LevelInfo)
} }
logbuch.Info("Wakapi " + version)
// Set up GORM // Set up GORM
gormLogger := logger.New( gormLogger := logger.New(
@ -184,6 +192,7 @@ func main() {
miscService = services.NewMiscService(userService, heartbeatService, summaryService, keyValueService, mailService) miscService = services.NewMiscService(userService, heartbeatService, summaryService, keyValueService, mailService)
// Schedule background tasks // Schedule background tasks
go conf.StartJobs()
go aggregationService.Schedule() go aggregationService.Schedule()
go leaderboardService.Schedule() go leaderboardService.Schedule()
go reportService.Schedule() go reportService.Schedule()

View File

@ -1,10 +1,11 @@
package models package models
import ( import (
conf "github.com/muety/wakapi/config"
"github.com/stretchr/testify/assert"
"testing" "testing"
"time" "time"
conf "github.com/muety/wakapi/config"
"github.com/stretchr/testify/assert"
) )
func TestUser_TZ(t *testing.T) { func TestUser_TZ(t *testing.T) {
@ -21,7 +22,7 @@ func TestUser_TZ(t *testing.T) {
} }
func TestUser_MinDataAge(t *testing.T) { func TestUser_MinDataAge(t *testing.T) {
c := conf.Load("") c := conf.Load("", "")
var sut *User var sut *User