Skip to content

The only tool you need to know what is happening and how to fix it.

21.1 KB
Raw Blame
package cmd

import (
	"bytes"
	"context"
	"fmt"
	"html"
	"io/fs"
	"net"
	"net/http"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/tracewayapp/traceway/backend/app/backfill"
	"github.com/tracewayapp/traceway/backend/app/cache"
	"github.com/tracewayapp/traceway/backend/app/chdb"
	"github.com/tracewayapp/traceway/backend/app/config"
	"github.com/tracewayapp/traceway/backend/app/controllers"
	"github.com/tracewayapp/traceway/backend/app/db"
	"github.com/tracewayapp/traceway/backend/app/middleware"
	"github.com/tracewayapp/traceway/backend/app/migrations"
	"github.com/tracewayapp/traceway/backend/app/models"
	"github.com/tracewayapp/traceway/backend/app/monitoring"
	"github.com/tracewayapp/traceway/backend/app/notifications"
	"github.com/tracewayapp/traceway/backend/app/oncall"
	"github.com/tracewayapp/traceway/backend/app/outbox"
	"github.com/tracewayapp/traceway/backend/app/recordings"
	"github.com/tracewayapp/traceway/backend/app/retention"
	"github.com/tracewayapp/traceway/backend/app/services"
	"github.com/tracewayapp/traceway/backend/app/services/mcpmount"
	"github.com/tracewayapp/traceway/backend/app/slo"
	"github.com/tracewayapp/traceway/backend/app/sourcemapbackfill"
	"github.com/tracewayapp/traceway/backend/app/storage"
	"github.com/tracewayapp/traceway/backend/app/symbolicator/sourcemap/scopes"
	"github.com/tracewayapp/traceway/backend/app/synthetics"
	"github.com/tracewayapp/traceway/backend/static"

	"github.com/gin-gonic/gin"
	"github.com/joho/godotenv"
	traceway "go.tracewayapp.com"
	tracewaygin "go.tracewayapp.com/tracewaygin"
)

var PostStartupHooks []func(ctx context.Context)

