controller
This commit is contained in:
parent
28a3ca3bb4
commit
8c3611e859
6 changed files with 78 additions and 30 deletions
46
controller/controller.go
Normal file
46
controller/controller.go
Normal 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
8
controller/util.go
Normal 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
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module github.com/Bronku/iroon
|
module github.com/Bronku/iroon
|
||||||
|
|
||||||
go 1.23.5
|
go 1.23.5
|
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.24
|
||||||
|
|
|
||||||
2
go.sum
Normal file
2
go.sum
Normal 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
34
main.go
|
|
@ -3,9 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Bronku/iroon/controller"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed public
|
//go:embed public
|
||||||
|
|
@ -14,32 +16,12 @@ var public embed.FS
|
||||||
//go:embed templates
|
//go:embed templates
|
||||||
var templates embed.FS
|
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() {
|
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"))))
|
err := http.ListenAndServe(":8080", c)
|
||||||
server.HandleFunc("POST /", handlePost)
|
|
||||||
|
|
||||||
err := http.ListenAndServe(":8080", server)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Can't start the server: ", err)
|
fmt.Println("Can't start the server: ", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,24 @@
|
||||||
<form x-show="open" hx-post="order/new" hx-swap="outerHTML">
|
<form x-show="open" hx-post="order/new" hx-swap="outerHTML">
|
||||||
<div>
|
<div>
|
||||||
<label for="firstname">First Name</label>
|
<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 >
|
<div>
|
||||||
<label for="lastname">Last Name</label>
|
<label for="lastname">Last Name</label>
|
||||||
<input type="text" id="lastname" name="lastname" value="">
|
<input type="text" id="lastname" name="lastname" value="">
|
||||||
</div>
|
</div>
|
||||||
<div >
|
<div>
|
||||||
<label for="phone">Phone</label>
|
<label for="phone">Phone</label>
|
||||||
<input type="tel" id="phone" name="phone" value="">
|
<input type="tel" id="phone" name="phone" value="">
|
||||||
</div>
|
</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="button" x-on:click="open = false">Cancel</button>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue