lets-go:3.3: structured logging, configuration, app struct, error

This commit is contained in:
tamsin johnson 2024-01-24 13:32:07 -08:00
parent cf5c574b57
commit ff3df0e57e
3 changed files with 41 additions and 13 deletions

View File

@ -3,13 +3,13 @@ package main
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"log" "log/slog"
"net/http" "net/http"
"strconv" "strconv"
) )
// home ... // home ...
func home(w http.ResponseWriter, r *http.Request) { func (app *application) home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -23,21 +23,21 @@ func home(w http.ResponseWriter, r *http.Request) {
ts, err := template.ParseFiles(files...) ts, err := template.ParseFiles(files...)
if err != nil { if err != nil {
log.Print(err.Error()) app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI()))
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
err = ts.ExecuteTemplate(w, "base", nil) err = ts.ExecuteTemplate(w, "base", nil)
if err != nil { if err != nil {
log.Print(err.Error()) app.logger.Error(err.Error(), slog.String("method", r.Method), slog.String("uri", r.URL.RequestURI()))
http.Error(w, "Internal Server Error", http.StatusInternalServerError) http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return return
} }
} }
// snippetView ... // snippetView ...
func snippetView(w http.ResponseWriter, r *http.Request) { func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id")) id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 { if err != nil || id < 1 {
http.NotFound(w, r) http.NotFound(w, r)
@ -48,7 +48,7 @@ func snippetView(w http.ResponseWriter, r *http.Request) {
} }
// snippetCreate ... // snippetCreate ...
func snippetCreate(w http.ResponseWriter, r *http.Request) { func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Allow", "POST") w.Header().Set("Allow", "POST")
if r.Method != http.MethodPost { if r.Method != http.MethodPost {

View File

@ -2,27 +2,55 @@ package main
import ( import (
"flag" "flag"
"log" "log/slog"
"net/http" "net/http"
"os"
) )
// for application wide dependencies
type application struct {
logger *slog.Logger
}
// main it's the snippetbox webapp // main it's the snippetbox webapp
func main() { func main() {
//configuration // configuration
addr := flag.String("addr", ":4000", "HTTP network address") addr := flag.String("addr", ":4000", "HTTP network address")
logfmt := flag.String("logfmt", "text", "Log output format")
loglevel := flag.String("loglevel", "INFO", "Log level: DEBUG, INFO, WARN, or ERROR")
flag.Parse() flag.Parse()
var logger *slog.Logger
var handler_options slog.HandlerOptions
switch *loglevel {
case "DEBUG": handler_options = slog.HandlerOptions{Level: slog.LevelDebug}
case "INFO": handler_options = slog.HandlerOptions{Level: slog.LevelInfo}
case "WARN": handler_options = slog.HandlerOptions{Level: slog.LevelWarn}
case "ERROR": handler_options = slog.HandlerOptions{Level: slog.LevelError}
}
switch *logfmt {
case "json": logger = slog.New(slog.NewJSONHandler(os.Stdout, &handler_options))
default: logger = slog.New(slog.NewTextHandler(os.Stdout, &handler_options))
}
app := &application{
logger: logger,
}
mux := http.NewServeMux() mux := http.NewServeMux()
// setup server for static files // setup server for static files
fileServer := http.FileServer(http.Dir("./ui/static")) fileServer := http.FileServer(http.Dir("./ui/static"))
mux.Handle("/static/", http.StripPrefix("/static", fileServer)) mux.Handle("/static/", http.StripPrefix("/static", fileServer))
mux.HandleFunc("/", home) mux.HandleFunc("/", app.home)
mux.HandleFunc("/snippet/view", snippetView) mux.HandleFunc("/snippet/view", app.snippetView)
mux.HandleFunc("/snippet/create", snippetCreate) mux.HandleFunc("/snippet/create", app.snippetCreate)
log.Printf("starting a server on %s", *addr) logger.Info("starting server", slog.String("addr", *addr))
err := http.ListenAndServe(*addr, mux) err := http.ListenAndServe(*addr, mux)
log.Fatal(err) logger.Error(err.Error())
os.Exit(1)
} }