Skip to content

Instantly share code, notes, and snippets.

@gregtap
Created September 26, 2019 16:45
Show Gist options
  • Save gregtap/27f25ea4d52a8d7c12787f9025ff9ffc to your computer and use it in GitHub Desktop.
Save gregtap/27f25ea4d52a8d7c12787f9025ff9ffc to your computer and use it in GitHub Desktop.
dumb redirect
package main
import (
"fmt"
"net/http"
"os"
)
var targetDomain = fmt.Sprintf("%s", getEnv("TARGET_DOMAIN", "https://google.com"))
func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, targetDomain, http.StatusMovedPermanently)
}
func main() {
port := fmt.Sprintf(":%s", getEnv("PORT", "8000"))
http.HandleFunc("/", redirect)
fmt.Println("🤤 301 dumb redirect server")
fmt.Println("Redirects to %s", targetDomain)
fmt.Println("Listening on port %s...", port)
http.ListenAndServe(port, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment