blog/bootstrap/bootstrap.go

91 lines
1.9 KiB
Go

package bootstrap
import (
"io"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
var Config bootstrap
type bootstrap struct {
Profile string `yaml:"profile"`
Iris iris `yaml:"iris"`
Database database `yaml:"database"`
}
// =========================================================
type iris struct {
Session session `yaml:"session"`
}
type session struct {
Address string `yaml:"address"`
Expires int `yaml:"expires"`
Password string `yaml:"password"`
Db string `yaml:"db"`
Prefix string `yaml:"prefix"`
}
//=========================================================
type database struct {
Redis redis `yaml:"redis"`
Sqlite sqlite `yaml:"sqlite"`
}
type redis struct {
Addr string `yaml:"addr"`
Db int `yaml:"db"`
Password string `yaml:"password"`
}
type sqlite struct {
BlogPath string `yaml:"blogPath"`
FilePath string `yaml:"filePath"`
}
//=========================================================
func init() {
//日志文件位置
f, err := os.OpenFile("./Iris.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic("日志文件错误")
}
//控制台和日志文件都输出
iw := io.MultiWriter(f, os.Stdout)
//日志设置
// logrus.SetFormatter(log.Ldate | log.Ltime | log.Lmicroseconds | log.Ltime | log.Llongfile)
logrus.SetOutput(iw)
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000",
})
logrus.SetReportCaller(true)
logrus.Info("init bootstrap")
b, err := os.ReadFile("conf/bootstrap.yaml")
if err != nil {
panic(err)
}
err3 := yaml.Unmarshal(b, &Config)
if err3 != nil {
panic(err3)
}
profile := Config.Profile
logrus.Info("当前Profile:", profile)
if profile == "dev" {
logrus.SetLevel(logrus.DebugLevel)
}
//初始化Gorm相关数据的配置
initBlogGorm()
initFileGorm()
initRedis()
}