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 Commit string 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)", Commit)) 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: Commit, 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)) }