func Run(opts ...Option) {
	var cfg *config.Cfg
	var o *options

	if len(opts) == 0 {
		godotenv.Load()
		cfg = config.LoadFromEnv()
	} else {
		o = &options{sqlitePath: ":memory:"}
		for _, opt := range opts {
			opt(o)
		}
		port := o.port
		if port == 0 {
			port = 8082
		}
		cfg = &config.Cfg{
			JWTSecret:   embeddedJWTSecret(o),
			DBType:      "sqlite",
			SQLitePath:  o.sqlitePath,
			StorageType: "local",
			StoragePath: "./storage",
			APIOnly:     "false",
			Ports:       fmt.Sprintf("%d", port),
		}
		if o.serverURL == "" {
			o.serverURL = fmt.Sprintf("http://localhost:%d", port)
		}
		cfg.AppBaseURL = o.serverURL
		cfg.MonitoringTracewayURL = o.monitoringTracewayURL
		gin.SetMode(gin.ReleaseMode)
		if o.disableLogging {
			config.LoggingEnabled = false
		}

		applyEnvOverrides(cfg)
	}
	config.Init(cfg)

	if err := services.InitJWT(); err != nil {
		// A missing signing key is a configuration mistake, not a crash: print
		// something actionable instead of burying the message under a panic
		// stack trace. The standalone binary deliberately has no default -- a
		// key committed to the repo would be identical in every clone.
		fmt.Fprintln(os.Stderr, "FATAL: "+err.Error())
		fmt.Fprintln(os.Stderr, "Generate one with: openssl rand -hex 32")
		os.Exit(1)
	}

	trustedNets, proxyErr := trustedProxyNets(cfg.TrustedProxyList())
	if proxyErr != nil {
		fmt.Fprintln(os.Stderr, "FATAL: invalid TRUSTED_PROXIES: "+proxyErr.Error())
		fmt.Fprintln(os.Stderr, "Use comma-separated IP addresses or CIDR ranges, for example 10.0.0.0/8,203.0.113.7")
		os.Exit(1)
	}

	err := db.Init()
	if err != nil {
		panic(fmt.Errorf("error connecting to database: %w", err))
	}

	err = chdb.Init()
	if err != nil {
		panic(fmt.Errorf("error connecting to chdb: %w", err))
	}

	models.Init(db.Driver)

	if err := storage.Init(); err != nil {
		panic(fmt.Errorf("failed to initialize storage: %w", err))
	}

	err = migrations.Run(cfg.DBType)
	if err != nil {
		panic(fmt.Errorf("migrations run failed: %w", err))
	}

	if err := backfill.RunDashboards(); err != nil {
		panic(fmt.Errorf("dashboards backfill failed: %w", err))
	}
	if err := backfill.RunOtelAgentDashboardSources(); err != nil {
		panic(fmt.Errorf("OTel agent dashboard backfill failed: %w", err))
	}

	if o != nil {
		if err := seed(o); err != nil {
			panic(fmt.Errorf("seeding failed: %w", err))
		}
	}

	ctx := context.Background()
	if err := cache.ProjectCache.Init(ctx); err != nil {
		panic(fmt.Errorf("projects cache could not be initialized: %w", err))
	}
	services.InitSourceMapCache(
		parsePositiveInt(cfg.SourceMapCacheMaxEntries, 200),
		int64(parsePositiveInt(cfg.SourceMapCacheMaxBytesMB, 500))*1024*1024,
	)
	switch cfg.SourceMapCacheType {
	case "", "memory":
	case "disk":
		dir := cfg.SourceMapDiskCachePath
		if dir == "" {
			dir = "./twcache"
		}
		maxBytes := int64(parsePositiveInt(cfg.SourceMapDiskCacheMaxMB, 2048)) * 1024 * 1024
		if err := services.EnableSymbolicatorDiskCache(dir, maxBytes); err != nil {
			panic(fmt.Errorf("source map disk cache init failed: %w", err))
		}
	default:
		panic(fmt.Errorf("unknown SOURCEMAP_CACHE_TYPE: %s", cfg.SourceMapCacheType))
	}
	if cfg.SymbolicatorParser != "" {
		if err := scopes.SetParser(cfg.SymbolicatorParser); err != nil {
			panic(fmt.Errorf("symbolicator parser init failed: %w", err))
		}
	}

	middleware.InitUseClientAuth()
	middleware.InitUseAppAuth()
	middleware.InitRequireWriteAccess()
	middleware.InitRequireProjectAccess()
	middleware.InitRequireAdminAccess()
	middleware.InitRequireOrganizationAccess()
	middleware.InitUseSourceMapAuth()
	middleware.InitUseRunnerAuth()

	services.InitEmail()
	services.InitTurnstile()
	services.InitOAuth()

	for _, hook := range PostStartupHooks {
		hook(ctx)
	}

	outbox.RegisterSender(notifications.AdapterSend)
	outbox.RegisterTerminalHook(notifications.OnOutboxTerminal)
	notifications.RegisterPageOpener(oncall.OpenPageFromDispatch)
	notifications.RegisterPageResolver(oncall.AutoResolveByDedupKey)
	synthetics.RegisterNotifier(notifications.OnCheckStateChange)
	outbox.StartDrain(ctx)
	oncall.StartEscalator(ctx)
	notifications.StartEvaluator(ctx)
	synthetics.Start(ctx)
	slo.Start(ctx)
	retention.Start(ctx)
	recordings.Start(ctx)
	sourcemapbackfill.Start(ctx)

	var router *gin.Engine
	if o != nil && o.disableLogging {
		router = gin.New()
		router.Use(gin.Recovery())
	} else {
		router = gin.Default()
	}

	router.Use(middleware.GuardBodyReads(middleware.DefaultBodyIdle, middleware.DefaultBodyTotal))

	if err := configureClientIP(router, cfg); err != nil {
		panic(fmt.Errorf("invalid TRUSTED_PROXIES: %w", err))
	}
	if cfg.TrustedProxyHeader == "" {
		router.Use(warnOnUntrustedForwardedFor(trustedNets))
	}
	router.Use(middleware.SecurityHeaders)

	if monitoringTracewayUrl := cfg.MonitoringTracewayURL; monitoringTracewayUrl != "" {
		twmw := tracewaygin.New(
			monitoringTracewayUrl,
			tracewaygin.WithOnErrorRecording(tracewaygin.RecordingQuery|tracewaygin.RecordingBody|tracewaygin.RecordingHeader|tracewaygin.RecordingUrl),
		)

		selfToken := monitoringTracewayUrl
		if i := strings.IndexByte(selfToken, '@'); i >= 0 {
			selfToken = selfToken[:i]
		}
		selfAuth := "Bearer " + selfToken

		router.Use(func(c *gin.Context) {
			if c.GetHeader("Authorization") == selfAuth {
				c.Next()
				return
			}
			// The runner poll long-polls for up to 25s; recording it would
			// register as 25s endpoint samples and wreck self-monitoring p95.
			if c.Request.URL.Path == "/api/runners/poll" {
				c.Next()
				return
			}
			twmw(c)
		})

		monitoring.StartClickHouseReporter(ctx)
		monitoring.StartBackendReporter(ctx)
		monitoring.StartTelemetryDBReporter(ctx)
		monitoring.StartOutboxReporter(ctx)
		monitoring.StartSyntheticsReporter(ctx)
		monitoring.StartSLOReporter(ctx)
	}

	router.GET("/health", func(c *gin.Context) {
		c.String(200, "OK")
	})

	apiRouterGroup := router.Group("/api")
	controllers.RegisterControllers(apiRouterGroup)

	router.GET("/version", func(ctx *gin.Context) {
		ctx.JSON(200, gin.H{"version": "0.0.1"})
	})

	wellKnown := []string{http.MethodGet, http.MethodOptions}
	router.Match(wellKnown, "/.well-known/oauth-authorization-server", middleware.WellKnownCors, controllers.WellKnownController.AuthorizationServer)
	router.Match(wellKnown, "/.well-known/oauth-protected-resource", middleware.WellKnownCors, controllers.WellKnownController.ProtectedResource)
	router.Match(wellKnown, "/.well-known/oauth-protected-resource"+mcpmount.Path, middleware.WellKnownCors, controllers.WellKnownController.ProtectedResourceMCP)
	router.Match(wellKnown, "/.well-known/oauth-protected-resource"+mcpmount.WritePath, middleware.WellKnownCors, controllers.WellKnownController.ProtectedResourceMCPWrite)

	mcpHandler := mcpmount.GinHandler(router, "0.0.1", false)
	mcpWriteHandler := mcpmount.GinHandler(router, "0.0.1", true)
	mcpMethods := []string{http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodOptions}
	router.Match(mcpMethods, mcpmount.Path, middleware.MCPCors, mcpHandler)
	router.Match(mcpMethods, mcpmount.Path+"/", middleware.MCPCors, mcpHandler)
	router.Match(mcpMethods, mcpmount.WritePath, middleware.MCPCors, mcpWriteHandler)
	router.Match(mcpMethods, mcpmount.WritePath+"/", middleware.MCPCors, mcpWriteHandler)

	apiOnly := cfg.APIOnly == "true"

	if apiOnly {
		router.NoRoute(func(c *gin.Context) {
			c.JSON(404, gin.H{"error": "Not found"})
		})
	} else {
		staticFS, err := static.GetStaticFS()
		if err != nil {
			config.Logf("Warning: Could not load static files: %v", err)
			staticFS = nil
		}

		if staticFS != nil {
			router.StaticFS("/assets", http.FS(mustSubFS(staticFS, "assets")))
			router.StaticFS("/_app", http.FS(mustSubFS(staticFS, "_app")))
			router.GET("/favicon.ico", serveStaticFile(staticFS, "favicon.ico"))
			router.GET("/robots.txt", serveStaticFile(staticFS, "robots.txt"))
		}

		router.NoRoute(createSPAHandler(staticFS))
	}

	ports := cfg.Ports
	if ports == "" {
		ports = "80,8082"
	}

	// Bind every port up front rather than inside the serving goroutines, so a
	// failure is known before the process reports itself started. Individual
	// failures are tolerated -- binding :80 as a non-root user is the ordinary
	// local-development case -- but losing every port is fatal.
	//
	// Previously only the first entry was bound on this goroutine and panicked,
	// while the rest panicked inside goroutines guarded by traceway.Recover(),
	// which swallows the panic (and reports nothing at all when monitoring is
	// unconfigured). That made the same failure either fatal or invisible
	// depending on the order of PORTS.
	var listeners []net.Listener
	for _, port := range parsePorts(ports) {
		addr := ":" + port
		listener, err := net.Listen("tcp", addr)
		if err != nil {
			config.Logln("WARNING: not listening on " + addr + ": " + err.Error())
			traceway.CaptureException(fmt.Errorf("could not listen on %s: %w", addr, err))
			continue
		}
		listeners = append(listeners, listener)
	}

	if len(listeners) == 0 {
		panic(fmt.Errorf("could not listen on any port in PORTS=%q", ports))
	}

	for _, listener := range listeners[1:] {
		go func(listener net.Listener) {
			config.Logln("Starting server on " + listener.Addr().String())
			serveHTTP(router, listener)
		}(listener)
	}

	notifySystemd(ctx)
	config.Logln("Starting server on " + listeners[0].Addr().String())
	serveHTTP(router, listeners[0])
}

