From a9420a2a4f3221d1771e7f28786ccdd80ceed88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Tue, 13 Feb 2024 18:33:05 +0100 Subject: Replace chi by http/net ServeMux --- go.mod | 5 +++-- go.sum | 6 ++++-- main.go | 61 +++++++++++++++++++++++++++++++------------------------------ 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index ceee926..60b816c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.22 require ( github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e - github.com/go-chi/chi/v5 v5.0.11 + github.com/go-sql-driver/mysql v1.7.1 + github.com/gorilla/handlers v1.5.2 github.com/gorilla/schema v1.2.1 github.com/mattn/go-sqlite3 v1.14.22 modernc.org/sqlite v1.28.0 @@ -12,7 +13,7 @@ require ( require ( github.com/dustin/go-humanize v1.0.1 // indirect - github.com/go-sql-driver/mysql v1.7.1 // indirect + github.com/felixge/httpsnoop v1.0.3 // 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 diff --git a/go.sum b/go.sum index 54c98b8..64d4922 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ github.com/Necoro/form v0.0.0-20240211223301-6fa9f8196e1e h1:v3DDTGBMt9pclCdG7jR 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= -github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= +github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -12,6 +12,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/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= +github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= 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= diff --git a/main.go b/main.go index 10ec34a..cef556e 100644 --- a/main.go +++ b/main.go @@ -6,15 +6,14 @@ import ( "log" "net" "net/http" + "os" "strconv" + "github.com/gorilla/handlers" "github.com/gorilla/schema" "gosten/model" "gosten/templ" - - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" ) // flags @@ -42,38 +41,40 @@ func main() { Q = model.New(db) s = schema.NewDecoder() - r := chi.NewRouter() + mux := http.NewServeMux() + + mux.HandleFunc("/{$}", showTemplate("index", nil)) + mux.HandleFunc("GET /login", showTemplate("login", User{})) + mux.HandleFunc("POST /login", handleLogin) - // A good base middleware stack - r.Use(middleware.RequestID) - r.Use(middleware.RealIP) - r.Use(middleware.CleanPath) - r.Use(middleware.Logger) - r.Use(middleware.Recoverer) + handler := handlers.CombinedLoggingHandler(os.Stderr, mux) + handler = handlers.ProxyHeaders(handler) - r.Get("/", func(w http.ResponseWriter, _ *http.Request) { - templ.Lookup("index").Execute(w, nil) - }) + address := net.JoinHostPort(host, strconv.FormatUint(port, 10)) + log.Fatal(http.ListenAndServe(address, handler)) +} - r.Route("/login", func(r chi.Router) { +type User struct { + Name string `form:"options=required"` + Password string `form:"type=password;options=required"` +} - type User struct { - Name string `form:"options=required"` - Password string `form:"type=password;options=required"` +func showTemplate(tpl string, data any) http.HandlerFunc { + return func(w http.ResponseWriter, _ *http.Request) { + if err := templ.Lookup(tpl).Execute(w, data); err != nil { + log.Panicf("Executing '%s' with %+v: %v", tpl, data, err) } + } +} - 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) - }) - }) +func handleLogin(w http.ResponseWriter, r *http.Request) { + u := User{} + if err := r.ParseForm(); err != nil { + log.Panic("Parsing form: ", err) + } + if err := s.Decode(&u, r.PostForm); err != nil { + log.Panic("Decoding form: ", err) + } - address := net.JoinHostPort(host, strconv.FormatUint(port, 10)) - log.Fatal(http.ListenAndServe(address, r)) + showTemplate("login2", u).ServeHTTP(w, r) } -- cgit v1.2.3