You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/cli/main.go

65 lines
1.7 KiB
Go

//
// main.go
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//
package main
import (
app "oa"
"oa/cfg"
"oa/models"
"github.com/veypi/OneBD/rest"
"github.com/veypi/utils/flags"
"github.com/veypi/utils/logv"
)
var CMD = flags.New("app", "the backend server of app")
var cmdCfg = CMD.SubCommand("cfg", "generate cfg file")
var cmdDB = CMD.SubCommand("db", "database operations")
var configFile = CMD.String("f", "./dev.yaml", "the config file")
func init() {
CMD.StringVar(&cfg.Config.Host, "host", "0.0.0.0", "host")
CMD.IntVar(&cfg.Config.Port, "p", 4000, "port")
CMD.StringVar(&cfg.Config.LoggerLevel, "l", "info", "log level")
CMD.StringVar(&cfg.Config.DSN, "dsn", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local", "data source name")
CMD.StringVar(&cfg.Config.DB, "db", "mysql", "data source type: mysql/postgre/sqlite")
CMD.Before = func() error {
flags.LoadCfg(*configFile, cfg.Config)
CMD.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cfg.Config.LoggerLevel)))
return nil
}
CMD.Command = runWeb
cmdCfg.Command = func() error {
flags.DumpCfg(*configFile, cfg.Config)
return nil
}
cmdDB.SubCommand("migrate", "migrate database").Command = models.Migrate
cmdDB.SubCommand("drop", "drop database").Command = models.Drop
cmdDB.SubCommand("init", "init db data").Command = models.Init
}
func main() {
CMD.Parse()
err := CMD.Run()
if err != nil {
logv.Warn().Msg(err.Error())
}
}
func runWeb() error {
server, err := rest.New(rest.WithHost(cfg.Config.Host), rest.WithPort(cfg.Config.Port))
if err != nil {
return err
}
server.SetRouter(app.Router)
server.Router().Print()
return server.Run()
}