golang.org/x/playground@v0.0.0-20230418134305-14ebe15bcd59/cmd/redirect/main.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // redirect serves an http server that redirects to the URL specified by the
     6  // environment variable PLAY_REDIRECT.
     7  package main
     8  
     9  import (
    10  	"log"
    11  	"net/http"
    12  	"os"
    13  )
    14  
    15  func main() {
    16  	port := os.Getenv("PORT")
    17  	if port == "" {
    18  		port = "8080"
    19  	}
    20  
    21  	redirect := os.Getenv("PLAY_REDIRECT")
    22  	if redirect == "" {
    23  		redirect = "https://play.golang.org"
    24  	}
    25  
    26  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    27  		http.Redirect(w, r, redirect+r.URL.Path, http.StatusFound)
    28  	})
    29  
    30  	log.Printf("Listening on :%v ...", port)
    31  	log.Fatalf("Error listening on :%v: %v", port, http.ListenAndServe(":"+port, handler))
    32  }