goploy/config/Config.go

110 lines
2.0 KiB
Go
Raw Normal View History

2021-12-14 17:42:12 +08:00
package config
import (
"github.com/pelletier/go-toml/v2"
"io/ioutil"
2022-01-08 21:35:57 +08:00
"time"
2021-12-14 17:42:12 +08:00
)
type Config struct {
Env string `toml:"env"`
2021-12-17 17:55:26 +08:00
APP APPConfig `toml:"app"`
2021-12-14 17:42:12 +08:00
Cookie CookieConfig `toml:"cookie"`
JWT JWTConfig `toml:"jwt"`
DB DBConfig `toml:"db"`
Log LogConfig `toml:"log"`
Web WebConfig `toml:"web"`
LDAP LDAPConfig `toml:"ldap"`
}
2021-12-17 17:55:26 +08:00
type APPConfig struct {
2022-01-08 21:35:57 +08:00
DeployLimit int32 `toml:"deployLimit"`
ShutdownTimeout time.Duration `toml:"shutdownTimeout"`
2021-12-17 17:55:26 +08:00
}
2021-12-14 17:42:12 +08:00
type CookieConfig struct {
Name string `toml:"name"`
Expire int `toml:"expire"` // second
}
type JWTConfig struct {
Key string `toml:"key"`
}
type DBConfig struct {
Type string `toml:"type"`
User string `toml:"user"`
Password string `toml:"password"`
Host string `toml:"host"`
Port string `toml:"port"`
2021-12-25 16:39:23 +08:00
Database string `toml:"database"`
2021-12-14 17:42:12 +08:00
}
type LogConfig struct {
Path string `toml:"path"`
}
type WebConfig struct {
Port string `toml:"port"`
}
type LDAPConfig struct {
2021-12-15 16:17:31 +08:00
Enabled bool `toml:"enabled"`
URL string `toml:"url"`
BindDN string `toml:"bindDN"`
Password string `toml:"password"`
BaseDN string `toml:"baseDN"`
UID string `toml:"uid"`
UserFilter string `toml:"userFilter"`
2021-12-14 17:42:12 +08:00
}
var Toml Config
func Create(filename string) {
config, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
err = toml.Unmarshal(config, &Toml)
if err != nil {
panic(err)
}
2022-01-08 21:35:57 +08:00
setAPPDefault()
2021-12-25 16:39:23 +08:00
setDBDefault()
}
2022-01-08 21:35:57 +08:00
func setAPPDefault() {
if Toml.APP.ShutdownTimeout == 0 {
Toml.APP.ShutdownTimeout = 10
}
}
2021-12-25 16:39:23 +08:00
func setDBDefault() {
if Toml.DB.Type == "" {
Toml.DB.Type = "mysql"
}
if Toml.DB.Host == "" {
Toml.DB.Host = "127.0.0.1"
}
if Toml.DB.Port == "" {
Toml.DB.Port = "3306"
}
if Toml.DB.Database == "" {
Toml.DB.Database = "goploy"
}
2021-12-14 17:42:12 +08:00
}
func Write(filename string, cfg Config) error {
yamlData, err := toml.Marshal(&cfg)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, yamlData, 0644)
if err != nil {
return err
}
return nil
}