rt-redirect/main.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-08-06 12:06:50 +00:00
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"
}
2024-12-07 02:47:13 +00:00
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/"))
2024-12-25 20:50:52 +00:00
r.Handle("workpad.dev/", redirectToJust("https://lmika.org/categories/workpad/"))
2023-08-06 12:06:50 +00:00
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) {
2024-12-07 02:47:13 +00:00
newURL := url + r.URL.Path
http.Redirect(w, r, newURL, http.StatusFound)
2023-08-06 12:06:50 +00:00
})
}
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)
})
}