From b8a964e19de9cb025587c3546930a18c0d4edd07 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Fri, 3 Oct 2025 11:36:22 +0200 Subject: feat(backend): handle simple root files --- backend/config.go | 3 +++ backend/home.go | 58 ++++++++++++++++++++++++++++++++++++++++++- backend/templates/base.html | 2 +- backend/templates/simple.html | 1 + main.go | 1 + 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 backend/templates/simple.html diff --git a/backend/config.go b/backend/config.go index b1fcbd8..33665ea 100644 --- a/backend/config.go +++ b/backend/config.go @@ -24,6 +24,7 @@ type Config struct { Links []Link `toml:"links"` Logo Logo `toml:"logo"` LogFolder string `toml:"log_folder"` + RootFolder string `toml:"root_folder"` Quotes []string `toml:"quotes"` } @@ -46,6 +47,8 @@ func (c *Config) DefaultValues() { Favicon: "favicon.jpg", } c.LogFolder = "data/logs" + c.RootFolder = "data" + c.Quotes = []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do."} } func LoadConfig(path string) (*Config, bool) { diff --git a/backend/home.go b/backend/home.go index d41102c..8799b36 100644 --- a/backend/home.go +++ b/backend/home.go @@ -1,16 +1,24 @@ package backend import ( + "fmt" + "html/template" "log/slog" "net/http" + "os" + "path/filepath" "strconv" + "git.anhgelus.world/anhgelus/small-world/markdown" "github.com/go-chi/chi/v5" ) const maxLogsPerPage = 5 -var sortedLogs []*logData +var ( + sortedLogs []*logData + rootContent = map[string]template.HTML{} +) type homeData struct { *data @@ -33,6 +41,54 @@ func HandleHome(r *chi.Mux) { }) } +type rootData struct { + *data + Content template.HTML +} + +func (l *rootData) SetData(d *data) { + l.data = d +} + +func HandleRoot(r *chi.Mux, cfg *Config) { + err := os.Mkdir(cfg.RootFolder, 0660) + if err != nil && !os.IsExist(err) { + panic(err) + } + r.Get("/{name:[a-zA-Z-]+}", func(w http.ResponseWriter, r *http.Request) { + handleGenericRoot(w, r, chi.URLParam(r, "name")) + }) +} + +func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { + d := new(rootData) + d.data = new(data) + if c, ok := rootContent[name]; ok { + d.Content = c + } else { + cfg := r.Context().Value("config").(*Config) + path := filepath.Join(cfg.RootFolder, name+".md") + b, err := os.ReadFile(path) + if err != nil { + if os.IsNotExist(err) { + http.NotFoundHandler().ServeHTTP(w, r) + return + } + panic(err) + } + var errMd *markdown.ParseError + d.Content, errMd = markdown.ParseBytes(b) + if errMd != nil { + slog.Error("parsing markdown", "path", path) + fmt.Println(errMd.Pretty()) + w.WriteHeader(http.StatusInternalServerError) + return + } + rootContent[name] = d.Content + } + d.handleGeneric(w, r, "simple", d) +} + func handleGenericLogsDisplay(w http.ResponseWriter, r *http.Request) *homeData { rawPage := r.URL.Query().Get("page") page := 1 diff --git a/backend/templates/base.html b/backend/templates/base.html index 8d8567a..5d62ffa 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -39,7 +39,7 @@ diff --git a/backend/templates/simple.html b/backend/templates/simple.html new file mode 100644 index 0000000..dd00397 --- /dev/null +++ b/backend/templates/simple.html @@ -0,0 +1 @@ +{{ define "body" }}{{ .Content }}{{ end }} diff --git a/main.go b/main.go index fc64d5c..480a501 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,7 @@ func main() { r := backend.NewRouter(dev, cfg) backend.HandleHome(r) + backend.HandleRoot(r, cfg) backend.HandleLogs(r) backend.HandleStaticFiles(r, "/assets", backend.UsableEmbedFS("dist", embeds)) -- cgit v1.2.3