From 3927a5dea40aa50213bda4895c34e296f122b9d3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 24 Feb 2024 22:39:06 +0100 Subject: Test new 1.22 http routes --- main.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 2c50720..d6841f0 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,41 @@ package main import ( + "embed" "flag" + "html/template" "net/http" ) -var ( - listen = flag.String("listen", ":8080", "Listen at") - data = flag.String("data", "data", "Data dir") -) +//go:embed static tmpl +var static embed.FS func main() { + var ( + listen = flag.String("listen", "localhost:8080", "Listen at") + data = flag.String("data", "data", "Data dir") + ) flag.Parse() - http.Handle("/css/", http.FileServer(http.Dir("assets"))) - http.Handle("/fonts/", http.FileServer(http.Dir("assets"))) - http.HandleFunc("/index", indexHandler) - http.HandleFunc("/view/", viewHandler) - http.HandleFunc("/edit/", editHandler) - http.HandleFunc("/del/", delHandler) - http.HandleFunc("/save/", saveHandler) - http.HandleFunc("/", homeHandler) + wiki := Wiki{Root: *data} + http.Handle("GET /static/", http.FileServerFS(static)) + http.Handle("GET /index", Index{ + Wiki: wiki, + Template: template.Must(template.ParseFS(static, "tmpl/root", "tmpl/index")), + }) + http.Handle("GET /view/{title}", View{ + Wiki: wiki, + Template: template.Must(template.ParseFS(static, "tmpl/root", "tmpl/view")), + }) + http.Handle("GET /edit/{title}", Edit{ + Wiki: wiki, + Template: template.Must(template.ParseFS(static, "tmpl/root", "tmpl/edit")), + }) + http.Handle("POST /save/{title}", Save{ + Wiki: wiki, + }) + http.Handle("GET /del/{title}", Delete{ + Wiki: wiki, + }) + http.Handle("GET /", http.RedirectHandler("/view/Home", http.StatusFound)) http.ListenAndServe(*listen, nil) } -- cgit v1.2.3