func parsePorts(ports string) []string {
	var out []string
	for _, port := range strings.Split(ports, ",") {
		if port = strings.TrimSpace(port); port != "" {
			out = append(out, port)
		}
	}
	return out
}

func warnOnUntrustedForwardedFor(nets []*net.IPNet) gin.HandlerFunc {
	detect := newUntrustedForwarderDetector(nets)
	return func(c *gin.Context) {
		if peer, first := detect(c); first {
			config.Logf("X-Forwarded-For or X-Real-IP received from %s, which is not in TRUSTED_PROXIES. The header is ignored, so per-IP rate limits and session client IPs see that address instead of the real client. If %s is your proxy or CDN, add its range to TRUSTED_PROXIES.", peer, peer)
		}
		c.Next()
	}
}

func newUntrustedForwarderDetector(nets []*net.IPNet) func(*gin.Context) (string, bool) {
	var once sync.Once
	return func(c *gin.Context) (string, bool) {
		if c.GetHeader("X-Forwarded-For") == "" && c.GetHeader("X-Real-IP") == "" {
			return "", false
		}
		peer := c.RemoteIP()
		ip := net.ParseIP(peer)
		if ip == nil || containsIP(nets, ip) {
			return "", false
		}
		first := false
		once.Do(func() { first = true })
		return peer, first
	}
}

