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

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