aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/config.go3
-rw-r--r--backend/home.go58
-rw-r--r--backend/templates/base.html2
-rw-r--r--backend/templates/simple.html1
-rw-r--r--main.go1
5 files changed, 63 insertions, 2 deletions
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 @@
<footer>
<p>&copy; 2025 - Anhgelus Morthuuzh</p>
<p>«&thinsp;{{ .Quote }}&thinsp;»</p>
- <p><a href="">Mentions légales</a>, <a href="https://git.anhgelus.world/anhgelus/small-web">Code source</a></p>
+ <p><a href="/legal">Mentions légales</a>, <a href="https://git.anhgelus.world/anhgelus/small-web">Code source</a></p>
</footer>
</body>
</html>
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))