-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouting.go
More file actions
110 lines (89 loc) · 2.68 KB
/
routing.go
File metadata and controls
110 lines (89 loc) · 2.68 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/RSOI/question/controller"
"github.com/RSOI/question/ui"
"github.com/RSOI/question/utils"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
func sendResponse(ctx *fasthttp.RequestCtx, r ui.Response, nolog ...bool) {
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Response.SetStatusCode(r.Status)
utils.LOG(fmt.Sprintf("Sending response. Status: %d", r.Status))
doLog := true
if len(nolog) > 0 {
doLog = !nolog[0]
}
if doLog {
controller.LogStat(ctx.Path(), r.Status, r.Error)
}
content, _ := json.Marshal(r)
ctx.Write(content)
}
func indexGET(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Get service stats (%s)", ctx.Path()))
var err error
var r ui.Response
r.Data, err = controller.IndexGET(ctx.Host())
r.Status, r.Error = ui.ErrToResponse(err)
nolog := true
sendResponse(ctx, r, nolog)
}
func askPUT(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Ask new question (%s)", ctx.Path()))
var err error
var r ui.Response
r.Data, err = controller.AskPUT(ctx.PostBody())
r.Status, r.Error = ui.ErrToResponse(err)
if r.Status == 200 {
r.Status = 201 // REST :)
}
sendResponse(ctx, r)
}
func questionGET(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Get one question (%s)", ctx.Path()))
var err error
var r ui.Response
id := ctx.UserValue("id").(string)
r.Data, err = controller.QuestionGET(id)
r.Status, r.Error = ui.ErrToResponse(err)
sendResponse(ctx, r)
}
func questionsGET(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Get questions by author (%s)", ctx.Path()))
var err error
var r ui.Response
aid := ctx.UserValue("authorid").(string)
r.Data, err = controller.QuestionsGET(aid)
r.Status, r.Error = ui.ErrToResponse(err)
sendResponse(ctx, r)
}
func updatePATCH(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Update question (%s)", ctx.Path()))
var err error
var r ui.Response
r.Data, err = controller.UpdatePATCH(ctx.PostBody())
r.Status, r.Error = ui.ErrToResponse(err)
sendResponse(ctx, r)
}
func removeDELETE(ctx *fasthttp.RequestCtx) {
utils.LOG(fmt.Sprintf("Request: Delete question (%s)", ctx.Path()))
var err error
var r ui.Response
err = controller.RemoveDELETE(ctx.PostBody())
r.Status, r.Error = ui.ErrToResponse(err)
sendResponse(ctx, r)
}
func initRoutes() *fasthttprouter.Router {
utils.LOG("Setup router...")
router := fasthttprouter.New()
router.GET("/", indexGET)
router.PUT("/ask", askPUT)
router.GET("/question/id:id", questionGET)
router.GET("/questions/author:authorid", questionsGET)
router.PATCH("/update", updatePATCH)
router.DELETE("/delete", removeDELETE)
return router
}