forked from looterz/grimd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
159 lines (136 loc) · 4.46 KB
/
api.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"net"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gopkg.in/gin-contrib/cors.v1"
)
// StartAPIServer starts the API server
func StartAPIServer(config *Config,
reloadChan chan bool,
blockCache *MemoryBlockCache,
questionCache *MemoryQuestionCache) (*http.Server, error) {
if !config.APIDebug {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
server := &http.Server{
Addr: config.API,
Handler: router,
}
router.Use(cors.Default())
router.GET("/blockcache", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"length": blockCache.Length(), "items": blockCache.Backend})
})
router.GET("/blockcache/exists/:key", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"exists": blockCache.Exists(c.Param("key"))})
})
router.GET("/blockcache/get/:key", func(c *gin.Context) {
if ok, _ := blockCache.Get(c.Param("key")); !ok {
c.IndentedJSON(http.StatusOK, gin.H{"error": c.Param("key") + " not found"})
} else {
c.IndentedJSON(http.StatusOK, gin.H{"success": ok})
}
})
router.GET("/blockcache/length", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"length": blockCache.Length()})
})
router.GET("/blockcache/remove/:key", func(c *gin.Context) {
// Removes from BlockCache only. If the domain has already been queried and placed into MemoryCache, will need to wait until item is expired.
blockCache.Remove(c.Param("key"))
c.IndentedJSON(http.StatusOK, gin.H{"success": true})
})
router.GET("/blockcache/set/:key", func(c *gin.Context) {
// MemoryBlockCache Set() always returns nil, so ignoring response.
_ = blockCache.Set(c.Param("key"), true)
c.IndentedJSON(http.StatusOK, gin.H{"success": true})
})
router.GET("/questioncache", func(c *gin.Context) {
highWater, err := strconv.ParseInt(c.DefaultQuery("highWater", "-1"), 10, 64)
if err != nil {
highWater = -1
}
c.IndentedJSON(http.StatusOK, gin.H{
"length": questionCache.Length(),
"items": questionCache.GetOlder(highWater),
})
})
router.GET("/questioncache/length", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"length": questionCache.Length()})
})
router.GET("/questioncache/clear", func(c *gin.Context) {
questionCache.Clear()
c.IndentedJSON(http.StatusOK, gin.H{"success": true})
})
router.GET("/questioncache/client/:client", func(c *gin.Context) {
var filteredCache []QuestionCacheEntry
questionCache.mu.RLock()
for _, entry := range questionCache.Backend {
if entry.Remote == c.Param("client") {
filteredCache = append(filteredCache, entry)
}
}
questionCache.mu.RUnlock()
c.IndentedJSON(http.StatusOK, filteredCache)
})
router.OPTIONS("/application/active", func(c *gin.Context) {
c.AbortWithStatus(http.StatusOK)
})
router.GET("/application/active", func(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"active": grimdActive})
})
// Handle the setting of active state.
// Possible values for state:
// On
// Off
// Snooze: off for `timeout` seconds; timeout defaults to 300
router.PUT("/application/active", func(c *gin.Context) {
active := c.Query("state")
version := c.Query("v")
if version != "1" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "Illegal value for 'version'"})
} else {
switch active {
case "On":
grimdActivation.set(true)
c.IndentedJSON(http.StatusOK, gin.H{"active": grimdActive})
case "Off":
grimdActivation.set(false)
c.IndentedJSON(http.StatusOK, gin.H{"active": grimdActive})
case "Snooze":
timeoutString := c.DefaultQuery("timeout", "300")
timeout, err := strconv.ParseUint(timeoutString, 0, 0)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Illegal value for 'timeout'"})
} else {
grimdActivation.toggleOff(uint(timeout))
c.IndentedJSON(http.StatusOK, gin.H{
"active": grimdActive,
"timeout": timeout,
})
}
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "Illegal value for 'state'"})
}
}
})
router.POST("/blocklist/update", func(c *gin.Context) {
c.AbortWithStatus(http.StatusOK)
// Send reload trigger to chan in background goroutine so does not hang
go func(reloadChan chan bool) {
reloadChan <- true
}(reloadChan)
})
listener, err := net.Listen("tcp", config.API)
if err != nil {
return nil, err
}
go func() {
if err := server.Serve(listener); err != http.ErrServerClosed {
logger.Fatal(err)
}
}()
logger.Criticalf("API server listening on %s", config.API)
return server, err
}