-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
57 lines (44 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
53
54
55
56
57
package neocortex
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type API struct {
e *gin.Engine
Port string
repository Repository
prefix string
analytics *Analytics
}
func newCortexAPI(repo Repository, analytics *Analytics, prefix, port string) *API {
return &API{
e: gin.Default(),
Port: port,
prefix: prefix,
repository: repo,
analytics: analytics,
}
}
func (api *API) registerEndpoints(engine *Engine) {
corsConf := cors.DefaultConfig()
corsConf.AddAllowHeaders("Authorization")
corsConf.AllowAllOrigins = true
c := cors.New(corsConf)
api.e.Use(c)
authJWTMiddleware := getJWTAuth(engine, engine.secret)
api.e.POST("/login", authJWTMiddleware.LoginHandler)
api.e.GET("/token_refresh", authJWTMiddleware.RefreshHandler)
api.e.Use(authJWTMiddleware.MiddlewareFunc())
r := api.e.Group(api.prefix)
api.registerDialogsAPI(r)
api.registerViewsAPI(r)
api.registerActionsAPI(r)
api.registerCollectionsAPI(r)
api.registerSummaryAPI(r)
api.registerChatsAPI(r)
api.registerDownloadsAPI(r)
}
func (api *API) Launch(engine *Engine) error {
api.registerEndpoints(engine)
return api.e.Run(api.Port)
}