-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinghandler.go
68 lines (63 loc) · 1.38 KB
/
pinghandler.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
package main
import (
"net/http"
"github.com/asendia/salmonping/db"
"github.com/gin-gonic/gin"
)
// pingHandler godoc
//
// @Summary Ping and scrape online listings
// @Description this endpoint is called by cloud scheduler
// @Tags ping
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} DefaultResponse
// @Failure 401 {object} DefaultErrorResponse
// @Failure 500 {object} DefaultErrorResponse
// @Router /ping [get]
func pingHandler(c *gin.Context) {
ctx := c.Request.Context()
tx, conn, _, message, err := prepareDBConn(ctx)
if conn != nil {
defer conn.Close(ctx)
}
if tx != nil {
// Commit everything
defer tx.Commit(ctx)
}
if err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: message,
}
logJson(log.JSON())
c.JSON(http.StatusInternalServerError, log)
return
}
queries := db.New(tx)
err = fetchListings(ctx, queries)
if err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: "Error fetching listings",
}
logJson(log.JSON())
c.JSON(http.StatusInternalServerError, log)
return
}
err = sendAlerts(ctx, queries)
if err != nil {
log := DefaultErrorResponse{
Error: err.Error(),
Level: "error",
Message: "Error sending alerts",
}
logJson(log.JSON())
}
c.JSON(http.StatusOK, DefaultResponse{
Message: "ok",
})
}