feat:完善项目信息

This commit is contained in:
henry
2021-12-28 10:38:02 +08:00
parent 76ca837fd6
commit 7eb8e5c48b
13 changed files with 161 additions and 137 deletions

View File

@ -6,8 +6,19 @@ import (
"SciencesServer/serve/orm/logic"
"fmt"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"os"
)
type Config struct {
Debug bool `json:"debug" yaml:"debug"`
Mode string `json:"mode" yaml:"mode"`
MaxLifetime int `json:"max_lifetime" yaml:"max_lifetime"`
MaxOpenConns int `json:"max_open_conns" yaml:"max_open_conns"`
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
Engines map[string]map[string]interface{} `json:"engines" yaml:"engines"`
}
type mysql struct {
host string
port int
@ -30,6 +41,7 @@ var (
run()
},
}
file string
mode string
_mysql = new(mysql)
@ -37,6 +49,7 @@ var (
)
func init() {
Cmd.PersistentFlags().StringVarP(&file, "file", "f", "./default_engine.yaml", "文件存储现支持yaml文件默认./default_engine.yaml")
Cmd.PersistentFlags().StringVarP(&mode, "mode", "m", "mysql", "数据引擎支持mysql和sqlite默认mysql")
Cmd.PersistentFlags().StringVarP(&_mysql.host, "mysql_host", "H", "127.0.0.1", "主机名默认127.0.0.1")
Cmd.PersistentFlags().IntVarP(&_mysql.port, "mysql_port", "P", 3306, "端口号默认为3306")
@ -54,9 +67,9 @@ func run() {
engine := orm.NewInstance(
orm.WithDBMode(mode),
orm.WithMysqlOption(&logic.Mysql{
User: _mysql.username, Password: _mysql.password,
Host: _mysql.host, Port: _mysql.port,
DBName: _mysql.database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
Username: _mysql.username, Password: _mysql.password,
Database: _mysql.database, Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Local",
}),
orm.WithSqliteOption(&logic.Sqlite{Path: _sqlite.path, Name: _sqlite.name}),
).Init()
@ -65,5 +78,44 @@ func run() {
// 迁移数据
migrate.NewInstance(migrate.WithGormDBOption(engine.Engine)).Handle()
if err := saveFile(file, &Config{
Debug: true,
Mode: mode,
MaxLifetime: 3600,
MaxOpenConns: 2000,
MaxIdleConns: 1000,
Engines: map[string]map[string]interface{}{
"mysql": map[string]interface{}{
"host": _mysql.host,
"port": _mysql.port,
"database": _mysql.database,
"username": _mysql.username,
"password": _mysql.password,
"parameters": "charset=utf8mb4,utf8&parseTime=True&loc=Local",
},
"sqlite": map[string]interface{}{
"path": _sqlite.path,
"name": _sqlite.name,
},
},
}); err != nil {
fmt.Errorf("数据初始化文件错误:%v", err)
return
}
fmt.Println("========================\n=== 数据初始化成功 ===\n========================")
}
func saveFile(_file string, data *Config) error {
file, err := os.OpenFile(_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
enc := yaml.NewEncoder(file)
err = enc.Encode(data)
return err
}