125 lines
3.0 KiB
Go
125 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"blog/global"
|
|
"blog/internal/model"
|
|
"blog/internal/routess"
|
|
"blog/pkg/logger"
|
|
"blog/pkg/setting"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
"github.com/go-playground/locales/en"
|
|
"github.com/go-playground/locales/zh"
|
|
"github.com/go-playground/locales/zh_Hant_TW"
|
|
ut "github.com/go-playground/universal-translator"
|
|
"github.com/go-playground/validator/v10"
|
|
enTranslations "github.com/go-playground/validator/v10/translations/en"
|
|
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
err := setupSetting()
|
|
if err != nil {
|
|
log.Fatalf("init.setupSetting err: %v", err)
|
|
}
|
|
err = setupDBEngine()
|
|
if err != nil {
|
|
log.Fatalf("init.setupDBEngine err: %v", err)
|
|
}
|
|
err = setupLogger()
|
|
if err != nil {
|
|
log.Fatalf("init.setupLogger err: %v", err)
|
|
}
|
|
}
|
|
func setupDBEngine() error {
|
|
var err error
|
|
global.DBEngine, err = model.NewDBEngine(global.DatabaseSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
func setupLogger() error {
|
|
global.Logger = logger.NewLogger(&lumberjack.Logger{
|
|
Filename: global.AppSetting.LogSavePath + "/" + global.AppSetting.LogFileName + global.AppSetting.LogFileExt,
|
|
MaxSize: 600,
|
|
MaxAge: 10,
|
|
LocalTime: true,
|
|
}, "", log.LstdFlags).WithCaller(2)
|
|
|
|
return nil
|
|
}
|
|
|
|
func setupSetting() error {
|
|
newSetting, err := setting.NewSetting()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = newSetting.ReadSection("Server", &global.ServerSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = newSetting.ReadSection("App", &global.AppSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = newSetting.ReadSection("Database", &global.DatabaseSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = newSetting.ReadSection("JWT", &global.JWTSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = newSetting.ReadSection("Email", &global.EmailSetting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
uni := ut.New(en.New(), zh.New(), zh_Hant_TW.New())
|
|
global.ZhTrans, _ = uni.GetTranslator("zh")
|
|
global.EnTrans, _ = uni.GetTranslator("en")
|
|
v, ok := binding.Validator.Engine().(*validator.Validate)
|
|
if ok {
|
|
err = zhTranslations.RegisterDefaultTranslations(v, global.ZhTrans)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = enTranslations.RegisterDefaultTranslations(v, global.EnTrans)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
global.JWTSetting.Expire *= time.Second
|
|
global.ServerSetting.ReadTimeout *= time.Second
|
|
global.ServerSetting.WriteTimeout *= time.Second
|
|
return nil
|
|
}
|
|
|
|
// @title 博客系统
|
|
// @version 1.0
|
|
// @description Go 语言编程之旅:一起用 Go 做项目
|
|
// @termsOfService https://github.com/go-programming-tour-book
|
|
// @securityDefinitions.apikey ApiKeyAuth
|
|
// @in header
|
|
// @name Authorization
|
|
func main() {
|
|
gin.SetMode(global.ServerSetting.RunMode)
|
|
|
|
r := routess.NewRouter()
|
|
s := &http.Server{
|
|
Addr: ":8080",
|
|
Handler: r,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
s.ListenAndServe()
|
|
}
|