summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2024-02-14 00:41:29 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2024-02-14 00:41:29 +0100
commit3194896b33500bab959147bac38ab4fb93dd55bb (patch)
tree420de5cc68b8f5bd57c871ce0ca27a880eadf7f8
parent53124974a4f6fd7c16579b8fc6b40ad620737af9 (diff)
downloadgosten-3194896b33500bab959147bac38ab4fb93dd55bb.tar.gz
gosten-3194896b33500bab959147bac38ab4fb93dd55bb.tar.bz2
gosten-3194896b33500bab959147bac38ab4fb93dd55bb.zip
Cleanup
-rw-r--r--auth.go7
-rw-r--r--form.go37
-rw-r--r--main.go37
3 files changed, 44 insertions, 37 deletions
diff --git a/auth.go b/auth.go
index 3613530..14cd6d7 100644
--- a/auth.go
+++ b/auth.go
@@ -101,6 +101,13 @@ func handleLogout() http.HandlerFunc {
}
}
+type User struct {
+ Name string `form:"options=required"`
+ Password string `form:"type=password;options=required"`
+ RememberMe bool `form:"type=checkbox;value=y;options=checked"`
+ Errors []error `form:"-"`
+}
+
func showLoginPage(w http.ResponseWriter, u User) {
showTemplate(w, "login", u)
}
diff --git a/form.go b/form.go
new file mode 100644
index 0000000..c5dffa0
--- /dev/null
+++ b/form.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+
+ "github.com/gorilla/schema"
+)
+
+var schemaDecoder *schema.Decoder
+
+func init() {
+ schemaDecoder = schema.NewDecoder()
+}
+
+type fieldError struct {
+ Field string
+ Issue string
+}
+
+func (fe fieldError) Error() string {
+ return fmt.Sprintf("%s: %v", fe.Field, fe.Issue)
+}
+
+func (fe fieldError) FieldError() (field, err string) {
+ return fe.Field, fe.Issue
+}
+
+func parseForm[T any](r *http.Request, data *T) {
+ if err := r.ParseForm(); err != nil {
+ log.Panic("Parsing form: ", err)
+ }
+ if err := schemaDecoder.Decode(data, r.PostForm); err != nil {
+ log.Panic("Decoding form: ", err)
+ }
+}
diff --git a/main.go b/main.go
index 896da78..20200c3 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,7 @@
package main
import (
- "encoding/gob"
"flag"
- "fmt"
"log"
"net"
"net/http"
@@ -11,7 +9,6 @@ import (
"strconv"
"github.com/gorilla/handlers"
- "github.com/gorilla/schema"
"gosten/model"
"gosten/templ"
@@ -26,24 +23,19 @@ var (
func init() {
flag.StringVar(&host, "h", "localhost", "address to listen on")
flag.Uint64Var(&port, "p", 8080, "port to listen on")
-
- gob.Register(SessionData{})
}
var Q *model.Queries
-var s *schema.Decoder
func main() {
flag.Parse()
db := openDB("")
-
if err := db.Ping(); err != nil {
log.Fatal(err)
}
Q = model.New(db)
- s = schema.NewDecoder()
mux := http.NewServeMux()
@@ -67,41 +59,12 @@ func main() {
log.Fatal(http.ListenAndServe(address, handler))
}
-type User struct {
- Name string `form:"options=required"`
- Password string `form:"type=password;options=required"`
- RememberMe bool `form:"type=checkbox;value=y;options=checked"`
- Errors []error `form:"-"`
-}
-
-type fieldError struct {
- Field string
- Issue string
-}
-
-func (fe fieldError) Error() string {
- return fmt.Sprintf("%s: %v", fe.Field, fe.Issue)
-}
-
-func (fe fieldError) FieldError() (field, err string) {
- return fe.Field, fe.Issue
-}
-
func showTemplate(w http.ResponseWriter, tpl string, data any) {
if err := templ.Lookup(tpl).Execute(w, data); err != nil {
log.Panicf("Executing '%s' with %+v: %v", tpl, data, err)
}
}
-func parseForm[T any](r *http.Request, data *T) {
- if err := r.ParseForm(); err != nil {
- log.Panic("Parsing form: ", err)
- }
- if err := s.Decode(data, r.PostForm); err != nil {
- log.Panic("Decoding form: ", err)
- }
-}
-
func indexPage() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uid := userId(r)