/
handler.go
60 lines (52 loc) · 1.3 KB
/
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
package api
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// Response is
type Response struct {
Code int
Message interface{}
}
type Handler interface {
ExecSearch(c *gin.Context) (*Response, Error)
GetSearch(c *gin.Context) (*Response, Error)
GetSearchLogs(c *gin.Context) (*Response, Error)
GetSearchTimeSeries(c *gin.Context) (*Response, Error)
}
type MinervaHandler struct {
DatabaseName string
IndexTableName string
MessageTableName string
OutputPath string
MetaTableName string
Region string
}
func (x *MinervaHandler) newSearchRepo() searchRepository {
return newSearchRepoDynamoDB(x.Region, x.MetaTableName)
}
// Handler is handler interface
func sendResponse(c *gin.Context, resp *Response, err Error) {
var code int
if resp != nil {
code = resp.Code
}
Logger.WithFields(logrus.Fields{
"path": c.FullPath(),
"request_id": c.GetHeader("x-request-id"),
"ipaddr": c.ClientIP(),
"user_agent": c.Request.UserAgent(),
"resp_code": code,
"error": err,
}).Info("Audit log")
if err != nil {
Logger.WithFields(logrus.Fields{
"error": err,
"params": c.Params,
"url": c.Request.URL,
}).Error("Request faield")
c.JSON(err.Code(), gin.H{"message": err.Message()})
} else {
c.JSON(resp.Code, resp.Message)
}
}