-
Notifications
You must be signed in to change notification settings - Fork 4
/
request_handler.go
42 lines (35 loc) · 1.03 KB
/
request_handler.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
package main
import (
"encoding/json"
"github.com/valyala/fasthttp"
"log"
"reflect"
)
var (
strContentType = []byte("Content-Type")
strApplicationJSON = []byte("application/json")
)
func requestHandler(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if val, ok := respMap[path]; ok {
log.Println(path)
ctx.Response.Header.SetCanonical(strContentType, strApplicationJSON)
statusCode := int(getObject(val, "statusCode").(float64))
ctx.Response.SetStatusCode(statusCode)
if err := json.NewEncoder(ctx).Encode(getObject(val, "responseBody")); err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
} else {
ctx.Error("Path not found", fasthttp.StatusInternalServerError)
}
}
func getObject(i interface{}, fieldName string) interface{} {
return reflect.ValueOf(i).MapIndex(reflect.ValueOf(fieldName)).Interface()
}
func isPostMethod(i interface{}) bool {
method := reflect.ValueOf(i).MapIndex(reflect.ValueOf("method")).Interface().(string)
if method == "POST" {
return true
}
return false
}