-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (56 loc) · 1.66 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
57
58
59
60
61
62
63
64
65
66
package main
import (
"time"
"github.com/gin-gonic/gin"
"github.com/inverse-inc/packetfence/go/log"
"github.com/inverse-inc/packetfence/go/sharedutils"
"github.com/jcuga/golongpoll"
)
const defaultPollTimeout = 30 * time.Second
const maxSessionIdleTime = 5 * time.Minute
const LONG_POLL_CONTEXT_KEY = "GLP-GIN-MIDDLEWARE"
func makeGinServer() *gin.Engine {
r := gin.Default()
r.Use(longPollMiddleware())
r.GET("/profile/:node_id", handleGetProfile)
r.GET("/peer/:node_id", handleGetPeer)
r.GET("/events", handleGetEvents)
r.GET("/events/:k", handleGetEventsKey)
r.POST("/events/:k", handlePostEvents)
return r
}
func renderError(c *gin.Context, code int, err error) {
renderErrors(c, code, []error{err})
}
func renderErrors(c *gin.Context, code int, errs []error) {
strErrs := []string{}
for _, err := range errs {
log.LoggerWContext(c).Error("Got the following error while processing the request: " + err.Error())
strErrs = append(strErrs, err.Error())
}
c.JSON(code, gin.H{"errors": strErrs})
}
func longPollFromContext(c *gin.Context) *golongpoll.LongpollManager {
if v, ok := c.Get(LONG_POLL_CONTEXT_KEY); ok {
return v.(*golongpoll.LongpollManager)
} else {
return nil
}
}
func longPollMiddleware() gin.HandlerFunc {
pubsub, err := golongpoll.StartLongpoll(golongpoll.Options{
LoggingEnabled: (sharedutils.EnvOrDefault("LOG_LEVEL", "") == "debug"),
MaxEventBufferSize: 1000,
// Events stay for up to 5 minutes
EventTimeToLiveSeconds: int(maxSessionIdleTime / time.Second),
})
sharedutils.CheckError(err)
return func(c *gin.Context) {
c.Set(LONG_POLL_CONTEXT_KEY, pubsub)
c.Next()
}
}
func main() {
r := makeGinServer()
r.Run()
}