1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-02-07 14:40:16 +00:00
mycorrhiza/handlers.go

69 lines
1.8 KiB
Go
Raw Normal View History

2020-06-18 15:23:44 +05:00
package main
import (
"log"
"net/http"
2020-06-19 19:30:19 +05:00
"github.com/bouncepaw/mycorrhiza/fs"
"github.com/bouncepaw/mycorrhiza/render"
2020-06-19 19:30:19 +05:00
"github.com/gorilla/mux"
2020-06-18 15:23:44 +05:00
)
// There are handlers below. See main() for their usage.
2020-06-18 15:23:44 +05:00
// Boilerplate code present in many handlers. Good to have it.
2020-06-27 22:47:53 +05:00
func HandlerBase(w http.ResponseWriter, rq *http.Request) *fs.Hypha {
vars := mux.Vars(rq)
2020-06-27 22:47:53 +05:00
return fs.Hs.Open(vars["hypha"]).OnRevision(RevInMap(vars))
2020-06-18 15:23:44 +05:00
}
func HandlerRaw(w http.ResponseWriter, rq *http.Request) {
log.Println("?action=raw")
2020-06-27 22:47:53 +05:00
HandlerBase(w, rq).ActionRaw(w).LogSuccMaybe("Serving raw text")
2020-06-18 15:23:44 +05:00
}
func HandlerBinary(w http.ResponseWriter, rq *http.Request) {
log.Println("?action=binary")
2020-06-27 22:47:53 +05:00
HandlerBase(w, rq).ActionBinary(w).LogSuccMaybe("Serving binary data")
2020-06-18 15:23:44 +05:00
}
func HandlerZen(w http.ResponseWriter, rq *http.Request) {
2020-06-27 22:47:53 +05:00
log.Println("?action=zen")
HandlerBase(w, rq).ActionZen(w).LogSuccMaybe("Rendering zen")
2020-06-18 15:23:44 +05:00
}
func HandlerView(w http.ResponseWriter, rq *http.Request) {
2020-06-27 22:47:53 +05:00
log.Println("?action=view")
HandlerBase(w, rq).
ActionView(w, render.HyphaPage, render.Hypha404).
LogSuccMaybe("Rendering hypha view")
2020-06-18 15:23:44 +05:00
}
func HandlerEdit(w http.ResponseWriter, rq *http.Request) {
vars := mux.Vars(rq)
2020-06-26 23:07:21 +05:00
h := fs.Hs.Open(vars["hypha"]).OnRevision("0")
2020-06-26 01:31:58 +05:00
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(http.StatusOK)
2020-06-28 20:02:07 +05:00
w.Write(render.HyphaEdit(h))
2020-06-18 15:23:44 +05:00
}
func HandlerUpdate(w http.ResponseWriter, rq *http.Request) {
vars := mux.Vars(rq)
2020-06-26 23:07:21 +05:00
log.Println("Attempt to update hypha", vars["hypha"])
h := fs.Hs.
Open(vars["hypha"]).
CreateDirIfNeeded().
AddRevisionFromHttpData(rq).
WriteTextFileFromHttpData(rq).
WriteBinaryFileFromHttpData(rq).
SaveJson().
2020-06-27 22:47:53 +05:00
Store().
LogSuccMaybe("Saved changes")
2020-06-26 23:07:21 +05:00
2020-06-27 22:47:53 +05:00
if !h.Invalid {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
2020-06-28 20:02:07 +05:00
w.Write(render.HyphaUpdateOk(h))
2020-06-19 18:03:31 +05:00
}
2020-06-18 15:23:44 +05:00
}