gin-blog/main.go

91 lines
2.0 KiB
Go
Raw Normal View History

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"
_ "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
}
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
func main() {
gin.SetMode(global.ServerSetting.RunMode)
global.Logger.Infof("%s: go-programming-tour-book/%s", "eddycjy", "blog-service")
r := routess.NewRouter()
s := &http.Server{
Addr: ":8080",
Handler: r,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}