read only mode a gogo

This commit is contained in:
Bel LaPointe
2020-08-06 21:20:29 -06:00
parent d6c95a536c
commit 952e04815a
3 changed files with 37 additions and 23 deletions

View File

@@ -2,6 +2,7 @@ package server
import (
"fmt"
"local/notes-server/config"
"local/notes-server/filetree"
"net/http"
"path"
@@ -58,18 +59,27 @@ func fileHead(w http.ResponseWriter, baseHREF string) {
}
func htmlEdit(w http.ResponseWriter, baseHREF string) {
if config.ReadOnly {
return
}
fmt.Fprintf(w, `<div style='display:inline-block'>
<a href=%q><input type="button" value="Edit"></input></a>
</div><br>`, path.Join("/edit/", baseHREF))
}
func htmlDelete(w http.ResponseWriter, baseHREF string) {
if config.ReadOnly {
return
}
fmt.Fprintf(w, `<div style='display:inline-block'>
<a href=%q><input type="button" value="Delete" onclick="return confirm('Delete?');"></input></a>
</div><br>`, path.Join("/delete/", baseHREF))
}
func htmlCreate(w http.ResponseWriter, baseHREF string) {
if config.ReadOnly {
return
}
fmt.Fprintf(w, `
<form action=%q method="get">
<input type="text" name="base"></input>

View File

@@ -3,49 +3,50 @@ package server
import (
"fmt"
"local/gziphttp"
"local/notes-server/config"
"local/router"
"net/http"
"path/filepath"
"strings"
)
func (s *Server) Routes() error {
wildcard := router.Wildcard
endpoints := []struct {
path string
endpoints := map[string]struct {
handler http.HandlerFunc
}{
{
path: "/",
"/": {
handler: s.root,
},
{
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.notes)),
},
{
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
fmt.Sprintf("edit/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.edit)),
},
{
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
fmt.Sprintf("delete/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.delete)),
},
{
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
fmt.Sprintf("submit/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.submit)),
},
{
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
fmt.Sprintf("create/%s%s", wildcard, wildcard): {
handler: s.gzip(s.authenticate(s.create)),
},
{
path: fmt.Sprintf("search"),
fmt.Sprintf("search"): {
handler: s.gzip(s.authenticate(s.search)),
},
}
for _, endpoint := range endpoints {
if err := s.Add(endpoint.path, endpoint.handler); err != nil {
for path, endpoint := range endpoints {
if config.ReadOnly {
for _, prefix := range []string{"edit/", "delete/", "delete/", "create/", "submit/"} {
if strings.HasPrefix(path, prefix) {
endpoint.handler = http.NotFound
}
}
}
if err := s.Add(path, endpoint.handler); err != nil {
return err
}
}