1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00

Make it render hyphae independently with some UI and add configs

This commit is contained in:
Timur Ismagilov 2020-06-13 21:42:43 +05:00
parent de7b7faca2
commit fc3f2963c3
6 changed files with 86 additions and 48 deletions

15
config.go Normal file
View File

@ -0,0 +1,15 @@
package main
const (
FooterText = `
<hr>
This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.
`
TitleTemplate = `%s at MycorrhizaWiki`
DefaultStyles = `
<link rel="stylesheet" href="/sys/main.css?action=raw">
`
DefaultHeader = `
<h1 class="header__site-title">MycorrhizaWiki</h1>
` // TODO: Search input
)

2
go.mod
View File

@ -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

46
main.gg
View File

@ -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())
}

20
main.go
View File

@ -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)

49
render.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"fmt"
)
func Layout(f map[string]string) string {
template := `
<!doctype html>
<html>
<head>
<title>%s</title>
%s
</head>
<body>
<header class="header">%s</header>
<main class="main">%s</main>
<aside class="sidebar">%s</aside>
<footer class="footer">%s</footer>
%s
</body>
</html>
`
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 := `
<div class="naviwrapper">
<form class="naviwrapper__buttons">
<input type="submit" name="action" value="edit"/>
<input type="submit" name="action" value="getBinary"/>
<input type="submit" name="action" value="zen"/>
<input type="submit" name="action" value="raw"/>
</form>
%s
</div>
`
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)
}

View File

@ -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(`<img src="%s" class="page__image"/>`, r.BinaryRequest)
ret += fmt.Sprintf(`<img src="/%s" class="page__image"/>`, r.BinaryRequest)
}
contents, err := ioutil.ReadFile(r.TextPath)