diff options
Diffstat (limited to 'cmd/server')
-rw-r--r-- | cmd/server/filemap.go | 77 | ||||
-rw-r--r-- | cmd/server/logging.go | 55 | ||||
-rw-r--r-- | cmd/server/main.go | 50 | ||||
-rw-r--r-- | cmd/server/server.go | 161 |
4 files changed, 39 insertions, 304 deletions
diff --git a/cmd/server/filemap.go b/cmd/server/filemap.go deleted file mode 100644 index 5f7e1bb..0000000 --- a/cmd/server/filemap.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "fmt" - "hash/fnv" - "io" - "io/fs" - "log" - "log/slog" - "os" - "path/filepath" - "strings" - - "github.com/pkg/errors" -) - -type File struct { - filename string - etag string -} - -var files = map[string]File{} - -func hashFile(filename string) (string, error) { - f, err := os.Open(filename) - if err != nil { - return "", err - } - defer f.Close() - hash := fnv.New64a() - if _, err := io.Copy(hash, f); err != nil { - return "", err - } - return fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), nil -} - -func registerFile(urlpath string, filepath string) error { - if files[urlpath] != (File{}) { - log.Printf("registerFile called with duplicate file, urlPath: %s", urlpath) - return nil - } - hash, err := hashFile(filepath) - if err != nil { - return err - } - files[urlpath] = File{ - filename: filepath, - etag: hash, - } - return nil -} - -func registerContentFiles(root string) error { - err := filepath.WalkDir(root, func(filePath string, f fs.DirEntry, err error) error { - if err != nil { - return errors.WithMessagef(err, "failed to access path %s", filePath) - } - relPath, err := filepath.Rel(root, filePath) - if err != nil { - return errors.WithMessagef(err, "failed to make path relative, path: %s", filePath) - } - urlPath, _ := strings.CutSuffix(relPath, "index.html") - if !f.IsDir() { - slog.Debug("registering file", "urlpath", "/"+urlPath) - return registerFile("/"+urlPath, filePath) - } - return nil - }) - if err != nil { - return err - } - return nil -} - -func GetFile(urlPath string) File { - return files[urlPath] -} diff --git a/cmd/server/logging.go b/cmd/server/logging.go deleted file mode 100644 index 601baab..0000000 --- a/cmd/server/logging.go +++ /dev/null @@ -1,55 +0,0 @@ -package main - -import ( - "fmt" - "io" - "net/http" -) - -type loggingResponseWriter struct { - http.ResponseWriter - statusCode int -} - -func (lrw *loggingResponseWriter) WriteHeader(code int) { - lrw.statusCode = code - // avoids warning: superfluous response.WriteHeader call - if lrw.statusCode != http.StatusOK { - lrw.ResponseWriter.WriteHeader(code) - } -} - -func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter { - return &loggingResponseWriter{w, http.StatusOK} -} - -type wrappedHandlerOptions struct { - defaultHostname string - logger io.Writer -} - -func wrapHandlerWithLogging(wrappedHandler http.Handler, opts wrappedHandlerOptions) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - scheme := r.Header.Get("X-Forwarded-Proto") - if scheme == "" { - scheme = "http" - } - host := r.Header.Get("Host") - if host == "" { - host = opts.defaultHostname - } - lw := NewLoggingResponseWriter(w) - wrappedHandler.ServeHTTP(lw, r) - statusCode := lw.statusCode - fmt.Fprintf( - opts.logger, - "%s %s %d %s %s %s\n", - scheme, - r.Method, - statusCode, - host, - r.URL.Path, - lw.Header().Get("Location"), - ) - }) -} diff --git a/cmd/server/main.go b/cmd/server/main.go index b6817d8..bae215a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -5,7 +5,10 @@ import ( "log" "log/slog" "os" - cfg "website/internal/config" + "os/signal" + "sync" + + "website/internal/server" "github.com/ardanlabs/conf/v3" "github.com/pkg/errors" @@ -16,20 +19,14 @@ var ( ShortSHA string ) -type Config struct { - Production bool `conf:"default:false"` - ListenAddress string `conf:"default:localhost"` - Port uint16 `conf:"default:3000,short:p"` - BaseURL cfg.URL `conf:"default:http://localhost:3000,short:b"` - RedirectOtherHostnames bool `conf:"default:false"` -} - func main() { if os.Getenv("DEBUG") != "" { slog.SetLogLoggerLevel(slog.LevelDebug) } + log.SetFlags(log.LstdFlags | log.Lmsgprefix) + log.SetPrefix("server: ") - runtimeConfig := Config{} + runtimeConfig := server.Config{} help, err := conf.Parse("", &runtimeConfig) if err != nil { if errors.Is(err, conf.ErrHelpWanted) { @@ -39,5 +36,36 @@ func main() { log.Panicf("parsing runtime configuration: %v", err) } - startServer(&runtimeConfig) + c := make(chan os.Signal, 2) + signal.Notify(c, os.Interrupt) + sv, err := server.New(&runtimeConfig) + if err != nil { + log.Fatalf("error setting up server: %v", err) + } + wg := &sync.WaitGroup{} + wg.Add(1) + go func() { + defer wg.Done() + sig := <-c + log.Printf("signal captured: %v", sig) + <-sv.Stop() + slog.Debug("server stopped") + }() + + sErr := make(chan error) + wg.Add(1) + go func() { + defer wg.Done() + sErr <- sv.Start() + }() + if !runtimeConfig.InDevServer { + log.Printf("server listening on %s", sv.Addr) + } + + err = <-sErr + if err != nil { + // Error starting or closing listener: + log.Fatalf("error: %v", err) + } + wg.Wait() } diff --git a/cmd/server/server.go b/cmd/server/server.go deleted file mode 100644 index 9a1e48a..0000000 --- a/cmd/server/server.go +++ /dev/null @@ -1,161 +0,0 @@ -package main - -import ( - "fmt" - "io" - "log" - "log/slog" - "mime" - "net" - "net/http" - "os" - "strings" - "time" - - cfg "website/internal/config" - - "github.com/getsentry/sentry-go" - sentryhttp "github.com/getsentry/sentry-go/http" - "github.com/shengyanli1982/law" -) - -var config *cfg.Config - -type HTTPError struct { - Error error - Message string - Code int -} - -func canonicalisePath(path string) (cPath string, differs bool) { - cPath = path - if strings.HasSuffix(path, "/index.html") { - cPath, differs = strings.CutSuffix(path, "index.html") - } else if !strings.HasSuffix(path, "/") && files[path+"/"] != (File{}) { - cPath, differs = path+"/", true - } - return cPath, differs -} - -func serveFile(w http.ResponseWriter, r *http.Request) *HTTPError { - urlPath, shouldRedirect := canonicalisePath(r.URL.Path) - if shouldRedirect { - http.Redirect(w, r, urlPath, 302) - return nil - } - file := GetFile(urlPath) - if file == (File{}) { - return &HTTPError{ - Message: "File not found", - Code: http.StatusNotFound, - } - } - w.Header().Add("ETag", file.etag) - w.Header().Add("Vary", "Accept-Encoding") - for k, v := range config.Extra.Headers { - w.Header().Add(k, v) - } - - http.ServeFile(w, r, files[urlPath].filename) - return nil -} - -type webHandler func(http.ResponseWriter, *http.Request) *HTTPError - -func (fn webHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - defer func() { - if fail := recover(); fail != nil { - w.WriteHeader(http.StatusInternalServerError) - slog.Error("runtime panic!", "error", fail) - } - }() - w.Header().Set("Server", fmt.Sprintf("website (%s)", ShortSHA)) - if err := fn(w, r); err != nil { - if strings.Contains(r.Header.Get("Accept"), "text/html") { - w.WriteHeader(err.Code) - notFoundPage := "website/private/404.html" - http.ServeFile(w, r, notFoundPage) - } else { - http.Error(w, err.Message, err.Code) - } - } -} - -var newMIMEs = map[string]string{ - ".xsl": "text/xsl", -} - -func fixupMIMETypes() { - for ext, newType := range newMIMEs { - if err := mime.AddExtensionType(ext, newType); err != nil { - slog.Error("could not update mime type", "ext", ext, "mime", newType) - } - } -} - -func startServer(runtimeConfig *Config) { - fixupMIMETypes() - - c, err := cfg.GetConfig() - if err != nil { - log.Panicf("parsing configuration file: %v", err) - } - config = c - - prefix := "website/public" - slog.Debug("registering content files", "prefix", prefix) - err = registerContentFiles(prefix) - if err != nil { - log.Panicf("registering content files: %v", err) - } - - env := "development" - if runtimeConfig.Production { - env = "production" - } - err = sentry.Init(sentry.ClientOptions{ - EnableTracing: true, - TracesSampleRate: 1.0, - Dsn: os.Getenv("SENTRY_DSN"), - Release: CommitSHA, - Environment: env, - }) - if err != nil { - log.Panic("could not set up sentry") - } - defer sentry.Flush(2 * time.Second) - sentryHandler := sentryhttp.New(sentryhttp.Options{ - Repanic: true, - }) - - mux := http.NewServeMux() - slog.Debug("binding main handler to", "host", runtimeConfig.BaseURL.Hostname()+"/") - mux.Handle(runtimeConfig.BaseURL.Hostname()+"/", webHandler(serveFile)) - - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - newURL := runtimeConfig.BaseURL.String() + r.URL.String() - http.Redirect(w, r, newURL, 301) - }) - - var logWriter io.Writer - if runtimeConfig.Production { - logWriter = law.NewWriteAsyncer(os.Stdout, nil) - } else { - logWriter = os.Stdout - } - http.Handle("/", - sentryHandler.Handle( - wrapHandlerWithLogging(mux, wrappedHandlerOptions{ - defaultHostname: runtimeConfig.BaseURL.Hostname(), - logger: logWriter, - }), - ), - ) - // no logging, no sentry - http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - }) - - listenAddress := net.JoinHostPort(runtimeConfig.ListenAddress, fmt.Sprint(runtimeConfig.Port)) - log.Fatal(http.ListenAndServe(listenAddress, nil)) -} |