2021-05-09 11:09:27 +00:00
|
|
|
// Package web contains web handlers and initialization stuff.
|
|
|
|
//
|
|
|
|
// It exports just one function: Init. Call it if you want to have web capabilities.
|
2021-05-09 10:42:12 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-06-12 13:51:28 +00:00
|
|
|
"mime"
|
2021-05-09 10:42:12 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2021-05-11 10:14:00 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2021-06-12 13:51:28 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/static"
|
2021-05-09 10:42:12 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
2021-05-11 10:14:00 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2021-05-09 10:42:12 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
|
|
|
)
|
|
|
|
|
2021-06-12 13:51:28 +00:00
|
|
|
var stylesheets = []string{"default.css", "custom.css"}
|
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
// httpErr is used by many handlers to signal errors in a compact way.
|
|
|
|
func httpErr(w http.ResponseWriter, status int, name, title, errMsg string) {
|
2021-05-09 10:42:12 +00:00
|
|
|
log.Println(errMsg, "for", name)
|
2021-06-23 15:44:27 +00:00
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
2021-05-09 10:42:12 +00:00
|
|
|
w.WriteHeader(status)
|
|
|
|
fmt.Fprint(
|
|
|
|
w,
|
|
|
|
views.BaseHTML(
|
|
|
|
title,
|
|
|
|
fmt.Sprintf(
|
2021-06-14 07:13:29 +00:00
|
|
|
`<main class="main-width"><p>%s. <a href="/hypha/%s">Go back to the hypha.<a></p></main>`,
|
2021-05-09 10:42:12 +00:00
|
|
|
errMsg,
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
user.EmptyUser(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerStyle(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
util.PrepareRq(rq)
|
|
|
|
|
2021-06-23 15:35:33 +00:00
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(".css"))
|
2021-06-12 13:51:28 +00:00
|
|
|
for _, name := range stylesheets {
|
|
|
|
file, err := static.FS.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2021-05-09 10:42:12 +00:00
|
|
|
}
|
2021-06-12 13:51:28 +00:00
|
|
|
io.Copy(w, file)
|
|
|
|
file.Close()
|
2021-05-09 10:42:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerUserList(w http.ResponseWriter, rq *http.Request) {
|
2021-07-10 19:04:21 +00:00
|
|
|
if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown {
|
|
|
|
return
|
|
|
|
}
|
2021-06-23 15:44:27 +00:00
|
|
|
w.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
2021-05-09 10:42:12 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(views.BaseHTML("User list", views.UserListHTML(), user.FromRequest(rq))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
2021-06-23 15:44:27 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
|
|
|
file, err := static.FS.Open("robots.txt")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
io.Copy(w, file)
|
|
|
|
file.Close()
|
2021-05-09 10:42:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
func Handler() http.Handler {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.Use(func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Do stuff here
|
|
|
|
log.Println(r.RequestURI)
|
|
|
|
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Available all the time
|
|
|
|
initAuth(r)
|
|
|
|
|
|
|
|
initReaders(r)
|
|
|
|
initMutators(r)
|
|
|
|
|
|
|
|
initAdmin(r)
|
|
|
|
initHistory(r)
|
|
|
|
initStuff(r)
|
|
|
|
initSearch(r)
|
2021-05-09 11:09:27 +00:00
|
|
|
|
2021-06-12 13:51:28 +00:00
|
|
|
// Miscellaneous
|
2021-07-15 17:46:35 +00:00
|
|
|
r.HandleFunc("/user-list", handlerUserList)
|
|
|
|
r.HandleFunc("/robots.txt", handlerRobotsTxt)
|
2021-06-12 13:51:28 +00:00
|
|
|
|
|
|
|
// Static assets
|
2021-07-15 17:46:35 +00:00
|
|
|
r.HandleFunc("/static/style.css", handlerStyle)
|
|
|
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.FS))))
|
2021-06-12 13:51:28 +00:00
|
|
|
|
|
|
|
// Index page
|
2021-07-15 17:46:35 +00:00
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Let's pray it never fails
|
|
|
|
addr, _ := url.Parse("/hypha/" + cfg.HomeHypha)
|
|
|
|
r.URL = addr
|
|
|
|
handlerHypha(w, r)
|
2021-05-09 10:42:12 +00:00
|
|
|
})
|
2021-07-15 17:46:35 +00:00
|
|
|
|
|
|
|
return r
|
2021-05-09 10:42:12 +00:00
|
|
|
}
|