wip: alpha version
This commit is contained in:
84
internal/opt/opt.go
Normal file
84
internal/opt/opt.go
Normal file
@ -0,0 +1,84 @@
|
||||
package opt
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
Type string `json:"type"` // postgresql, mysql, sqlite
|
||||
Path string `json:"path"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
DB string `json:"db"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
Type string `json:"type"` // redis, memor
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type config struct {
|
||||
Name string `json:"name"`
|
||||
Address string `json:"address"`
|
||||
Database database `json:"database"`
|
||||
Cache cache `json:"cache"`
|
||||
}
|
||||
|
||||
var (
|
||||
ConfigFile string
|
||||
Debug int
|
||||
Cfg = &config{}
|
||||
)
|
||||
|
||||
func MustInitConfig() {
|
||||
flag.Parse()
|
||||
|
||||
var (
|
||||
err error
|
||||
cf *os.File
|
||||
bs []byte
|
||||
)
|
||||
|
||||
if cf, err = os.Open(ConfigFile); err != nil {
|
||||
logrus.Panicf("MustInitConfig: open config file=%s err=%v", ConfigFile, err)
|
||||
}
|
||||
defer cf.Close()
|
||||
|
||||
if bs, err = io.ReadAll(cf); err != nil {
|
||||
logrus.Panicf("MustInitConfig: read in config file err=%v", err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(bs, Cfg); err != nil {
|
||||
logrus.Panicf("MustInitConfig: json marshal config=%s err=%v", string(bs), err)
|
||||
}
|
||||
|
||||
if Debug > 0 {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
|
||||
if Debug >= 9999 {
|
||||
logrus.SetLevel(logrus.TraceLevel)
|
||||
logrus.SetReportCaller(true)
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
ForceColors: true,
|
||||
CallerPrettyfier: func(frame *runtime.Frame) (string, string) {
|
||||
file := path.Base(frame.File)
|
||||
return "", " " + file + ":" + strconv.Itoa(frame.Line)
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Infof("read in config detail: \n%s", string(bs))
|
||||
}
|
46
internal/opt/var.go
Normal file
46
internal/opt/var.go
Normal file
@ -0,0 +1,46 @@
|
||||
package opt
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
Version = "v0.0.1"
|
||||
|
||||
// todo: 可以替换自己生生成的 secret
|
||||
JwtTokenSecret = "7^D+UW3BPB2Mnz)bY3uVrAUyv&dj8Kdz"
|
||||
|
||||
// todo: 是否打开 gorm 的 debug 打印 (开发和 dev 环境时可以打开)
|
||||
DBDebug = true
|
||||
|
||||
// todo: 同一个账号是否可以多 client 登录
|
||||
MultiLogin = false
|
||||
|
||||
// todo: 用户量不大的情况, 并没有缓存用户具体信息, 如果需要可以打开
|
||||
EnableUserCache = true
|
||||
|
||||
// todo: 缓存时, key 的前缀
|
||||
CachePrefix = "ultone"
|
||||
|
||||
// todo: 登录颁发的 cookie 的 name
|
||||
CookieName = "utlone-token"
|
||||
|
||||
// todo: 用户列表,日志列表 size 参数
|
||||
DefaultSize, MaxSize = 20, 200
|
||||
|
||||
// todo: 操作用户时, role 相等时能否操作: 包括 列表, 能否新建,修改,删除同样 role 的用户
|
||||
RoleMustLess = false
|
||||
|
||||
// todo: 通过 c.Local() 存入 oplog 时的 key 值
|
||||
OpLogLocalKey = "oplog"
|
||||
|
||||
// todo: 操作日志 最多延迟多少秒写入(最多缓存多少秒的日志,然后 bulk 写入)
|
||||
OpLogWriteDurationSecond = 5
|
||||
|
||||
TaskMinTimeout = 10
|
||||
TaskMaxTimeout = 24 * 3600
|
||||
TaskFetchInterval = 5 * 60
|
||||
)
|
||||
|
||||
var (
|
||||
// todo: 颁发的 token, (cookie) 在缓存中存在的时间 (每次请求该时间也会被刷新)
|
||||
TokenTimeout = time.Duration(3600*12) * time.Second
|
||||
)
|
Reference in New Issue
Block a user