-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreator.go
129 lines (110 loc) · 2.75 KB
/
creator.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
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
126
127
128
129
package tyrgin
import (
"fmt"
"log"
"os"
"github.com/appleboy/gin-jwt"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
godotenv "github.com/joho/godotenv"
)
// MongoTyrRSStatusEndpoint is for healthcheck api to know about mongo replica sets.
var MongoTyrRSStatusEndpoint StatusEndpoint
func init() {
env := os.Getenv("ENV")
if env == "" {
os.Setenv("ENV", "dev")
env = "dev"
}
if env == "dev" {
if err := godotenv.Load(); err != nil {
log.Fatal("Could not load .env file.")
}
}
checkSession, err := GetMongoDB(os.Getenv("DB_NAME"))
if err != nil {
log.Println("Could not get Mongo connection")
}
MongoTyrRSStatusEndpoint = StatusEndpoint{
Name: "Mongo Tyr Replica Set Check",
Slug: "mongo",
Type: "internal",
IsTraversable: false,
StatusCheck: MongoRPLStatusChecker{
RPL: checkSession,
},
TraverseCheck: nil,
}
}
// action takes the APIAction method and creates a gin route of that type.
// Also makes the route private if it labeled as private in the apiaction.
func (a *APIAction) action(route *gin.RouterGroup) {
switch a.Method {
case GET:
route.GET(a.Route, a.Func)
break
case DELETE:
route.DELETE(a.Route, a.Func)
break
case PATCH:
route.PATCH(a.Route, a.Func)
break
case POST:
route.POST(a.Route, a.Func)
break
case PUT:
route.PUT(a.Route, a.Func)
break
}
}
// AddRoutes takes a gin server, gin jwt instance, version number as a string,
// api endpoint name and a list of APIActions to add to it.
func AddRoutes(router *gin.Engine, private bool, jwt *jwt.GinJWTMiddleware, version, api string, fns []APIAction) {
ver := router.Group("/api/v" + version)
{
route := ver.Group(api)
if private {
route.Use(jwt.MiddlewareFunc())
}
{
for _, fn := range fns {
fn.action(route)
}
}
}
}
// NotFound a general 404 error message.
func NotFound(c *gin.Context) {
fmt.Println(c.Request.URL.Path[1:])
c.File("./static/index.html")
}
// SetupRouter returns an instance to a *gin.Enginer that is has
// some preconfigurations already set up.
func SetupRouter() *gin.Engine {
router := gin.Default()
router.Use(Logger())
router.Use(gin.Recovery())
router.GET(
"/status/:slug",
HealthPointHandler(
[]StatusEndpoint{
MongoTyrRSStatusEndpoint,
},
"./about.json",
"./version.txt",
make(map[string]interface{}),
),
)
return router
}
// ServeReact is a function to serve react from a(n) service.
func ServeReact(r *gin.Engine) {
r.Use(static.Serve("/", static.LocalFile("./static", true)))
}
// ErrorHandler handles gin errors in a more clean way
func ErrorHandler(err error, c *gin.Context, sc int, json interface{}) {
c.Writer.Header().Add("Content-Type", "application/json+error")
c.AbortWithStatusJSON(sc, json)
c.Error(err)
return
}