mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-13 05:50:27 +00:00
Move handlers to a different place
This commit is contained in:
parent
5f8739e936
commit
2879096258
2
go.mod
2
go.mod
@ -5,6 +5,4 @@ go 1.14
|
||||
require (
|
||||
github.com/gomarkdown/markdown v0.0.0-20200609195525-3f9352745725
|
||||
github.com/gorilla/mux v1.7.4
|
||||
github.com/sirupsen/logrus v1.6.0 // indirect
|
||||
gopkg.in/ini.v1 v1.57.0
|
||||
)
|
||||
|
68
handlers.go
Normal file
68
handlers.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Boilerplate code present in many handlers. Good to have it.
|
||||
func HandlerBase(w http.ResponseWriter, r *http.Request) (Revision, bool) {
|
||||
vars := mux.Vars(r)
|
||||
revno := RevInMap(vars)
|
||||
return GetRevision(hyphae, vars["hypha"], revno, w)
|
||||
}
|
||||
|
||||
func HandlerGetBinary(w http.ResponseWriter, r *http.Request) {
|
||||
if rev, ok := HandlerBase(w, r); ok {
|
||||
rev.ActionGetBinary(w)
|
||||
}
|
||||
}
|
||||
|
||||
func HandlerRaw(w http.ResponseWriter, r *http.Request) {
|
||||
if rev, ok := HandlerBase(w, r); ok {
|
||||
rev.ActionRaw(w)
|
||||
}
|
||||
}
|
||||
|
||||
func HandlerZen(w http.ResponseWriter, r *http.Request) {
|
||||
if rev, ok := HandlerBase(w, r); ok {
|
||||
rev.ActionZen(w)
|
||||
}
|
||||
}
|
||||
|
||||
func HandlerView(w http.ResponseWriter, r *http.Request) {
|
||||
if rev, ok := HandlerBase(w, r); ok {
|
||||
rev.ActionView(w, HyphaPage)
|
||||
}
|
||||
}
|
||||
|
||||
func HandlerHistory(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerEdit(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
ActionEdit(vars["hypha"], w)
|
||||
}
|
||||
|
||||
func HandlerRewind(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerDelete(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerRename(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
107
main.go
107
main.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -36,112 +35,6 @@ func RevInMap(m map[string]string) string {
|
||||
return "0"
|
||||
}
|
||||
|
||||
// handlers
|
||||
func HandlerGetBinary(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
revno := RevInMap(vars)
|
||||
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fileContents, err := ioutil.ReadFile(rev.BinaryPath)
|
||||
if err != nil {
|
||||
log.Println("Failed to load binary data of", rev.FullName, rev.Id)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", rev.BinaryMime)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(fileContents)
|
||||
log.Println("Showing image of", rev.FullName, rev.Id)
|
||||
}
|
||||
|
||||
func HandlerRaw(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
revno := RevInMap(vars)
|
||||
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fileContents, err := ioutil.ReadFile(rev.TextPath)
|
||||
if err != nil {
|
||||
log.Println("Failed to load text data of", rev.FullName, rev.Id)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", rev.TextMime)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(fileContents)
|
||||
log.Println("Serving text data of", rev.FullName, rev.Id)
|
||||
}
|
||||
|
||||
func HandlerZen(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
revno := RevInMap(vars)
|
||||
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
html, err := rev.AsHtml(hyphae)
|
||||
if err != nil {
|
||||
log.Println("Failed to render", rev.FullName)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, html)
|
||||
}
|
||||
|
||||
func HandlerView(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
revno := RevInMap(vars)
|
||||
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
html, err := rev.AsHtml(hyphae)
|
||||
if err != nil {
|
||||
log.Println("Failed to render", rev.FullName)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, HyphaPage(hyphae, rev, html))
|
||||
log.Println("Rendering", rev.FullName)
|
||||
}
|
||||
|
||||
func HandlerHistory(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerEdit(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
ActionEdit(vars["hypha"], w)
|
||||
}
|
||||
|
||||
func HandlerRewind(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerDelete(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerRename(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
func HandlerUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotImplemented)
|
||||
log.Println("Attempt to access an unimplemented thing")
|
||||
}
|
||||
|
||||
var rootWikiDir string
|
||||
var hyphae map[string]*Hypha
|
||||
|
||||
|
@ -62,7 +62,7 @@ func (r *Revision) AsHtml(hyphae map[string]*Hypha) (ret string, err error) {
|
||||
}
|
||||
|
||||
func (r *Revision) ActionGetBinary(w http.ResponseWriter) {
|
||||
fileContents, err := ioutil.ReadFile(r.urlOfBinary())
|
||||
fileContents, err := ioutil.ReadFile(r.BinaryPath)
|
||||
if err != nil {
|
||||
log.Println("Failed to load binary data of", r.FullName, r.Id)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
@ -87,7 +87,7 @@ func (r *Revision) ActionRaw(w http.ResponseWriter) {
|
||||
log.Println("Serving text data of", r.FullName, r.Id)
|
||||
}
|
||||
|
||||
func (r *Revision) ActionZen(w http.ResponseWriter, hyphae map[string]*Hypha) {
|
||||
func (r *Revision) ActionZen(w http.ResponseWriter) {
|
||||
html, err := r.AsHtml(hyphae)
|
||||
if err != nil {
|
||||
log.Println("Failed to render", r.FullName)
|
||||
@ -99,7 +99,7 @@ func (r *Revision) ActionZen(w http.ResponseWriter, hyphae map[string]*Hypha) {
|
||||
fmt.Fprint(w, html)
|
||||
}
|
||||
|
||||
func (r *Revision) ActionView(w http.ResponseWriter, hyphae map[string]*Hypha, layoutFun func(map[string]*Hypha, Revision, string) string) {
|
||||
func (r *Revision) ActionView(w http.ResponseWriter, layoutFun func(map[string]*Hypha, Revision, string) string) {
|
||||
html, err := r.AsHtml(hyphae)
|
||||
if err != nil {
|
||||
log.Println("Failed to render", r.FullName)
|
||||
@ -111,6 +111,7 @@ func (r *Revision) ActionView(w http.ResponseWriter, hyphae map[string]*Hypha, l
|
||||
fmt.Fprint(w, layoutFun(hyphae, *r, html))
|
||||
log.Println("Rendering", r.FullName)
|
||||
}
|
||||
|
||||
func (r *Revision) Name() string {
|
||||
return r.FullName
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user