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

73 lines
1.8 KiB
Go

7 months ago
//
// 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 (
6 months ago
"github.com/veypi/OneAuth"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
3 months ago
"github.com/vyes-ai/vigo"
"github.com/vyes-ai/vigo/flags"
"github.com/vyes-ai/vigo/logv"
7 months ago
)
6 months ago
var cmdMain = flags.New("app", "the backend server of app")
var cmdCfg = cmdMain.SubCommand("cfg", "generate cfg file")
var cmdDB = cmdMain.SubCommand("db", "database operations")
var configFile = cmdMain.String("f", "./dev.yaml", "the config file")
6 months ago
6 months ago
var cliOpts = &struct {
Host string `json:"host"`
Port int `json:"port"`
LoggerPath string `json:"logger_path,omitempty"`
LoggerLevel string `json:"logger_level,omitempty"`
Core *cfg.Options
}{
Core: cfg.Config,
}
6 months ago
func init() {
6 months ago
cmdMain.StringVar(&cliOpts.Host, "host", "0.0.0.0", "host")
cmdMain.IntVar(&cliOpts.Port, "p", 4000, "port")
cmdMain.StringVar(&cliOpts.LoggerLevel, "l", "info", "log level")
cmdMain.Before = func() error {
6 months ago
flags.LoadCfg(*configFile, cfg.Config)
6 months ago
cmdMain.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cliOpts.LoggerLevel)))
6 months ago
return nil
}
6 months ago
cmdMain.Command = runWeb
6 months ago
cmdCfg.Command = func() error {
6 months ago
return flags.DumpCfg(*configFile, cfg.Config)
6 months ago
}
cmdDB.SubCommand("migrate", "migrate database").Command = models.Migrate
cmdDB.SubCommand("drop", "drop database").Command = models.Drop
6 months ago
cmdDB.SubCommand("init", "init db data").Command = models.InitDB
6 months ago
}
func main() {
6 months ago
cmdMain.Parse()
err := cmdMain.Run()
7 months ago
if err != nil {
logv.Warn().Msg(err.Error())
}
}
func runWeb() error {
server, err := vigo.New(vigo.WithHost(cliOpts.Host), vigo.WithPort(cliOpts.Port))
7 months ago
if err != nil {
return err
}
6 months ago
server.SetRouter(OneAuth.Router)
server.EnableAI()
7 months ago
server.Router().Print()
return server.Run()
}