controller

This commit is contained in:
Bronku 2025-01-29 10:26:36 +01:00
parent 28a3ca3bb4
commit 8c3611e859
6 changed files with 78 additions and 30 deletions

46
controller/controller.go Normal file
View file

@ -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)
}

8
controller/util.go Normal file
View file

@ -0,0 +1,8 @@
package controller
func unwrap[T any](output T, err error) T {
if err != nil {
panic(err)
}
return output
}

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/Bronku/iroon
go 1.23.5
require github.com/mattn/go-sqlite3 v1.14.24

2
go.sum Normal file
View file

@ -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=

34
main.go
View file

@ -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)
}

View file

@ -1,16 +1,24 @@
<form x-show="open" hx-post="order/new" hx-swap="outerHTML">
<div>
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname" value="">
<input type="text" id="firstname" name="firstname" required value="">
</div>
<div >
<div>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" value="">
</div>
<div >
<div>
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone" value="">
</div>
<div>
<label for="location">Location</label>
<input type="text" id="location" name="location" value="">
</div>
<div>
<label for="paid">Paid</label>
<input type="number" id="paid" name="paid" step="0.01" min="0" value="">
</div>
<button type="button" x-on:click="open = false">Cancel</button>
<button type="submit">Submit</button>
</form>