-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
56 lines (46 loc) · 1.38 KB
/
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
49
50
51
52
53
54
55
56
package main
import (
"net/http"
"github.com/gdgtoledo/linneo/dao"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"go.elastic.co/apm/module/apmgin"
"go.elastic.co/apm/module/apmlogrus"
)
func init() {
// apmlogrus.Hook will send "error", "panic", and "fatal"
// level log messages to Elastic APM.
log.AddHook(&apmlogrus.Hook{})
}
func searchPlants(c *gin.Context) {
// apmlogrus.TraceContext extracts the transaction and span (if any) from the given context,
// and returns logrus.Fields containing the trace, transaction, and span IDs.
traceContextFields := apmlogrus.TraceContext(c)
log.WithFields(traceContextFields).Debug("handling request")
res, err := dao.Search(c, "plants", map[string]interface{}{})
log.WithFields(log.Fields{
"result": res,
}).Info("Query Result")
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error querying database")
}
hits := res["hits"].(map[string]interface{})["hits"].([]interface{})
if len(hits) == 0 {
c.String(http.StatusNoContent, "There are no plants in the primary storage")
} else {
c.String(http.StatusOK, "YAY! There are %d plants in the primary storage", len(hits))
}
}
func setupRouter() *gin.Engine {
r := gin.Default()
r.Use(apmgin.Middleware(r))
r.GET("/plants", searchPlants)
return r
}
func main() {
r := setupRouter()
// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}