58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"gin-server/models"
|
|
"gin-server/routers"
|
|
"log"
|
|
"net/http"
|
|
|
|
"gin-server/lib/setting"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func init() {
|
|
setting.Setup()
|
|
models.Setup()
|
|
}
|
|
|
|
// @title Gin server example
|
|
// @version 1.0
|
|
// @description An example of gin server
|
|
func main() {
|
|
gin.SetMode(setting.ServerSetting.RunMode)
|
|
|
|
routersInit := routers.InitRouter()
|
|
readTimeout := setting.ServerSetting.ReadTimeout
|
|
writeTimeout := setting.ServerSetting.WriteTimeout
|
|
|
|
endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
|
|
maxHeaderBytes := 1 << 20
|
|
|
|
server := &http.Server{
|
|
Addr: endPoint,
|
|
Handler: routersInit,
|
|
ReadTimeout: readTimeout,
|
|
WriteTimeout: writeTimeout,
|
|
MaxHeaderBytes: maxHeaderBytes,
|
|
}
|
|
|
|
log.Printf("[info] start http server listening %s", endPoint)
|
|
|
|
server.ListenAndServe()
|
|
|
|
// If you want Graceful Restart, you need a Unix system and download github.com/fvbock/endless
|
|
//endless.DefaultReadTimeOut = readTimeout
|
|
//endless.DefaultWriteTimeOut = writeTimeout
|
|
//endless.DefaultMaxHeaderBytes = maxHeaderBytes
|
|
//server := endless.NewServer(endPoint, routersInit)
|
|
//server.BeforeBegin = func(add string) {
|
|
// log.Printf("Actual pid is %d", syscall.Getpid())
|
|
//}
|
|
//
|
|
//err := server.ListenAndServe()
|
|
//if err != nil {
|
|
// log.Printf("Server err: %v", err)
|
|
//}
|
|
}
|