From fea0ef77c8c98dfadd2f9d29804653293fd31c99 Mon Sep 17 00:00:00 2001 From: a73x Date: Sun, 25 Aug 2024 15:14:41 +0100 Subject: feat: add templating --- cmd/home/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cmd/home/main.go (limited to 'cmd/home') diff --git a/cmd/home/main.go b/cmd/home/main.go new file mode 100644 index 0000000..f284c30 --- /dev/null +++ b/cmd/home/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "io/fs" + "log" + "net/http" + "time" + + "git.sr.ht/~a73x/home" + "go.uber.org/zap" +) + +func main() { + logger, _ := zap.NewProduction() + loggingMiddleware := func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + next.ServeHTTP(w, r) + logger.Info("request received", + zap.String("url", r.URL.Path), + zap.String("method", r.Method), + zap.Duration("duration", time.Since(start)), + zap.String("user-agent", r.UserAgent()), + ) + }) + } + + mux := http.NewServeMux() + + staticFs, err := fs.Sub(home.Content, "public") + if err != nil { + log.Fatal(err) + } + + mux.Handle("GET /", http.FileServer(http.FS(staticFs))) + + server := http.Server{ + Addr: ":8080", + Handler: loggingMiddleware(mux), + } + + err = server.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} -- cgit v1.2.3