summaryrefslogtreecommitdiff
path: root/web/web.go
blob: a1d2353e7335f055aaea6ee35e72caadc3e7bbe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package web

import (
	"fmt"
	"io/fs"
	"net/http"
	"time"

	"git.sr.ht/~a73x/home/pages"
	"git.sr.ht/~a73x/home/public"
	"go.uber.org/zap"
)

func New(logger *zap.Logger) (*http.Server, error) {
	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()
	pages, err := pages.Collect()
	if err != nil {
		return nil, err
	}

	staticFs, err := fs.Sub(public.FS, "static")
	if err != nil {
		return nil, fmt.Errorf("failed to setup static handler: %v", err)
	}

	mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFs))))
	for _, page := range pages {
		mux.HandleFunc("GET /"+page.Path, func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte(page.Content))
		})
	}

	server := http.Server{
		Addr:    ":8080",
		Handler: loggingMiddleware(mux),
	}

	return &server, nil
}