-
Notifications
You must be signed in to change notification settings - Fork 199
/
api.go
52 lines (40 loc) · 1.17 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
package api
import (
"net/http"
"github.com/ElrondNetwork/elrond-go-core/marshal"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/api/logs"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
var log = logger.GetOrCreate("seednode/api")
// Start will boot up the api and appropriate routes, handlers and validators
func Start(restApiInterface string, marshalizer marshal.Marshalizer) error {
ws := gin.Default()
ws.Use(cors.Default())
registerRoutes(ws, marshalizer)
return ws.Run(restApiInterface)
}
func registerRoutes(ws *gin.Engine, marshalizer marshal.Marshalizer) {
registerLoggerWsRoute(ws, marshalizer)
}
func registerLoggerWsRoute(ws *gin.Engine, marshalizer marshal.Marshalizer) {
upgrader := websocket.Upgrader{}
ws.GET("/log", func(c *gin.Context) {
upgrader.CheckOrigin = func(r *http.Request) bool {
return true
}
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Error(err.Error())
return
}
ls, err := logs.NewLogSender(marshalizer, conn, log)
if err != nil {
log.Error(err.Error())
return
}
ls.StartSendingBlocking()
})
}