func trustedProxyNets(proxies []string) ([]*net.IPNet, error) {
	var nets []*net.IPNet
	for _, p := range proxies {
		if _, n, err := net.ParseCIDR(p); err == nil {
			nets = append(nets, n)
			continue
		}
		ip := net.ParseIP(p)
		if ip == nil {
			return nil, fmt.Errorf("%q is not an IP address or CIDR range", p)
		}
		bits := 32
		if ip.To4() == nil {
			bits = 128
		}
		nets = append(nets, &net.IPNet{IP: ip, Mask: net.CIDRMask(bits, bits)})
	}
	return nets, nil
}

func containsIP(nets []*net.IPNet, ip net.IP) bool {
	for _, n := range nets {
		if n.Contains(ip) {
			return true
		}
	}
	return false
}

const serverReadTimeout = time.Hour

// serveHTTP takes an already-bound listener rather than an address: the ports
// are bound up front so a bind failure is known before the process reports
// itself started, which ListenAndServe cannot express. The timeouts are the
// point of this helper and must survive that change -- gin's RunListener sets
// none of them.
func serveHTTP(router *gin.Engine, listener net.Listener) {
	srv := &http.Server{
		Handler:           router,
		ReadHeaderTimeout: 15 * time.Second,
		ReadTimeout:       serverReadTimeout,
		IdleTimeout:       2 * time.Minute,
	}
	if err := srv.Serve(listener); err != nil {
		panic(fmt.Errorf("server on %s stopped: %w", listener.Addr().String(), err))
	}
}

