mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-06-26 07:02:49 +00:00
Implement showing actions
This commit is contained in:
parent
822a122f1d
commit
aef4f1dd09
12
hypha.go
12
hypha.go
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,21 +40,23 @@ func (h Hypha) String() string {
|
|||||||
revbuf)
|
revbuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string) (Revision, error) {
|
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {
|
||||||
for name, _ := range hyphae {
|
for name, _ := range hyphae {
|
||||||
if name == hyphaName {
|
if name == hyphaName {
|
||||||
for _, r := range hyphae[name].Revisions {
|
for _, r := range hyphae[name].Revisions {
|
||||||
id, err := strconv.Atoi(rev)
|
id, err := strconv.Atoi(rev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Revision{}, err
|
log.Println("No such revision", rev, "at hypha", hyphaName)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return Revision{}, false
|
||||||
}
|
}
|
||||||
if r.Id == id {
|
if r.Id == id {
|
||||||
return r, nil
|
return r, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Revision{}, errors.New("Some error idk")
|
return Revision{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// `rev` is the id of revision to render. If it = 0, the last one is rendered. If the revision is not found, an error is returned.
|
// `rev` is the id of revision to render. If it = 0, the last one is rendered. If the revision is not found, an error is returned.
|
||||||
|
65
main.go
65
main.go
@ -23,46 +23,73 @@ func RevInMap(m map[string]string) string {
|
|||||||
func HandlerGetBinary(w http.ResponseWriter, r *http.Request) {
|
func HandlerGetBinary(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
revno := RevInMap(vars)
|
revno := RevInMap(vars)
|
||||||
rev, err := GetRevision(hyphae, vars["hypha"], revno)
|
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||||
if err != nil {
|
if !ok {
|
||||||
log.Println("Failed to show image of", rev.FullName)
|
return
|
||||||
}
|
}
|
||||||
fileContents, err := ioutil.ReadFile(rev.BinaryPath)
|
fileContents, err := ioutil.ReadFile(rev.BinaryPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to show image of", rev.FullName)
|
log.Println("Failed to load binary data of", rev.FullName, rev.Id)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
log.Println("Contents:", fileContents[:10], "...")
|
|
||||||
w.Header().Set("Content-Type", rev.MimeType)
|
w.Header().Set("Content-Type", rev.MimeType)
|
||||||
// w.Header().Set("Content-Length", strconv.Itoa(len(fileContents)))
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(fileContents)
|
w.Write(fileContents)
|
||||||
log.Println("Showing image of", rev.FullName)
|
log.Println("Showing image of", rev.FullName, rev.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandlerRaw(w http.ResponseWriter, r *http.Request) {
|
func HandlerRaw(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotImplemented)
|
vars := mux.Vars(r)
|
||||||
log.Println("Attempt to access an unimplemented thing")
|
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", "text/plain")
|
||||||
|
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) {
|
func HandlerZen(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusNotImplemented)
|
|
||||||
log.Println("Attempt to access an unimplemented thing")
|
|
||||||
}
|
|
||||||
|
|
||||||
func HandlerView(w http.ResponseWriter, r *http.Request) {
|
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
rev, err := GetRevision(hyphae, vars["hypha"], "0")
|
revno := RevInMap(vars)
|
||||||
if err != nil {
|
rev, ok := GetRevision(hyphae, vars["hypha"], revno, w)
|
||||||
log.Println("Failed to show image of", rev.FullName)
|
if !ok {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
html, err := rev.Render(hyphae)
|
html, err := rev.Render(hyphae)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to show image of", rev.FullName)
|
log.Println("Failed to render", rev.FullName)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
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.Render(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)
|
w.WriteHeader(http.StatusOK)
|
||||||
fmt.Fprint(w, HyphaPage(hyphae, rev, html))
|
fmt.Fprint(w, HyphaPage(hyphae, rev, html))
|
||||||
log.Println("Rendering", rev.FullName)
|
log.Println("Rendering", rev.FullName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user