This repository was archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (110 loc) · 2.49 KB
/
Copy pathmain.go
File metadata and controls
125 lines (110 loc) · 2.49 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/mylockerteam/alog"
)
type request struct {
Message string `json:"message"`
}
func main() {
mode := getApplicationMode()
port := getListenPort()
flag.Parse()
gin.SetMode(*mode)
engine := gin.Default()
engine.Use(getCors())
engine.POST("/info", infoHandler)
engine.POST("/warning", warningHandler)
engine.POST("/error", errorHandler)
engine.NoRoute(noRoute)
GetLogger().Error(http.ListenAndServe(fmt.Sprintf(":%s", *port), engine))
}
func errorHandler(c *gin.Context) {
var req request
if err := c.Bind(&req); err != nil {
c.SecureJSON(getResponse400(err))
return
}
GetLogger().Error(errors.New(req.Message))
c.SecureJSON(getResponse201())
return
}
func warningHandler(c *gin.Context) {
var req request
if err := c.Bind(&req); err != nil {
c.SecureJSON(getResponse400(err))
return
}
GetLogger().Warning(req.Message)
c.SecureJSON(getResponse201())
return
}
func infoHandler(c *gin.Context) {
var req request
if err := c.Bind(&req); err != nil {
c.SecureJSON(getResponse400(err))
return
}
GetLogger().Info(req.Message)
c.SecureJSON(getResponse201())
return
}
func getCors() gin.HandlerFunc {
return cors.New(cors.Config{
AllowMethods: []string{
http.MethodPost,
},
AllowHeaders: []string{
"Origin",
"Content-Length",
"Content-Type",
"Authorization",
},
AllowCredentials: false,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
})
}
func noRoute(c *gin.Context) {
c.AbortWithStatusJSON(getResponse404())
return
}
func getResponse404() (int, gin.H) {
return http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"message": http.StatusText(http.StatusNotFound),
}
}
func getResponse400(err error) (int, gin.H) {
return http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"message": http.StatusText(http.StatusBadRequest),
"error": err.Error(),
}
}
func getResponse201() (int, gin.H) {
return http.StatusCreated, gin.H{
"code": http.StatusCreated,
"message": http.StatusText(http.StatusCreated),
}
}
func getListenPort() *string {
port := "80"
if envPort := alog.GetEnvStr("PORT"); envPort != "" {
port = envPort
}
return flag.String("port", port, "Example: -port=8080")
}
func getApplicationMode() *string {
mode := gin.ReleaseMode
if envMode := alog.GetEnvStr("APP_MODE"); envMode != "" {
mode = envMode
}
return flag.String("mode", mode, "Example: -mode=debug")
}