1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00
mycorrhiza/http_readers.go

117 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/bouncepaw/mycorrhiza/gemtext"
2020-08-19 18:54:23 +00:00
"github.com/bouncepaw/mycorrhiza/history"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/templates"
2020-08-05 20:19:14 +00:00
"github.com/bouncepaw/mycorrhiza/tree"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/util"
)
func init() {
http.HandleFunc("/page/", handlerPage)
http.HandleFunc("/text/", handlerText)
http.HandleFunc("/binary/", handlerBinary)
2020-08-19 18:54:23 +00:00
http.HandleFunc("/history/", handlerHistory)
http.HandleFunc("/rev/", handlerRevision)
}
// handlerRevision displays a specific revision of text part a page
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
var (
shorterUrl = strings.TrimPrefix(rq.URL.Path, "/rev/")
revHash = path.Dir(shorterUrl)
hyphaName = CanonicalName(strings.TrimPrefix(shorterUrl, revHash+"/"))
contents = fmt.Sprintf(`<p>This hypha had no text at this revision.</p>`)
textPath = hyphaName + "&.gmi"
textContents, err = history.FileAtRevision(textPath, revHash)
)
if err == nil {
contents = gemtext.ToHtml(hyphaName, textContents)
}
2020-08-31 17:52:26 +00:00
page := templates.RevisionHTML(
hyphaName,
naviTitle(hyphaName),
contents,
2020-08-28 19:43:36 +00:00
tree.TreeAsHtml(hyphaName, IterateHyphaNamesWith),
2020-08-31 17:52:26 +00:00
revHash,
)
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(http.StatusOK)
2020-08-31 17:52:26 +00:00
w.Write([]byte(base(hyphaName, page)))
2020-08-19 18:54:23 +00:00
}
// handlerHistory lists all revisions of a hypha
func handlerHistory(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
hyphaName := HyphaNameFromRq(rq, "history")
var tbody string
2020-08-28 19:10:46 +00:00
if _, ok := HyphaStorage[hyphaName]; ok {
revs, err := history.Revisions(hyphaName)
2020-08-19 18:54:23 +00:00
if err == nil {
2020-08-28 19:10:46 +00:00
for _, rev := range revs {
tbody += rev.AsHtmlTableRow(hyphaName)
2020-08-19 18:54:23 +00:00
}
}
2020-08-28 19:10:46 +00:00
log.Println(revs)
2020-08-19 18:54:23 +00:00
}
2020-08-31 17:52:26 +00:00
util.HTTP200Page(w,
base(hyphaName, templates.HistoryHTML(hyphaName, tbody)))
}
// handlerText serves raw source text of the hypha.
func handlerText(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
hyphaName := HyphaNameFromRq(rq, "text")
if data, ok := HyphaStorage[hyphaName]; ok {
log.Println("Serving", data.textPath)
w.Header().Set("Content-Type", data.textType.Mime())
http.ServeFile(w, rq, data.textPath)
}
}
// handlerBinary serves binary part of the hypha.
func handlerBinary(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
hyphaName := HyphaNameFromRq(rq, "binary")
if data, ok := HyphaStorage[hyphaName]; ok {
log.Println("Serving", data.binaryPath)
w.Header().Set("Content-Type", data.binaryType.Mime())
http.ServeFile(w, rq, data.binaryPath)
}
}
// handlerPage is the main hypha action that displays the hypha and the binary upload form along with some navigation.
func handlerPage(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
var (
hyphaName = HyphaNameFromRq(rq, "page")
data, hyphaExists = HyphaStorage[hyphaName]
2020-08-31 17:52:26 +00:00
contents string
)
if hyphaExists {
fileContentsT, errT := ioutil.ReadFile(data.textPath)
_, errB := os.Stat(data.binaryPath)
if errT == nil {
contents = gemtext.ToHtml(hyphaName, string(fileContentsT))
}
if !os.IsNotExist(errB) {
contents = binaryHtmlBlock(hyphaName, data) + contents
}
}
2020-08-31 17:52:26 +00:00
util.HTTP200Page(w, base(hyphaName, templates.PageHTML(hyphaName,
2020-08-05 20:19:14 +00:00
naviTitle(hyphaName),
contents,
2020-08-31 17:52:26 +00:00
tree.TreeAsHtml(hyphaName, IterateHyphaNamesWith))))
}