func configureClientIP(router *gin.Engine, cfg *config.Cfg) error {
	// Gin trusts X-Forwarded-For from any peer by default, which would let a
	// direct client pick its own ClientIP() and rotate out of the per-IP limits.
	if err := router.SetTrustedProxies(cfg.TrustedProxyList()); err != nil {
		return err
	}
	// Taken verbatim as the client IP when present, bypassing the proxy list —
	// only safe when every path to the backend runs through a proxy that
	// overwrites the header (e.g. ingress-nginx X-Real-IP, CF-Connecting-IP).
	router.TrustedPlatform = cfg.TrustedProxyHeader
	return nil
}

// applyEnvOverrides forwards env vars to a config the embedded Run(opts...)
// path built without LoadFromEnv; new Cfg fields belong in this table.
func applyEnvOverrides(cfg *config.Cfg) {
	for _, m := range []struct {
		envVar string
		dest   *string
	}{
		{"SMTP_ENABLED", &cfg.SMTPEnabled},
		{"SMTP_HOST", &cfg.SMTPHost},
		{"SMTP_PORT", &cfg.SMTPPort},
		{"SMTP_USERNAME", &cfg.SMTPUsername},
		{"SMTP_PASSWORD", &cfg.SMTPPassword},
		{"SMTP_FROM", &cfg.SMTPFrom},
		{"ONCALL_POLL_SECONDS", &cfg.OncallPollSeconds},
		{"OUTBOX_POLL_SECONDS", &cfg.OutboxPollSeconds},
		{"SLO_POLL_SECONDS", &cfg.SLOPollSeconds},
		{"SLO_COMPUTE_WORKERS", &cfg.SLOComputeWorkers},
		{"SLO_COMPUTE_TIMEOUT_SECONDS", &cfg.SLOComputeTimeoutSecs},
		{"SYNTHETICS_POLL_SECONDS", &cfg.SyntheticsPollSeconds},
		{"SYNTHETICS_BROWSER_MODE", &cfg.SyntheticsBrowserMode},
		{"SYNTHETICS_BROWSER_SANDBOX", &cfg.SyntheticsBrowserSandbox},
		{"SYNTHETICS_HTTP_CONCURRENCY", &cfg.SyntheticsHTTPConcurrency},
		{"SYNTHETICS_BROWSER_CONCURRENCY", &cfg.SyntheticsBrowserConcurrency},
		{"SYNTHETICS_ALLOW_PRIVATE_TARGETS", &cfg.SyntheticsAllowPrivateTargets},
		{"SYNTHETICS_PLAYWRIGHT_DIR", &cfg.SyntheticsPlaywrightDir},
		{"SYNTHETICS_SCREENSHOT_RETENTION_DAYS", &cfg.SyntheticsScreenshotRetentionDays},
		{"SYNTHETICS_RUNNER_SECRET", &cfg.SyntheticsRunnerSecret},
		{"HEALTH_DEEP_TOKEN", &cfg.HealthDeepToken},
		{"TRUSTED_PROXIES", &cfg.TrustedProxies},
		{"TRUSTED_PROXY_HEADER", &cfg.TrustedProxyHeader},
		{"TWILIO_ACCOUNT_SID", &cfg.TwilioAccountSID},
		{"TWILIO_AUTH_TOKEN", &cfg.TwilioAuthToken},
		{"TWILIO_FROM_NUMBER", &cfg.TwilioFromNumber},
		{"TWILIO_MESSAGING_SERVICE_SID", &cfg.TwilioMessagingServiceSID},
		{"ALLOW_PRIVATE_NOTIFICATION_TARGETS", &cfg.AllowPrivateNotificationTargets},
		{"REPORT_MAX_BODY_MB", &cfg.ReportMaxBodyMB},
		{"SQLITE_CACHE_SIZE_MB", &cfg.SQLiteCacheSizeMB},
		{"DUCKDB_MEMORY_LIMIT", &cfg.DuckDBMemoryLimit},
		{"DUCKDB_THREADS", &cfg.DuckDBThreads},
		{"DUCKDB_CHECKPOINT_THRESHOLD", &cfg.DuckDBCheckpointThreshold},
		{"DUCKDB_RETENTION_DAYS", &cfg.DuckDBRetentionDays},
		{"OAUTH_SESSION_SECRET", &cfg.OAuthSessionSecret},
		{"GOOGLE_CLIENT_ID", &cfg.GoogleClientID},
		{"GOOGLE_CLIENT_SECRET", &cfg.GoogleClientSecret},
		{"GITHUB_CLIENT_ID", &cfg.GitHubClientID},
		{"GITHUB_CLIENT_SECRET", &cfg.GitHubClientSecret},
		{"OIDC_CLIENT_ID", &cfg.OIDCClientID},
		{"OIDC_CLIENT_SECRET", &cfg.OIDCClientSecret},
		{"OIDC_DISCOVERY_URL", &cfg.OIDCDiscoveryURL},
		{"OIDC_DISPLAY_NAME", &cfg.OIDCDisplayName},
		{"OIDC_AUTO_CREATE_USERS", &cfg.OIDCAutoCreateUsers},
		{"OIDC_ORG_CLAIM", &cfg.OIDCOrgClaim},
		{"OIDC_EXTRA_SCOPES", &cfg.OIDCExtraScopes},
		{"OIDC_ROLE_CLAIM", &cfg.OIDCRoleClaim},
		{"OIDC_ROLE_MAP", &cfg.OIDCRoleMap},
		{"OIDC_AUTH_URL", &cfg.OIDCAuthURL},
		{"OIDC_TOKEN_URL", &cfg.OIDCTokenURL},
		{"OIDC_USER_INFO_URL", &cfg.OIDCUserInfoURL},
		{"DISABLE_PASSWORD_LOGIN", &cfg.DisablePasswordLogin},
		{"NOTIFICATION_POLL_SECONDS", &cfg.NotificationPollSeconds},
		{"SOURCEMAP_CACHE_MAX_ENTRIES", &cfg.SourceMapCacheMaxEntries},
		{"SOURCEMAP_CACHE_MAX_BYTES_MB", &cfg.SourceMapCacheMaxBytesMB},
		{"SOURCEMAP_CACHE_TYPE", &cfg.SourceMapCacheType},
		{"SOURCEMAP_DISK_CACHE_PATH", &cfg.SourceMapDiskCachePath},
		{"SOURCEMAP_DISK_CACHE_MAX_MB", &cfg.SourceMapDiskCacheMaxMB},
		{"SYMBOLICATOR_PARSER", &cfg.SymbolicatorParser},
	} {
		if v := os.Getenv(m.envVar); v != "" {
			*m.dest = v
		}
	}
}

