diff options
| author | a73x <[email protected]> | 2024-08-25 15:14:41 +0100 |
|---|---|---|
| committer | a73x <[email protected]> | 2024-08-25 15:14:41 +0100 |
| commit | fea0ef77c8c98dfadd2f9d29804653293fd31c99 (patch) | |
| tree | 6dadd3062fc77d37e622838eb6b16a60e2774f4d /cmd/home | |
| parent | 794401aaadfaa4efd91587db40439226231ff303 (diff) | |
feat: add templating
Diffstat (limited to 'cmd/home')
| -rw-r--r-- | cmd/home/main.go | 46 |
1 files changed, 46 insertions, 0 deletions
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) + } +} |
