-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
114 lines (93 loc) · 3.37 KB
/
controller.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
// Package controller implements the Radar API controller.
package controller
/* Copyright (C) 2017-2018 Radar team (see AUTHORS)
This file is part of radar.
radar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
radar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with radar. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"fmt"
"github.com/buaazp/fasthttprouter"
"github.com/golang/glog"
"github.com/valyala/fasthttp"
"github.com/radar-go/radar/datastore"
)
// Controller struct to manager the Radar API Controller.
type Controller struct {
Router *fasthttprouter.Router
}
// New creates and return a new Controller object.
func New() *Controller {
c := &Controller{
Router: fasthttprouter.New(),
}
c.register()
return c
}
// logPaths logs the requested path to the info log.
func logPath(path []byte) {
glog.Infof("Request path: %s", path)
}
// register defines all the router paths the API implements.
func (c *Controller) register() {
c.Router.HandleMethodNotAllowed = true
c.Router.NotFound = c.notFound
c.Router.MethodNotAllowed = c.methodNotAllowed
c.Router.PanicHandler = c.panic
c.Router.GET("/healthcheck", c.healthcheck)
ds := datastore.New()
endpoints := ds.Endpoints()
for key := range endpoints {
c.Router.POST(key, c.apiHandler)
}
}
// panic handles when the server have a fatal error.
func (c *Controller) panic(ctx *fasthttp.RequestCtx, from interface{}) {
logPath(ctx.Path())
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetBodyString(fmt.Sprintf(`{"error": "API fatal error calling %s"}`,
ctx.Path()))
}
// methodNotAllowed handles the response when a method call is not allowed from
// the client.
func (c *Controller) methodNotAllowed(ctx *fasthttp.RequestCtx) {
logPath(ctx.Path())
ctx.SetStatusCode(fasthttp.StatusMethodNotAllowed)
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetBodyString(fmt.Sprintf(`{"error": "Method not allowed calling %s"}`,
ctx.Path()))
}
// notFound handles the response when a path have not been found.
func (c *Controller) notFound(ctx *fasthttp.RequestCtx) {
logPath(ctx.Path())
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetBodyString(fmt.Sprintf(`{"error": "Path %s not found"}`,
ctx.Path()))
}
// healthcheck handler.
func (c *Controller) healthcheck(ctx *fasthttp.RequestCtx) {
logPath(ctx.Path())
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetContentType("application/json; charset=utf-8")
ctx.SetBodyString(`{"status": "ok"}`)
}
// internalServerError response
func internalServerError(ctx *fasthttp.RequestCtx, msg string) {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetBodyString(fmt.Sprintf(`{"error":"%s"}`, msg))
}
// badRequest response
func badRequest(ctx *fasthttp.RequestCtx, msg string) {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
ctx.SetBodyString(fmt.Sprintf(`{"error":"%s"}`, msg))
}