-
Notifications
You must be signed in to change notification settings - Fork 27
/
router.go
54 lines (42 loc) · 1.44 KB
/
router.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
package server
import (
"net/http"
"github.com/src-d/gitbase-web/server/handler"
"github.com/src-d/gitbase-web/server/service"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pressly/lg"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
)
// Router returns a Handler to serve the backend
func Router(
logger *logrus.Logger,
static *handler.Static,
version string,
db service.SQLDB,
bbblfshServerURL string,
) http.Handler {
// cors options
corsOptions := cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "OPTIONS"},
AllowedHeaders: []string{"Location", "Authorization", "Content-Type"},
AllowCredentials: true,
}
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(cors.New(corsOptions).Handler)
r.Use(lg.RequestLogger(logger))
r.Post("/query", handler.APIHandlerFunc(handler.Query(db)))
r.Get("/schema", handler.APIHandlerFunc(handler.Schema(db)))
r.Get("/export", handler.Export(db))
r.Post("/parse", handler.APIHandlerFunc(handler.Parse(bbblfshServerURL)))
r.Post("/filter", handler.APIHandlerFunc(handler.Filter()))
r.Post("/detect-lang", handler.APIHandlerFunc(handler.DetectLanguage()))
r.Get("/get-languages", handler.APIHandlerFunc(handler.GetLanguages(bbblfshServerURL)))
r.Get("/version", handler.APIHandlerFunc(handler.Version(version, bbblfshServerURL, db)))
r.Get("/static/*", static.ServeHTTP)
r.Get("/*", static.ServeHTTP)
return r
}