框架基本搭建完成
This commit is contained in:
37
config/config.full.yml
Normal file
37
config/config.full.yml
Normal file
@@ -0,0 +1,37 @@
|
||||
# 应用版本,应与当前版本一致
|
||||
version: {{ .Version }}
|
||||
|
||||
# 应用配置
|
||||
app:
|
||||
# 域名或IP
|
||||
host: 0.0.0.0
|
||||
# 端口
|
||||
port: 7899
|
||||
|
||||
# 安全
|
||||
security:
|
||||
jwt:
|
||||
# Token过期时间 (默认4小时)
|
||||
expire: 14400
|
||||
# Refresh Token过期时间 (默认24小时)
|
||||
refresh-expire: 86400
|
||||
# 以下字段指定key的文件路径
|
||||
private-key: id_ecdsa
|
||||
public-key: id_ecdsa.pub
|
||||
# 允许额外的跨域请求
|
||||
allow-CORS:
|
||||
- "*"
|
||||
|
||||
# 数据库配置
|
||||
database:
|
||||
driver: sqlite3
|
||||
dsn: file:drivelinked.db?cache=shared&mode=rwc
|
||||
# DSN references
|
||||
# sqlite3 https://github.com/mattn/go-sqlite3#dsn-examples
|
||||
# mysql https://github.com/go-sql-driver/mysql#dsn-data-source-name
|
||||
# postgres https://github.com/lib/pq
|
||||
|
||||
max-idle-conns: 20
|
||||
max-open-conns: 50
|
||||
# with unit sec
|
||||
conn-max-life-time: 30
|
99
config/config.go
Normal file
99
config/config.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"drive-linked/pkg/utils"
|
||||
_ "embed"
|
||||
"github.com/kataras/golog"
|
||||
"gopkg.in/yaml.v2"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Version string
|
||||
App struct {
|
||||
Host string
|
||||
port uint16
|
||||
}
|
||||
Security struct {
|
||||
AllowCORS []string `yaml:"allow-CORS"`
|
||||
Jwt struct {
|
||||
Expire uint32
|
||||
RefreshExpire uint32 `yaml:"refresh-expire"`
|
||||
PrivateKey string `yaml:"private-key"`
|
||||
PublicKey string `yaml:"public-key"`
|
||||
}
|
||||
}
|
||||
Database struct {
|
||||
Driver string
|
||||
Dsn string
|
||||
}
|
||||
}
|
||||
|
||||
var Cfg Config
|
||||
|
||||
//go:embed config.full.yml
|
||||
var configFull string
|
||||
|
||||
// 加载配置文件
|
||||
func SetupConfig() {
|
||||
// 读取配置文件,若不存在则创建
|
||||
if isExist := utils.FileExist("config.yml"); !isExist {
|
||||
newCfgFile()
|
||||
}
|
||||
|
||||
// 读取文件
|
||||
cfgFile, err := os.OpenFile("config.yml", os.O_RDONLY, 0755)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
cfgContent, err := ioutil.ReadAll(cfgFile)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
|
||||
// 解析文件
|
||||
err = yaml.Unmarshal(cfgContent, &Cfg)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
golog.Debug("Loaded config: ", Cfg)
|
||||
}
|
||||
|
||||
// 解析配置文件模板
|
||||
func parseConfigTmpl() []byte {
|
||||
// 加载模板
|
||||
cfgTmpl, err := template.New("config.full.yml").Parse(configFull)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
|
||||
// 解析
|
||||
var tmplBytes bytes.Buffer
|
||||
err = cfgTmpl.Execute(&tmplBytes, Cfg)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
|
||||
return tmplBytes.Bytes()
|
||||
}
|
||||
|
||||
// 创建配置文件
|
||||
func newCfgFile() {
|
||||
// 解析配置文件模板
|
||||
tmplBytes := parseConfigTmpl()
|
||||
|
||||
// 生成新配置文件config.yml
|
||||
cfgFile, err := os.OpenFile("config.yml", os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
defer cfgFile.Close()
|
||||
|
||||
_, err = cfgFile.Write(tmplBytes)
|
||||
if err != nil {
|
||||
golog.Fatal(err)
|
||||
}
|
||||
}
|
7
config/config_test.go
Normal file
7
config/config_test.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSetupConfig(t *testing.T) {
|
||||
Cfg.Version = "0.0.1-dev"
|
||||
}
|
Reference in New Issue
Block a user