diff --git a/controller/controller.go b/controller/controller.go new file mode 100644 index 0000000..58f862e --- /dev/null +++ b/controller/controller.go @@ -0,0 +1,46 @@ +package controller + +import ( + "fmt" + "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.html"], _ = template.ParseFS(fs, "templates/order/confirmation.html") + c.tmpl["new_order.html"], _ = template.ParseFS(fs, "templates/new_order.html") +} + +func (c *Controller) LoadRouter(pub fs.FS) { + serveMux := http.NewServeMux() + + serveMux.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(pub, "public")))) + serveMux.HandleFunc("POST /", c.HandlePost) + serveMux.HandleFunc("GET /new_order.html", c.HandleGet) + + c.Handler = serveMux +} + +func (c *Controller) HandlePost(w http.ResponseWriter, r *http.Request) { + fmt.Println("received Post request: ", r) + err := r.ParseForm() + if err != nil { + _ = c.tmpl["order/confirmation.html"].Execute(w, struct{ Status any }{Status: err}) + } + w.WriteHeader(http.StatusAccepted) + fmt.Println(r.Form) + _ = c.tmpl["order/confirmation.html"].Execute(w, struct{ Status any }{Status: r.Form}) +} + +func (c *Controller) HandleGet(w http.ResponseWriter, r *http.Request) { + fmt.Println("received Get request: ", r) + _ = c.tmpl["new_order.html"].Execute(w, nil) +} diff --git a/controller/util.go b/controller/util.go new file mode 100644 index 0000000..cc4b7d6 --- /dev/null +++ b/controller/util.go @@ -0,0 +1,8 @@ +package controller + +func unwrap[T any](output T, err error) T { + if err != nil { + panic(err) + } + return output +} diff --git a/go.mod b/go.mod index 6ea3f07..d3bfd04 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/Bronku/iroon go 1.23.5 + +require github.com/mattn/go-sqlite3 v1.14.24 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9dcdc9b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go index c49d7cb..eb2e3d1 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,11 @@ package main import ( "embed" "fmt" - "html/template" - "io/fs" + "net/http" + + "github.com/Bronku/iroon/controller" + _ "github.com/mattn/go-sqlite3" ) //go:embed public @@ -14,32 +16,12 @@ var public embed.FS //go:embed templates var templates embed.FS -func unwrap[T any](output T, err error) T { - if err != nil { - panic(err) - } - return output -} - -func handlePost(w http.ResponseWriter, r *http.Request) { - fmt.Println("received Post request: ", r) - err := r.ParseForm() - if err != nil { - fmt.Println(err) - } - w.WriteHeader(http.StatusAccepted) - fmt.Println(r.Form) - tmpl, _ := template.ParseFS(templates, "templates/order/confirmation.html") - _ = tmpl.Execute(w, struct{ Status string }{Status: "accepted"}) -} - func main() { - server := http.NewServeMux() + var c controller.Controller + c.LoadTemplates(templates) + c.LoadRouter(public) - server.Handle("GET /", http.FileServerFS(unwrap(fs.Sub(public, "public")))) - server.HandleFunc("POST /", handlePost) - - err := http.ListenAndServe(":8080", server) + err := http.ListenAndServe(":8080", c) if err != nil { fmt.Println("Can't start the server: ", err) } diff --git a/public/new_order.html b/templates/new_order.html similarity index 55% rename from public/new_order.html rename to templates/new_order.html index f3b9501..c76e8fa 100644 --- a/public/new_order.html +++ b/templates/new_order.html @@ -1,16 +1,24 @@