func parsePositiveInt(s string, def int) int {
	s = strings.TrimSpace(s)
	if s == "" {
		return def
	}
	v, err := strconv.Atoi(s)
	if err != nil || v < 1 {
		return def
	}
	return v
}

func mustSubFS(fsys fs.FS, dir string) fs.FS {
	sub, err := fs.Sub(fsys, dir)
	if err != nil {
		return emptyFS{}
	}
	return sub
}

type emptyFS struct{}

func (emptyFS) Open(name string) (fs.File, error) {
	return nil, fs.ErrNotExist
}

func serveStaticFile(staticFS fs.FS, filename string) gin.HandlerFunc {
	return func(c *gin.Context) {
		data, err := fs.ReadFile(staticFS, filename)
		if err != nil {
			c.Status(404)
			return
		}
		contentType := "application/octet-stream"
		if strings.HasSuffix(filename, ".ico") {
			contentType = "image/x-icon"
		} else if strings.HasSuffix(filename, ".txt") {
			contentType = "text/plain"
		}
		c.Data(200, contentType, data)
	}
}

func createSPAHandler(staticFS fs.FS) gin.HandlerFunc {
	return func(c *gin.Context) {
		path := c.Request.URL.Path
		accept := c.GetHeader("Accept")

		if strings.HasPrefix(path, "/api") {
			c.JSON(404, gin.H{"error": "Not found"})
			return
		}

		if strings.Contains(accept, "application/json") &&
			!strings.Contains(accept, "text/html") &&
			!strings.Contains(accept, "*/*") {
			c.JSON(404, gin.H{"error": "Not found"})
			return
		}

		if staticFS == nil {
			c.JSON(404, gin.H{"error": "Not found"})
			return
		}

		cleanPath := strings.TrimPrefix(path, "/")
		isPage := cleanPath == "" || strings.HasSuffix(cleanPath, ".html")
		if !isPage {
			if data, err := fs.ReadFile(staticFS, cleanPath); err == nil {
				c.Data(200, detectContentType(cleanPath), data)
				return
			}
		}

		statusPage, err := controllers.StatusPageForHost(c.Request.Host)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, traceway.NewStackTraceErrorf("failed to resolve status page host: %w", err))
			return
		}

		if statusPage == nil && cleanPath != "" && isPage {
			if data, err := fs.ReadFile(staticFS, cleanPath); err == nil {
				c.Data(200, detectContentType(cleanPath), data)
				return
			}
		}

		if statusPage != nil && path != "/" {
			c.Redirect(http.StatusFound, "/")
			return
		}

		indexData, err := fs.ReadFile(staticFS, "index.html")
		if err != nil {
			c.JSON(404, gin.H{"error": "Not found"})
			return
		}
		if statusPage != nil {
			title := html.EscapeString(statusPage.Name + " status")
			head := `<meta name="traceway-status-slug" content="` + html.EscapeString(statusPage.Slug) + `">` +
				`<meta property="og:title" content="` + title + `">`
			if statusPage.Description != "" {
				description := html.EscapeString(statusPage.Description)
				head += `<meta name="description" content="` + description + `">` +
					`<meta property="og:description" content="` + description + `">`
			}
			indexData = bytes.Replace(indexData, []byte("<title>Traceway</title>"), []byte("<title>"+title+"</title>"), 1)
			indexData = bytes.Replace(indexData, []byte("</head>"), []byte(head+"</head>"), 1)
			c.Header("Content-Security-Policy", "base-uri 'self'; form-action 'self'")
			c.Writer.Header().Del("X-Frame-Options")
		}
		c.Data(200, "text/html; charset=utf-8", indexData)
	}
}

func detectContentType(filename string) string {
	switch {
	case strings.HasSuffix(filename, ".html"):
		return "text/html; charset=utf-8"
	case strings.HasSuffix(filename, ".css"):
		return "text/css; charset=utf-8"
	case strings.HasSuffix(filename, ".js"):
		return "application/javascript; charset=utf-8"
	case strings.HasSuffix(filename, ".json"):
		return "application/json"
	case strings.HasSuffix(filename, ".png"):
		return "image/png"
	case strings.HasSuffix(filename, ".jpg"), strings.HasSuffix(filename, ".jpeg"):
		return "image/jpeg"
	case strings.HasSuffix(filename, ".svg"):
		return "image/svg+xml"
	case strings.HasSuffix(filename, ".ico"):
		return "image/x-icon"
	case strings.HasSuffix(filename, ".woff"):
		return "font/woff"
	case strings.HasSuffix(filename, ".woff2"):
		return "font/woff2"
	default:
		return "application/octet-stream"
	}
}