-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (61 loc) · 1.84 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/201R/go_api_boilerplate/database"
"github.com/201R/go_api_boilerplate/packages/logger"
"github.com/201R/go_api_boilerplate/packages/setting"
"github.com/201R/go_api_boilerplate/router"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
// @title Golang Rest API
// @version 1.0
// @description This is a real world sample golang Rest API server
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name MIT
// @license.url https://github.com/yhagio/github.com/yhagio/go_api_boilerplate/blob/master/LICENSE
// @host localhost:8090
// @BasePath /
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
func main() {
setting.Setup()
client := database.Connect()
defer client.Close()
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
// bundle.MustLoadMessageFile("i18n/translation/en/*.en.json")
// bundle.MustLoadMessageFile("i18n/translation/fr/*.fr.json")
closeLogFile, err := logger.Setup()
if err != nil {
logger.Fatal(err)
}
defer closeLogFile()
gin.SetMode(setting.ServerSetting.RunMode)
port := os.Getenv("PORT")
if port == "" {
port = "8090"
logger.Warnf("Defaulting to port %s", port)
}
routersInit := router.InitRouter(*client)
readTimeout := setting.ServerSetting.ReadTimeout
writeTimeout := setting.ServerSetting.WriteTimeout
endPoint := fmt.Sprintf(":%s", port)
server := &http.Server{
Addr: endPoint,
Handler: routersInit,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}
logger.Infof("[info] start http server listening %s", endPoint)
server.ListenAndServe()
}