diff --git a/config.go b/config.go new file mode 100644 index 0000000..e325a85 --- /dev/null +++ b/config.go @@ -0,0 +1,15 @@ +package main + +const ( + FooterText = ` +
+This website runs MycorrhizaWiki. +` + TitleTemplate = `%s at MycorrhizaWiki` + DefaultStyles = ` + +` + DefaultHeader = ` +

MycorrhizaWiki

+` // TODO: Search input +) diff --git a/go.mod b/go.mod index 7863c69..d8fca3b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bouncepaw/mycorrhiza go 1.14 require ( - github.com/gomarkdown/markdown v0.0.0-20200609195525-3f9352745725 // indirect + 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 diff --git a/main.gg b/main.gg deleted file mode 100644 index 21ac5fc..0000000 --- a/main.gg +++ /dev/null @@ -1,46 +0,0 @@ -package main - -import ( - "fmt" - "github.com/gorilla/mux" - "log" - "net/http" - "time" -) - -func RegexForHypha(s string) string { - return s + ":[^\\s\\d:/?&\\\\][^:?&\\\\]*}" -} - -func EditFillHandler(w http.ResponseWriter, r *http.Request) { - -} - -func HyphaHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - fmt.Fprintf(w, "Accessing hypha %v\n", vars["hypha"]) - fmt.Fprintf(w, "Request at %v", r) -} - -func main() { - r := mux.NewRouter() - r.Queries( - "action", "edit", - "fill", "{templateName}"). - Path(RegexForHypha("/{hypha")). - HandlerFunc(EditFillHandler) - - r.HandleFunc(RegexForHypha("/{hypha"), HyphaHandler) - r.HandleFunc("/kek", HyphaHandler) - http.Handle("/", r) - - srv := &http.Server{ - Handler: r, - Addr: "127.0.0.1:8000", - // Good practice: enforce timeouts for servers you create! - WriteTimeout: 15 * time.Second, - ReadTimeout: 15 * time.Second, - } - - log.Fatal(srv.ListenAndServe()) -} diff --git a/main.go b/main.go index 3cde01b..27662d7 100644 --- a/main.go +++ b/main.go @@ -65,6 +65,26 @@ func main() { log.Println("Showing image of", rev.FullName) }) + r.HandleFunc("/{hypha:"+hyphaPattern+"}", + func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + rev, err := GetRevision(hyphae, vars["hypha"], "0") + if err != nil { + log.Println("Failed to show image of", rev.FullName) + w.WriteHeader(http.StatusInternalServerError) + return + } + html, err := rev.Render(hyphae) + if err != nil { + log.Println("Failed to show image of", rev.FullName) + w.WriteHeader(http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) + fmt.Fprint(w, HyphaPage(hyphae, rev, html)) + log.Println("Rendering", rev.FullName) + }) + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusOK) diff --git a/render.go b/render.go new file mode 100644 index 0000000..0a13779 --- /dev/null +++ b/render.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" +) + +func Layout(f map[string]string) string { + template := ` + + + + %s + %s + + +
%s
+
%s
+ + + %s + + +` + return fmt.Sprintf(template, f["title"], f["head"], f["header"], f["main"], f["sidebar"], FooterText, f["bodyBottom"]) +} + +func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string { + template := ` + +` + args := map[string]string{ + "title": fmt.Sprintf(TitleTemplate, rev.FullName), + "head": DefaultStyles, + "header": DefaultHeader, + "main": fmt.Sprintf(template, content), + "sidebar": "", + "footer": FooterText, + } + + return Layout(args) +} diff --git a/revision.go b/revision.go index abf283e..6ae734f 100644 --- a/revision.go +++ b/revision.go @@ -53,7 +53,7 @@ func (r Revision) Render(hyphae map[string]*Hypha) (ret string, err error) { // If it is a binary hypha (we support only images for now): // TODO: support things other than images. if r.MimeType != "application/x-hypha" { - ret += fmt.Sprintf(``, r.BinaryRequest) + ret += fmt.Sprintf(``, r.BinaryRequest) } contents, err := ioutil.ReadFile(r.TextPath)