-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
48 lines (40 loc) · 871 Bytes
/
main.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
package main
import (
"encoding/json"
"flag"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
type ResultData struct {
Mba string `json:"mba"`
}
type Response struct {
Code int `json:"code"`
Error string `json:"error"`
Warn string `json:"warn"`
Data ResultData `json:"data"`
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.POST("/request", func(c *gin.Context) {
request, _ := ioutil.ReadAll(c.Request.Body)
response, err := json.Marshal(process(string(request)))
if err != nil {
c.String(http.StatusBadGateway, `{"code": 502}`)
} else {
c.String(http.StatusOK, string(response))
}
})
return r
}
func listen() string {
listen := ":10000"
flag.StringVar(&listen, "l", ":10000", "listen address, default is :10000")
flag.Parse()
return listen
}
func main() {
r := setupRouter()
_ = r.Run(listen())
}