-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
42 lines (32 loc) · 1.04 KB
/
app.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
package api
import (
"net/http"
"github.com/alexedwards/flow"
"github.com/theandrew168/bloggulus/backend/storage"
"github.com/theandrew168/bloggulus/backend/web/api/admin"
"github.com/theandrew168/bloggulus/backend/web/api/reader"
"github.com/theandrew168/bloggulus/backend/web/api/util"
"github.com/theandrew168/bloggulus/backend/web/middleware"
)
type Application struct {
store *storage.Storage
}
func NewApplication(store *storage.Storage) *Application {
app := Application{
store: store,
}
return &app
}
func (app *Application) Router() http.Handler {
adminApp := admin.NewApplication(app.store)
readerApp := reader.NewApplication(app.store)
mux := flow.New()
mux.NotFound = http.HandlerFunc(util.NotFoundResponse)
mux.MethodNotAllowed = http.HandlerFunc(util.MethodNotAllowedResponse)
mux.Use(middleware.SecureHeaders)
mux.Use(middleware.EnableCORS)
mux.HandleFunc("/", app.handleIndex(), "GET")
mux.Handle("/admin/...", http.StripPrefix("/admin", adminApp.Router()))
mux.Handle("/...", readerApp.Router())
return mux
}