diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-08 16:54:02 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-08 16:54:02 +0000 |
commit | ced9bbed371d7a1d7692c820f80c77f43ebd32a3 (patch) | |
tree | 896ef80f261b81049ac18797e7b0c3ae89c0837f /cmd | |
parent | 4bcba90a1a78cac4ec2ed370bb751d95717b7120 (diff) |
Created a /organisations/list route
There is also some basic CSS here.
Also introduced a proper structured logger.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/web/handlers.go | 21 | ||||
-rw-r--r-- | cmd/web/main.go | 14 | ||||
-rw-r--r-- | cmd/web/routes.go | 4 |
3 files changed, 35 insertions, 4 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index fd70c5e..8df8928 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -18,11 +18,30 @@ func (app *application) serverError(w http.ResponseWriter, r *http.Request, err trace = string(debug.Stack()) ) - // TODO: reintroduce: app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace) + app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace) log.Printf("Crash! %s %s %s", method, uri, trace) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) } +func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) { + files := []string{ + "./ui/html/base.tmpl.html", + "./ui/html/pages/organisations/list.tmpl.html", + // "./ui/html/partials/nav.tmpl.html", + } + + ts, err := template.ParseFiles(files...) + if err != nil { + app.serverError(w, r, err) // Use the serverError() helper + return + } + + err = ts.ExecuteTemplate(w, "base", nil) + if err != nil { + app.serverError(w, r, err) // Use the serverError() helper + } +} + func (app *application) home(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { app.notFound(w) diff --git a/cmd/web/main.go b/cmd/web/main.go index bd7e500..d388364 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -2,16 +2,24 @@ package main import ( "log" + "log/slog" "net/http" + "os" ) -type application struct{} +type application struct { + logger *slog.Logger +} func main() { - app := &application{} + logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + app := &application{ + logger: logger, + } // mux := http.NewServeMux() // mux.HandleFunc("/", home) - log.Print("starting server on :4000") + // log.Print("starting server on :4000") + logger.Info("starting server on :4000") err := http.ListenAndServe(":4000", app.routes()) log.Fatal(err) } diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 2761d99..7e3aabd 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -5,6 +5,10 @@ import "net/http" func (app *application) routes() *http.ServeMux { mux := http.NewServeMux() + fileServer := http.FileServer(http.Dir("./ui/static/")) + mux.Handle("/static/", http.StripPrefix("/static", fileServer)) + mux.HandleFunc("/", app.home) + mux.HandleFunc("/organisation/list", app.listOrganisation) return mux } |