This repository has been archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
121 lines (110 loc) · 3.09 KB
/
handlers.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
115
116
117
118
119
120
121
package server
import (
"encoding/json"
"os"
"github.com/jmoiron/sqlx"
"github.com/sebach1/rtc/config"
"github.com/sebach1/rtc/git"
"github.com/sebach1/rtc/internal/name"
"github.com/valyala/fasthttp"
)
// Router is a handler which redirects to handlers
func Router(reqCtx *fasthttp.RequestCtx) {
reqCtx.SetContentType("application/json")
db := databaseHandler(reqCtx)
switch string(reqCtx.Path()) {
case "/add":
addHandler(reqCtx, db)
case "/rm":
rmHandler(reqCtx, db)
case "/commit":
commitHandler(reqCtx, db)
case "/orchestrate":
orchestrateHandler(reqCtx, db)
default:
reqCtx.NotFound()
}
}
func databaseHandler(reqCtx *fasthttp.RequestCtx) *sqlx.DB {
dataSource := os.Getenv("db")
if dataSource == "" {
dataSource = config.DefaultDBSrc
}
db, err := sqlx.Open("postgres", dataSource)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusBadGateway)
}
db.MapperFunc(name.ToSnakeCase)
return db
}
func encoderHandler(reqCtx *fasthttp.RequestCtx, v interface{}) {
err := json.NewEncoder(reqCtx).Encode(v)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusUnprocessableEntity)
}
}
func decoderHandler(reqCtx *fasthttp.RequestCtx, validator func(body *reqBody) error) *reqBody {
body := &reqBody{}
rawBody := reqCtx.PostBody()
err := json.Unmarshal(rawBody, body)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusPreconditionFailed)
}
err = validator(body)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusUnprocessableEntity)
}
return body
}
func addHandler(reqCtx *fasthttp.RequestCtx, db *sqlx.DB) {
body := decoderHandler(reqCtx, validateAdd)
var err error
respBody := &respBody{}
respBody.Change, err = git.Add(reqCtx, db,
body.Entity, body.Table, body.Column, body.Branch, body.Value, body.Type, body.Opts,
)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusBadRequest)
}
reqCtx.SetStatusCode(fasthttp.StatusAccepted)
encoderHandler(reqCtx, respBody)
}
func rmHandler(reqCtx *fasthttp.RequestCtx, db *sqlx.DB) {
body := decoderHandler(reqCtx, validateRm)
var err error
respBody := &respBody{}
respBody.Change, err = git.Rm(reqCtx, db,
body.Entity, body.Table, body.Column, body.Branch, body.Value, body.Type, body.Opts,
)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusBadRequest)
}
reqCtx.SetStatusCode(fasthttp.StatusAccepted)
encoderHandler(reqCtx, respBody)
}
func commitHandler(reqCtx *fasthttp.RequestCtx, db *sqlx.DB) {
reqBody := decoderHandler(reqCtx, validateCommit)
respBody := &respBody{}
var err error
respBody.Commits, err = git.Comm(reqCtx, db,
reqBody.Branch,
)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusBadRequest)
}
reqCtx.SetStatusCode(fasthttp.StatusAccepted)
encoderHandler(reqCtx, respBody)
}
func orchestrateHandler(reqCtx *fasthttp.RequestCtx, db *sqlx.DB) {
reqBody := decoderHandler(reqCtx, validateRm)
respBody := &respBody{}
var err error
respBody.Commits, err = git.Comm(reqCtx, db,
reqBody.Branch,
)
if err != nil {
reqCtx.Error(err.Error(), fasthttp.StatusBadRequest)
}
reqCtx.SetStatusCode(fasthttp.StatusAccepted)
encoderHandler(reqCtx, respBody)
}