From ca828e72d3636101f5044b53e02d233879fa8858 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Mon, 18 Mar 2024 15:50:20 +0300 Subject: [PATCH] Simplify file structure --- files.go | 35 ++++++++++++++++++++++++++++++++++- pages.go | 43 ------------------------------------------- serve.go | 5 ++++- 3 files changed, 38 insertions(+), 45 deletions(-) delete mode 100644 pages.go diff --git a/files.go b/files.go index e4b3c44..ab84533 100644 --- a/files.go +++ b/files.go @@ -4,11 +4,13 @@ import ( "encoding/json" "errors" "fmt" + "github.com/go-chi/chi/v5" "io" "net/http" "os" "path/filepath" "strings" + "time" ) // GetFile returns raw contents of a txt file in data directory @@ -16,7 +18,7 @@ func GetFile(filename string, w http.ResponseWriter, r *http.Request) { path := "data/" + filename + ".txt" // Can we and should we sanitize this? if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { - NotFound(w, r) + http.Error(w, "file not found", http.StatusNotFound) return } @@ -74,3 +76,34 @@ func ListFiles(directory string, w http.ResponseWriter, r *http.Request) { filenamesJson, err := json.Marshal(filenames) w.Write(filenamesJson) } + +func GetDay(w http.ResponseWriter, r *http.Request) { + // TODO: This will be different if I move away from chi + dayString := chi.URLParam(r, "day") + if dayString == "" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("day not specified")) + return + } + GetFile("day/"+dayString, w, r) +} + +func GetNote(w http.ResponseWriter, r *http.Request) { + noteString := chi.URLParam(r, "note") + if noteString == "" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("note not specified")) + return + } + GetFile("notes/"+noteString, w, r) +} + +// GetToday runs GetFile with today's daily txt +func GetToday(w http.ResponseWriter, r *http.Request) { + GetFile("day/"+time.Now().Format("2006-01-02"), w, r) +} + +// PostToday runs PostFile with today's daily txt +func PostToday(w http.ResponseWriter, r *http.Request) { + PostFile("day/"+time.Now().Format("2006-01-02"), w, r) +} diff --git a/pages.go b/pages.go deleted file mode 100644 index 38eb797..0000000 --- a/pages.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "github.com/go-chi/chi/v5" - "net/http" - "time" -) - -// NotFound returns a user-friendly 404 error page -func NotFound(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(404) - http.ServeFile(w, r, "./pages/error/404.html") -} - -func GetDay(w http.ResponseWriter, r *http.Request) { - // TODO: This will be *very* different, `today` func will be needed - dayString := chi.URLParam(r, "day") - if dayString == "" { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("day not specified")) - return - } - GetFile("day/"+dayString, w, r) -} - -func GetNote(w http.ResponseWriter, r *http.Request) { - // TODO: This will be *very* different, `today` func will be needed - noteString := chi.URLParam(r, "note") - if noteString == "" { - w.WriteHeader(http.StatusBadRequest) - w.Write([]byte("note not specified")) - return - } - GetFile("notes/"+noteString, w, r) -} - -func GetToday(w http.ResponseWriter, r *http.Request) { - GetFile("day/"+time.Now().Format("2006-01-02"), w, r) -} - -func PostToday(w http.ResponseWriter, r *http.Request) { - PostFile("day/"+time.Now().Format("2006-01-02"), w, r) -} diff --git a/serve.go b/serve.go index 356db81..1ccc070 100644 --- a/serve.go +++ b/serve.go @@ -12,7 +12,10 @@ func Serve() { r := chi.NewRouter() r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes) r.Use(basicAuth) // TODO: ..duh! - r.NotFound(NotFound) + r.NotFound(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(404) + http.ServeFile(w, r, "./pages/error/404.html") + }) // Home page r.Get("/", func(w http.ResponseWriter, r *http.Request) {