From 934df3f098c33bcbb1493a420833817b80518020 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Sun, 11 Feb 2024 23:45:56 +0100 Subject: Include form handling --- main.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index dd735a1..10ec34a 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "database/sql" "flag" "log" @@ -9,6 +8,8 @@ import ( "net/http" "strconv" + "github.com/gorilla/schema" + "gosten/model" "gosten/templ" @@ -27,6 +28,9 @@ func init() { flag.Uint64Var(&port, "p", 8080, "port to listen on") } +var Q *model.Queries +var s *schema.Decoder + func main() { flag.Parse() @@ -35,8 +39,8 @@ func main() { log.Fatal(err) } - queries := model.New(db) - _, err = queries.GetUsers(context.Background()) + Q = model.New(db) + s = schema.NewDecoder() r := chi.NewRouter() @@ -51,6 +55,25 @@ func main() { templ.Lookup("index").Execute(w, nil) }) + r.Route("/login", func(r chi.Router) { + + type User struct { + Name string `form:"options=required"` + Password string `form:"type=password;options=required"` + } + + r.Get("/", func(w http.ResponseWriter, _ *http.Request) { + templ.Lookup("login").Execute(w, User{}) + }) + + r.Post("/", func(w http.ResponseWriter, r *http.Request) { + u := User{} + r.ParseForm() + s.Decode(&u, r.PostForm) + templ.Lookup("login2").Execute(w, u) + }) + }) + address := net.JoinHostPort(host, strconv.FormatUint(port, 10)) log.Fatal(http.ListenAndServe(address, r)) } -- cgit v1.2.3-70-g09d2