-
Notifications
You must be signed in to change notification settings - Fork 26
/
router.go
113 lines (85 loc) 路 3.46 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
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
package server
import (
"net/http"
"github.com/src-d/code-annotation/server/dbutil"
"github.com/src-d/code-annotation/server/handler"
"github.com/src-d/code-annotation/server/model"
"github.com/src-d/code-annotation/server/repository"
"github.com/src-d/code-annotation/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 code-anotation backend
func Router(
logger *logrus.Logger,
jwt *service.JWT,
oauth *service.OAuth,
diffService *service.Diff,
static *handler.Static,
dbWrapper *dbutil.DB,
exportsPath string,
version string,
) http.Handler {
db := dbWrapper.SQLDB()
// create repos
userRepo := repository.NewUsers(db)
experimentRepo := repository.NewExperiments(db)
assignmentRepo := repository.NewAssignments(db)
filePairRepo := repository.NewFilePairs(db)
featureRepo := repository.NewFeatures(db)
// cors options
corsOptions := cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "OPTIONS"},
AllowedHeaders: []string{"Location", "Authorization", "Content-Type"},
AllowCredentials: true,
}
requesterACL := service.NewACL(userRepo, model.Requester)
export := handler.NewExport(dbWrapper, exportsPath)
r := chi.NewRouter()
r.Use(middleware.Recoverer)
r.Use(cors.New(corsOptions).Handler)
r.Use(lg.RequestLogger(logger))
r.Get("/login", handler.Login(oauth))
r.Get("/api/auth", handler.APIHandlerFunc(handler.OAuthCallback(oauth, jwt, userRepo, logger)))
r.Route("/api", func(r chi.Router) {
r.Use(jwt.Middleware)
r.Get("/me", handler.APIHandlerFunc(handler.Me(userRepo)))
r.Get("/experiments", handler.APIHandlerFunc(handler.GetExperiments(experimentRepo, assignmentRepo)))
r.With(requesterACL.Middleware).
Post("/experiments", handler.APIHandlerFunc(handler.CreateExperiment(experimentRepo)))
r.Route("/experiments/{experimentId}", func(r chi.Router) {
r.Get("/", handler.APIHandlerFunc(handler.GetExperimentDetails(experimentRepo, assignmentRepo)))
r.With(requesterACL.Middleware).
Put("/", handler.APIHandlerFunc(handler.UpdateExperiment(experimentRepo, assignmentRepo)))
r.Route("/assignments", func(r chi.Router) {
r.Get("/", handler.APIHandlerFunc(handler.GetAssignmentsForUserExperiment(assignmentRepo)))
r.Put("/{assignmentId}", handler.APIHandlerFunc(handler.SaveAssignment(assignmentRepo)))
})
r.Route("/file-pairs", func(r chi.Router) {
r.Use(requesterACL.Middleware)
r.Get("/", handler.APIHandlerFunc(handler.GetFilePairs(filePairRepo)))
r.Post("/", handler.APIHandlerFunc(handler.UploadFilePairs(dbWrapper)))
r.Get("/{pairId}/annotations", handler.APIHandlerFunc(handler.GetFilePairAnnotations(assignmentRepo)))
})
r.Get("/file-pairs/{pairId}", handler.APIHandlerFunc(handler.GetFilePairDetails(filePairRepo, diffService)))
})
r.Route("/file-pair", func(r chi.Router) {
r.Use(requesterACL.Middleware)
r.Get("/{pairId}/features", handler.APIHandlerFunc(handler.GetFeatures(filePairRepo, featureRepo)))
})
r.Route("/exports", func(r chi.Router) {
r.Use(requesterACL.Middleware)
r.Get("/", handler.APIHandlerFunc(export.List))
r.Post("/", handler.APIHandlerFunc(export.Create))
r.Get("/{filename}/download", export.Download)
})
})
r.Get("/version", handler.APIHandlerFunc(handler.Version(version)))
r.Get("/static/*", static.ServeHTTP)
r.Get("/*", static.ServeHTTP)
return r
}