-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
56 lines (50 loc) · 1.7 KB
/
router.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
package main
import (
"github.com/sinhashubham95/go-actuator"
actuatorCore "github.com/sinhashubham95/go-actuator/core"
actuatorModels "github.com/sinhashubham95/go-actuator/models"
"github.com/valyala/fasthttp"
"net/http"
"strings"
"github.com/sinhashubham95/moxy/commons"
"github.com/sinhashubham95/moxy/controllers"
)
func getFastHTTPHandler() fasthttp.RequestHandler {
actuatorHandler := actuator.GetFastHTTPActuatorHandler(&actuatorModels.Config{
Prefix: commons.ActuatorPrefix,
})
return actuatorCore.WrapFastHTTPHandler(func(ctx *fasthttp.RequestCtx) {
// actuator
if strings.HasPrefix(string(ctx.Path()), commons.ActuatorPrefix) && string(ctx.Method()) == http.MethodGet {
// this means it is an actuator endpoint
actuatorHandler(ctx)
return
}
// cors
ctx.Response.Header.Add("Access-Control-Allow-Origin", "*")
ctx.Response.Header.Add("Access-Control-Allow-Credentials", "true")
ctx.Response.Header.Add("Access-Control-Allow-Headers", "*")
ctx.Response.Header.Add("Access-Control-Allow-Methods", "POST,HEAD,PATCH,OPTIONS,GET,PUT")
if strings.ToUpper(string(ctx.Method())) == http.MethodOptions {
ctx.SetStatusCode(http.StatusNoContent)
return
}
switch string(ctx.Path()) {
case commons.BasePath:
handle(ctx, http.MethodGet, controllers.HandleBase)
case commons.MockEndpointPath:
handle(ctx, http.MethodPost, controllers.HandleMock)
case commons.UnMockEndpointPath:
handle(ctx, http.MethodDelete, controllers.HandleUnMock)
default:
controllers.Handle(ctx)
}
})
}
func handle(ctx *fasthttp.RequestCtx, method string, handler fasthttp.RequestHandler) {
if string(ctx.Method()) == method {
handler(ctx)
return
}
ctx.SetStatusCode(http.StatusNotFound)
}