-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathserver.go
52 lines (48 loc) · 1.4 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"52lu/go-import-template/global"
"52lu/go-import-template/middleware"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
// 获取自定义HTTP SERVER
func getCustomHttpServer(engine *gin.Engine) *http.Server {
// 创建自定义配置服务
httpServer := &http.Server{
//ip和端口号
Addr: global.GvaConfig.App.Addr,
//调用的处理器,如为nil会调用http.DefaultServeMux
Handler: engine,
//计算从成功建立连接到request body(或header)完全被读取的时间
ReadTimeout: time.Second * 10,
//计算从request body(或header)读取结束到 response write结束的时间
WriteTimeout: time.Second * 10,
//请求头的最大长度,如为0则用DefaultMaxHeaderBytes
MaxHeaderBytes: 1 << 20,
}
return httpServer
}
// RunServer 启动服务
func RunServer() {
engine := gin.New()
// 注册公共中间件
engine.Use(gin.Recovery(), middleware.CatchErrorMiddleWare())
// 获取自定义http配置
httpServer := getCustomHttpServer(engine)
// 注册路由
registerRouters(engine)
// 打印服务信息
printServerInfo()
// 启动服务
err := httpServer.ListenAndServe()
if err != nil {
panic("启动失败: " + err.Error())
}
}
// 打印服务信息
func printServerInfo() {
appConfig := global.GvaConfig.App
fmt.Printf("\n【 当前环境: %s 当前版本: %s 接口地址: http://%s 】\n", appConfig.Env, appConfig.Version, appConfig.Addr)
}