Simplify file structure

This commit is contained in:
Andrew-71 2024-03-18 15:50:20 +03:00
parent b807a65c0e
commit ca828e72d3
3 changed files with 38 additions and 45 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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) {