package main import ( "fmt" "log" "net/http" "os" "time" ) const ( readTimeout = 30 * time.Second ) func main() { port, hasPort := os.LookupEnv("PORT") if !hasPort { port = "8080" } r := http.NewServeMux() r.Handle("media.lmika.org/", redirectTo("https://pub-d9610a73a30e4060aaf007864420dd0f.r2.dev")) r.Handle("folio.red/", redirectToJust("https://lmika.org/categories/old-projects/")) r.Handle("workpad.dev/", redirectToJust("https://lmika.org/categories/workpad/")) svr := http.Server{ Addr: fmt.Sprintf(":%v", port), Handler: r, ReadTimeout: readTimeout, ReadHeaderTimeout: readTimeout, } if err := svr.ListenAndServe(); err != nil { log.Fatal(err) } } func redirectTo(url string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { newURL := url + r.URL.Path http.Redirect(w, r, newURL, http.StatusFound) }) } func redirectToJust(url string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { newURL := url http.Redirect(w, r, newURL, http.StatusMovedPermanently) }) }