forked from ito-org/api-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
93 lines (77 loc) · 2.1 KB
/
server.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
package main
import (
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
"github.com/ito-org/go-backend/tcn"
)
const (
requestBodyReadError = "Failed to read request body"
invalidRequestError = "Invalid request"
reportVerificationError = "Failed to verify report"
)
// GetRouter returns the Gin router.
func GetRouter(port string, dbConnection *DBConnection) *gin.Engine {
h := &TCNReportHandler{
dbConn: dbConnection,
}
r := gin.Default()
r.POST("/tcnreport", h.postTCNReport)
r.GET("/tcnreport", h.getTCNReport)
return r
}
// TCNReportHandler implements the handler functions for the API endpoints.
// It also holds the database connection that's used by the handler functions.
type TCNReportHandler struct {
dbConn *DBConnection
}
func (h *TCNReportHandler) postTCNReport(c *gin.Context) {
body := c.Request.Body
data, err := ioutil.ReadAll(body)
if err != nil {
c.String(http.StatusBadRequest, requestBodyReadError)
return
}
signedReport, err := tcn.GetSignedReport(data)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
// If the memo field doesn't exist or the memo type is not ito's code, we
// simply ignore the request.
if signedReport.Report.Memo == nil || signedReport.Report.Memo.Type != 0x2 {
c.String(http.StatusBadRequest, invalidRequestError)
return
}
ok, err := signedReport.Verify()
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
if !ok {
c.String(http.StatusBadRequest, reportVerificationError)
return
}
if err := h.dbConn.insertSignedReport(signedReport); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Status(http.StatusOK)
}
func (h *TCNReportHandler) getTCNReport(c *gin.Context) {
signedReports, err := h.dbConn.getSignedReports()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
data := []byte{}
for _, sr := range signedReports {
b, err := sr.Bytes()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
data = append(data, b...)
}
c.Data(http.StatusOK, "application/octet-stream", data)
}