-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_handler.go
66 lines (54 loc) · 1.64 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"net"
"reflect"
"regexp"
"github.com/valyala/fasthttp"
)
////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions
type ipResponse struct {
Origin string `json:"origin"`
}
////////////////////////////////////////////////////////////////////////////
// Global variables definitions
var (
strContentType = []byte("Content-Type")
strApplicationJSON = "application/json"
)
////////////////////////////////////////////////////////////////////////////
// Function definitions
func requestHandler(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
for _, k := range respMap {
if path == k.HTTPRequest.Path {
if e.Verbose >= 3 {
h, _, _ := net.SplitHostPort(ctx.RemoteAddr().String())
log.Printf("%s (%s)", path, h)
} else if e.Verbose >= 2 {
log.Println(path)
}
contentType := k.HTTPRequest.Body.ContentType
if contentType == "" {
contentType = strApplicationJSON
} else if regexp.MustCompile(`(?i)application/x-www-form-urlencoded;`).MatchString(contentType) {
contentType = "text/html; charset=UTF-8"
}
ctx.Response.Header.SetCanonical(strContentType, []byte(contentType))
statusCode := k.HTTPResponse.StatusCode
if statusCode == 0 {
statusCode = 200
}
ctx.Response.SetStatusCode(statusCode)
// directly write to body
fmt.Fprintf(ctx, k.HTTPResponse.Body)
return
}
}
ctx.Error("Path not found", fasthttp.StatusInternalServerError)
}
func getObject(i interface{}, fieldName string) interface{} {
return reflect.ValueOf(i).MapIndex(reflect.ValueOf(fieldName)).Interface()
}