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