diff options
Diffstat (limited to '')
-rw-r--r-- | go.mod | 3 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | main.go | 29 | ||||
-rw-r--r-- | templ/form.tpl | 14 | ||||
-rw-r--r-- | templ/login.tpl | 6 | ||||
-rw-r--r-- | templ/login2.tpl | 4 | ||||
-rw-r--r-- | templ/template.go | 7 |
7 files changed, 62 insertions, 5 deletions
@@ -3,14 +3,15 @@ module gosten go 1.22 require ( + github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e github.com/go-chi/chi/v5 v5.0.11 + github.com/gorilla/schema v1.2.1 github.com/mattn/go-sqlite3 v1.14.22 modernc.org/sqlite v1.28.0 ) require ( github.com/dustin/go-humanize v1.0.1 // indirect - github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -1,3 +1,5 @@ +github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e h1:v3DDTGBMt9pclCdG7jRyNAABmtJw3uky/Xoi/DfbWNs= +github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e/go.mod h1:JxpmgZ5hjL6fyhBoZ4HAUadkp7DNqWlHbFL7l8oic4Y= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= @@ -8,6 +10,8 @@ github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbu github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/schema v1.2.1 h1:tjDxcmdb+siIqkTNoV+qRH2mjYdr2hHe5MKXbp61ziM= +github.com/gorilla/schema v1.2.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -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)) } diff --git a/templ/form.tpl b/templ/form.tpl new file mode 100644 index 0000000..b17cb1d --- /dev/null +++ b/templ/form.tpl @@ -0,0 +1,14 @@ +{{define "formItem"}} + <label {{with .ID}}for="{{.}}"{{end}}>{{.Label}}</label> + <input + {{with .ID}}id="{{.}}"{{end}} + type="{{.Type}}" + name="{{.Name}}" + placeholder="{{.Placeholder}}" + {{with .Value}}value="{{.}}"{{end}} + {{with .Class}}class="{{.}}"{{end}} + {{range .Options}} {{.}} {{end}} + > + {{with .Footer}}<p>{{.}}</p>{{end}} + <br /> +{{end}}
\ No newline at end of file diff --git a/templ/login.tpl b/templ/login.tpl new file mode 100644 index 0000000..40d49c9 --- /dev/null +++ b/templ/login.tpl @@ -0,0 +1,6 @@ +{{define "body"}} + <form action="/login" method="post"> + {{inputs_for .}} + <button type="submit">Log In!</button> + </form> +{{end}}
\ No newline at end of file diff --git a/templ/login2.tpl b/templ/login2.tpl new file mode 100644 index 0000000..89ba6a5 --- /dev/null +++ b/templ/login2.tpl @@ -0,0 +1,4 @@ +{{define "body"}} + Logged in with user: {{.Name}} <br> + You have chosen: {{.Password}} +{{end}}
\ No newline at end of file diff --git a/templ/template.go b/templ/template.go index 8fed965..20a4de7 100644 --- a/templ/template.go +++ b/templ/template.go @@ -4,6 +4,8 @@ import ( "embed" "html/template" "sync" + + "github.com/Necoro/form" ) //go:embed *.tpl @@ -13,9 +15,12 @@ var templates = make(map[string]*template.Template) var muTpl sync.RWMutex var baseTpl *template.Template +var formBuilder form.Builder func init() { - baseTpl = template.Must(template.ParseFS(fs, "base.tpl")) + baseTpl = template.Must(template.ParseFS(fs, "base.tpl", "form.tpl")) + formBuilder = form.Builder{InputTemplate: baseTpl.Lookup("formItem")} + baseTpl.Funcs(formBuilder.FuncMap()) } func Lookup(name string) *template.Template { |