1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/web/web.go

97 lines
2.7 KiB
Go
Raw Normal View History

// Package web contains web handlers and initialization stuff.
package web
import (
2022-05-18 18:03:36 +00:00
"github.com/bouncepaw/mycorrhiza/admin"
2022-04-02 16:58:57 +00:00
"github.com/bouncepaw/mycorrhiza/backlinks"
"github.com/bouncepaw/mycorrhiza/categories"
2022-04-09 08:28:57 +00:00
"github.com/bouncepaw/mycorrhiza/help"
2022-05-18 17:30:24 +00:00
"github.com/bouncepaw/mycorrhiza/history/histweb"
"github.com/bouncepaw/mycorrhiza/hypview"
"github.com/bouncepaw/mycorrhiza/interwiki"
"github.com/bouncepaw/mycorrhiza/misc"
"io"
"net/http"
"net/url"
"github.com/gorilla/mux"
2021-05-11 10:14:00 +00:00
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/user"
2021-05-11 10:14:00 +00:00
"github.com/bouncepaw/mycorrhiza/util"
)
2021-10-01 17:34:56 +00:00
// Handler initializes and returns the HTTP router based on the configuration.
func Handler() http.Handler {
2021-07-15 18:14:05 +00:00
router := mux.NewRouter()
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
w.Header().Add("Content-Security-Policy",
"default-src 'self' telegram.org *.telegram.org; "+
"img-src * data:; media-src *; style-src *; font-src * data:")
next.ServeHTTP(w, rq)
})
})
router.StrictSlash(true)
// Public routes. They're always accessible regardless of the user status.
2021-07-15 18:14:05 +00:00
initAuth(router)
// Wiki routes. They may be locked or restricted.
2021-07-15 18:14:05 +00:00
wikiRouter := router.PathPrefix("").Subrouter()
wikiRouter.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
user := user.FromRequest(rq)
if !user.ShowLockMaybe(w, rq) {
next.ServeHTTP(w, rq)
}
})
})
2021-07-15 18:14:05 +00:00
initReaders(wikiRouter)
initMutators(wikiRouter)
2022-04-09 08:28:57 +00:00
help.InitHandlers(wikiRouter)
2022-04-02 16:58:57 +00:00
backlinks.InitHandlers(wikiRouter)
2022-04-01 22:01:54 +00:00
categories.InitHandlers(wikiRouter)
misc.InitHandlers(wikiRouter)
hypview.Init()
2022-05-18 17:30:24 +00:00
histweb.InitHandlers(wikiRouter)
interwiki.InitHandlers(wikiRouter)
// Admin routes.
if cfg.UseAuth {
adminRouter := wikiRouter.PathPrefix("/admin").Subrouter()
adminRouter.Use(groupMiddleware("admin"))
initAdmin(adminRouter)
2022-05-18 18:03:36 +00:00
admin.Init(adminRouter)
}
// Index page
2021-07-15 18:14:05 +00:00
wikiRouter.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
// Let's pray it never fails
addr, _ := url.Parse("/hypha/" + cfg.HomeHypha)
2021-07-15 18:14:05 +00:00
rq.URL = addr
handlerHypha(w, rq)
})
2021-07-15 18:14:05 +00:00
return router
}
func groupMiddleware(group string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, rq *http.Request) {
if cfg.UseAuth && user.CanProceed(rq, group) {
next.ServeHTTP(w, rq)
return
}
// TODO: handle this better. Merge this code with all other
// authorization code in this project.
w.WriteHeader(http.StatusForbidden)
io.WriteString(w, "403 forbidden")
})
}
}