-
Notifications
You must be signed in to change notification settings - Fork 9
/
routes.go
65 lines (56 loc) · 1.85 KB
/
routes.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
package fedbox
import (
"net/http"
"git.sr.ht/~mariusor/lw"
"github.com/go-ap/errors"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func (f FedBOX) Routes() func(chi.Router) {
return func(r chi.Router) {
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(CleanRequestPath)
r.Use(SetCORSHeaders)
r.Use(lw.Middlewares(f.logger)...)
r.Method(http.MethodGet, "/", HandleItem(f))
r.Method(http.MethodHead, "/", HandleItem(f))
// TODO(marius): we can separate here the FedBOX specific collections from the ActivityPub spec ones
// using some regular expressions
// Eg: "/{collection:(inbox|outbox|followed)}"
// Eg: "/{collection:(activities|objects|actors|moderators|ignored|blocked|flagged)}"
r.Route("/{collection}", f.CollectionRoutes(true))
if f.conf.Env.IsDev() {
r.Mount("/debug", middleware.Profiler())
}
r.Handle("/favicon.ico", errors.NotFound)
r.NotFound(errors.NotFound.ServeHTTP)
r.MethodNotAllowed(errors.HandleError(errors.MethodNotAllowedf("method not allowed")).ServeHTTP)
}
}
func (f FedBOX) CollectionRoutes(descend bool) func(chi.Router) {
return func(r chi.Router) {
r.Group(func(r chi.Router) {
r.Method(http.MethodGet, "/", HandleCollection(f))
r.Method(http.MethodHead, "/", HandleCollection(f))
r.Method(http.MethodPost, "/", HandleActivity(f))
r.Route("/{id}", func(r chi.Router) {
r.Method(http.MethodGet, "/", HandleItem(f))
r.Method(http.MethodHead, "/", HandleItem(f))
if descend {
r.Route("/{collection}", f.CollectionRoutes(false))
}
})
})
}
}
func SetCORSHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}