1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

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 (
"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 == "" {

View File

@ -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)

49
main.go
View File

@ -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()

View File

@ -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