read only mode a gogo

master
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

@ -19,6 +19,7 @@ var (
Foot string Foot string
OAuthServer string OAuthServer string
VersionInterval time.Duration VersionInterval time.Duration
ReadOnly bool
) )
func init() { func init() {
@ -36,11 +37,12 @@ func Refresh() {
as.Append(args.STRING, "wrap", "file with http header/footer", "") as.Append(args.STRING, "wrap", "file with http header/footer", "")
as.Append(args.STRING, "oauth", "oauth URL", "") as.Append(args.STRING, "oauth", "oauth URL", "")
as.Append(args.DURATION, "version", "duration to mark versions", "0s") as.Append(args.DURATION, "version", "duration to mark versions", "0s")
as.Append(args.BOOL, "ro", "read-only mode", false)
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
wrap := as.Get("wrap").GetString() wrap := as.GetString("wrap")
var b []byte var b []byte
if len(wrap) > 0 { if len(wrap) > 0 {
log.Printf("reading %v (%T)", wrap, wrap) log.Printf("reading %v (%T)", wrap, wrap)
@ -57,13 +59,14 @@ func Refresh() {
panic(len(bs)) panic(len(bs))
} }
Root = strings.TrimSuffix(as.Get("root").GetString(), "/") Root = strings.TrimSuffix(as.GetString("root"), "/")
Root, _ = filepath.Abs(Root) Root, _ = filepath.Abs(Root)
Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":") Port = ":" + strings.TrimPrefix(as.GetString("port"), ":")
Head = string(bs[0]) Head = string(bs[0])
Foot = string(bs[1]) Foot = string(bs[1])
OAuthServer = as.Get("oauth").GetString() OAuthServer = as.GetString("oauth")
VersionInterval = as.Get("version").GetDuration() VersionInterval = as.GetDuration("version")
ReadOnly = as.GetBool("ro")
} }
const defaultWrapper = ` const defaultWrapper = `

View File

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

View File

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