2021-05-28 10:40:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blog/global"
|
|
|
|
"blog/internal/model"
|
|
|
|
"blog/internal/routess"
|
|
|
|
"blog/pkg/logger"
|
|
|
|
"blog/pkg/setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-06-03 09:03:43 +00:00
|
|
|
"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"
|
2021-05-28 10:40:39 +00:00
|
|
|
_ "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
|
|
|
|
}
|
2021-05-31 09:40:47 +00:00
|
|
|
err = newSetting.ReadSection("JWT", &global.JWTSetting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-01 08:19:40 +00:00
|
|
|
err = newSetting.ReadSection("Email", &global.EmailSetting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-03 09:03:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 09:40:47 +00:00
|
|
|
global.JWTSetting.Expire *= time.Second
|
2021-05-28 10:40:39 +00:00
|
|
|
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
|
2021-06-01 08:19:40 +00:00
|
|
|
// @securityDefinitions.apikey ApiKeyAuth
|
|
|
|
// @in header
|
|
|
|
// @name Authorization
|
2021-05-28 10:40:39 +00:00
|
|
|
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()
|
|
|
|
}
|