From f7498f4ef1af9581c85a72a39daf40479b92f7d9 Mon Sep 17 00:00:00 2001 From: Bronku Date: Fri, 31 Jan 2025 16:56:12 +0100 Subject: [PATCH] loading all templates automatically --- README.md | 5 ++ controller.go | 22 ++++++ controller/cake.go | 14 ---- controller/controller.go | 29 -------- controller/new_order.go | 17 ----- controller/util.go | 8 --- handlers.go | 24 +++++++ main.go | 5 +- public/index.html | 71 ++++++++++--------- public/script.js | 0 .../options.html => available_cakes.html} | 0 .../confirmation.html => confirm_order.html} | 2 +- util.go | 36 ++++++++++ 13 files changed, 128 insertions(+), 105 deletions(-) create mode 100644 controller.go delete mode 100644 controller/cake.go delete mode 100644 controller/controller.go delete mode 100644 controller/new_order.go delete mode 100644 controller/util.go create mode 100644 handlers.go delete mode 100644 public/script.js rename templates/{cake/options.html => available_cakes.html} (100%) rename templates/{order/confirmation.html => confirm_order.html} (70%) create mode 100644 util.go diff --git a/README.md b/README.md index b2ba6b7..89ff00b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # iroon +## todo: +- extract order creation form into it's own element +- option to add additional cakes +- saving orders on server +- displaying orders ## ux design ### adding an order: 1. user selects the _add order_ button diff --git a/controller.go b/controller.go new file mode 100644 index 0000000..10f2954 --- /dev/null +++ b/controller.go @@ -0,0 +1,22 @@ +package main + +import ( + "html/template" + "io/fs" + "net/http" +) + +type controller struct { + tmpl map[string]*template.Template + http.Handler +} + +func (c *controller) LoadRouter(pub fs.FS) { + serveMux := http.NewServeMux() + + serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) + serveMux.HandleFunc("POST /order/new", c.postNewOrder) + serveMux.HandleFunc("GET /cake/options", c.cakeOptions) + + c.Handler = serveMux +} diff --git a/controller/cake.go b/controller/cake.go deleted file mode 100644 index 96832f9..0000000 --- a/controller/cake.go +++ /dev/null @@ -1,14 +0,0 @@ -package controller - -import ( - "net/http" -) - -func (c *Controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { - type cake struct { - Name string - ID int - } - //w.Header().Set("Content-Type", "text/html") - c.tmpl["cake/options"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) -} diff --git a/controller/controller.go b/controller/controller.go deleted file mode 100644 index 383ba42..0000000 --- a/controller/controller.go +++ /dev/null @@ -1,29 +0,0 @@ -package controller - -import ( - "html/template" - "io/fs" - "net/http" -) - -type Controller struct { - tmpl map[string]*template.Template - http.Handler -} - -// #todo: load all automatically -func (c *Controller) LoadTemplates(fs fs.FS) { - c.tmpl = make(map[string]*template.Template) - c.tmpl["order/confirmation"], _ = template.ParseFS(fs, "templates/order/confirmation.html") - c.tmpl["cake/options"], _ = template.ParseFS(fs, "templates/cake/options.html") -} - -func (c *Controller) LoadRouter(pub fs.FS) { - serveMux := http.NewServeMux() - - serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) - serveMux.HandleFunc("POST /order/new", c.postNewOrder) - serveMux.HandleFunc("GET /cake/options", c.cakeOptions) - - c.Handler = serveMux -} diff --git a/controller/new_order.go b/controller/new_order.go deleted file mode 100644 index dc73107..0000000 --- a/controller/new_order.go +++ /dev/null @@ -1,17 +0,0 @@ -package controller - -import ( - "fmt" - "net/http" -) - -func (c *Controller) postNewOrder(w http.ResponseWriter, r *http.Request) { - fmt.Println("received Post request: ", r) - err := r.ParseForm() - if err != nil { - _ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: err}) - } - w.WriteHeader(http.StatusAccepted) - fmt.Println(r.Form) - _ = c.tmpl["order/confirmation"].Execute(w, struct{ Status any }{Status: r.Form}) -} diff --git a/controller/util.go b/controller/util.go deleted file mode 100644 index cc4b7d6..0000000 --- a/controller/util.go +++ /dev/null @@ -1,8 +0,0 @@ -package controller - -func unwrap[T any](output T, err error) T { - if err != nil { - panic(err) - } - return output -} diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..89308d2 --- /dev/null +++ b/handlers.go @@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "net/http" +) + +func (c *controller) cakeOptions(w http.ResponseWriter, _ *http.Request) { + type cake struct { + Name string + ID int + } + c.tmpl["available_cakes"].Execute(w, struct{ Cakes []cake }{Cakes: []cake{{"Hello", 1}, {"World", 2}}}) +} + +func (c *controller) postNewOrder(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: err}) + } + w.WriteHeader(http.StatusAccepted) + fmt.Println(r.Form) + _ = c.tmpl["confirm_order"].Execute(w, struct{ Status any }{Status: r.Form}) +} diff --git a/main.go b/main.go index eb2e3d1..e5d122d 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "net/http" - "github.com/Bronku/iroon/controller" _ "github.com/mattn/go-sqlite3" ) @@ -17,8 +16,8 @@ var public embed.FS var templates embed.FS func main() { - var c controller.Controller - c.LoadTemplates(templates) + var c controller + c.tmpl = LoadTemplates(templates) c.LoadRouter(public) err := http.ListenAndServe(":8080", c) diff --git a/public/index.html b/public/index.html index 0d8fa4d..6d0b8a6 100644 --- a/public/index.html +++ b/public/index.html @@ -14,39 +14,44 @@

I ran out of names

-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - - -
-
-
+
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + +
+ +
+ +
+
diff --git a/public/script.js b/public/script.js deleted file mode 100644 index e69de29..0000000 diff --git a/templates/cake/options.html b/templates/available_cakes.html similarity index 100% rename from templates/cake/options.html rename to templates/available_cakes.html diff --git a/templates/order/confirmation.html b/templates/confirm_order.html similarity index 70% rename from templates/order/confirmation.html rename to templates/confirm_order.html index 6e09132..4b50c69 100644 --- a/templates/order/confirmation.html +++ b/templates/confirm_order.html @@ -2,6 +2,6 @@ {{.Status}} \ No newline at end of file diff --git a/util.go b/util.go new file mode 100644 index 0000000..7a9afdf --- /dev/null +++ b/util.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "html/template" + "io/fs" + "strings" +) + +func unwrap[T any](output T, err error) T { + if err != nil { + panic(err) + } + return output +} + +func LoadTemplates(fs fs.ReadDirFS) map[string]*template.Template { + files, err := fs.ReadDir("templates") + if err != nil { + fmt.Println("can't read the templates directory") + return nil + } + tmpl := make(map[string]*template.Template) + for _, e := range files { + if e.IsDir() { + continue + } + name := e.Name() + name = strings.Split(name, ".")[0] + tmpl[name], err = template.ParseFS(fs, "templates/"+name+".html") + if err != nil { + fmt.Println("can't open template: " + name) + } + } + return tmpl +}