package main import ( "context" "embed" "flag" "net/http" "os" "os/signal" "time" httputils "git.mstar.dev/mstar/goutils/http" "git.mstar.dev/mstar/goutils/other" "github.com/glebarez/sqlite" "github.com/rs/zerolog/log" "gorm.io/gorm" "git.mstar.dev/mstar/mstar-webserver/storage/generated" "git.mstar.dev/mstar/mstar-webserver/storage/models" ) const HTML_PREFIX = "" //go:embed templates var embed_templates embed.FS //go:embed static var embed_static embed.FS //go:embed well-known var embed_well_known embed.FS // go: embed robots.txt var embed_robots_txt string // go: embed humans.txt var embed_humans_txt string var redirectMapping = map[string]string{ "/twitch": "https://twitch.tv/mstarontwitch", "/bsky": "https://bsky.app/profile/mstar.dev", "/bluesky": "https://bsky.app/profile/mstar.dev", "/git": "https://git.mstar.dev", "/gitlab": "https://gitlab.com/mstarongitlab", "/github": "https://github.com/mstarongithub", "/fedi": "https://www.youtube.com/watch?v=dQw4w9WgXcQ", "/youtube": "https://www.youtube.com/channel/UC-LklY_wt004i_30xcVohbg", } func main() { other.SetupFlags() portFlag := flag.String("port", "8080", "Set the port. Overwrites env vars") flag.Parse() other.ConfigureLogging(nil) db, err := gorm.Open(sqlite.Open("./db.sqlite"), &gorm.Config{}) if err != nil { log.Fatal().Err(err).Msg("Failed to open db file") } generated.SetDefault(db) if err := db.AutoMigrate(&models.Link{}); err != nil { log.Fatal().Err(err).Msg("Failed to automigrate db setup") } mux := http.NewServeMux() mux.HandleFunc("/{$}", handleRoot) // Catchall that just redirects to Hetzner's 10GB speedtest file // TODO: Maybe swap that for a forced zip bomb? mux.Handle( "/", http.RedirectHandler("https://hil-speed.hetzner.com/10GB.bin", http.StatusMovedPermanently), ) for k, v := range redirectMapping { mux.Handle(k, countingRedirect(v)) } mux.HandleFunc("/cat/awawawa", awawaStream) // static files in /static mux.Handle("/static/", http.FileServer(http.FS(embed_static))) // .well-known from /well-known mux.Handle( "/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.FS(embed_well_known))), ) // Static files not in /static or /.well-known mux.HandleFunc("/robots.txt", buildHTTPFileReader(embed_robots_txt)) mux.HandleFunc("/humans.txt", buildHTTPFileReader(embed_humans_txt)) server := http.Server{ Addr: ":" + *portFlag, Handler: httputils.ChainMiddlewares(mux, httputils.LoggingMiddleware), } exitChan := make(chan os.Signal, 1) signal.Notify(exitChan, os.Interrupt) go func() { log.Info().Str("addr", server.Addr).Msg("Starting server") if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { log.Fatal().Err(err).Msg("Server exited unexpectedly") } }() <-exitChan log.Info().Msg("Shutting down server due to Interrupt") ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) defer cancel() err = server.Shutdown(ctx) if err != nil { log.Fatal().Err(err).Msg("Failed to cleanly shut down") } log.Info().Msg("Clean shutdown complete") }