From 952e04815a8339b6ac7d599e7a8e409002a03aa5 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 6 Aug 2020 21:20:29 -0600 Subject: [PATCH] read only mode a gogo --- config/config.go | 13 ++++++++----- server/notes.go | 10 ++++++++++ server/routes.go | 37 +++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index e8c1549..9c5ff27 100755 --- a/config/config.go +++ b/config/config.go @@ -19,6 +19,7 @@ var ( Foot string OAuthServer string VersionInterval time.Duration + ReadOnly bool ) func init() { @@ -36,11 +37,12 @@ func Refresh() { as.Append(args.STRING, "wrap", "file with http header/footer", "") as.Append(args.STRING, "oauth", "oauth URL", "") 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 { panic(err) } - wrap := as.Get("wrap").GetString() + wrap := as.GetString("wrap") var b []byte if len(wrap) > 0 { log.Printf("reading %v (%T)", wrap, wrap) @@ -57,13 +59,14 @@ func Refresh() { panic(len(bs)) } - Root = strings.TrimSuffix(as.Get("root").GetString(), "/") + Root = strings.TrimSuffix(as.GetString("root"), "/") Root, _ = filepath.Abs(Root) - Port = ":" + strings.TrimPrefix(as.Get("port").GetString(), ":") + Port = ":" + strings.TrimPrefix(as.GetString("port"), ":") Head = string(bs[0]) Foot = string(bs[1]) - OAuthServer = as.Get("oauth").GetString() - VersionInterval = as.Get("version").GetDuration() + OAuthServer = as.GetString("oauth") + VersionInterval = as.GetDuration("version") + ReadOnly = as.GetBool("ro") } const defaultWrapper = ` diff --git a/server/notes.go b/server/notes.go index e21e742..7543356 100755 --- a/server/notes.go +++ b/server/notes.go @@ -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, `

`, path.Join("/edit/", baseHREF)) } func htmlDelete(w http.ResponseWriter, baseHREF string) { + if config.ReadOnly { + return + } fmt.Fprintf(w, `

`, path.Join("/delete/", baseHREF)) } func htmlCreate(w http.ResponseWriter, baseHREF string) { + if config.ReadOnly { + return + } fmt.Fprintf(w, `
diff --git a/server/routes.go b/server/routes.go index bf31d62..2e9a3f0 100755 --- a/server/routes.go +++ b/server/routes.go @@ -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 } }