Skip to content

Commit

Permalink
Support URL_PATH variable to allow setting path for 200 server
Browse files Browse the repository at this point in the history
  • Loading branch information
bencooper222 committed Sep 14, 2023
1 parent ad15a73 commit f7e5935
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
)

func main() {
Expand All @@ -14,6 +15,15 @@ func main() {
if portStr == "" {
log.Fatal("PORT env var must be set")
}

urlPathStr := os.Getenv("URL_PATH")
if urlPathStr == "" {
urlPathStr = "/"
log.Println("URL_PATH env var not set, using default '/'")
}
if !strings.HasPrefix(urlPathStr, "/") {
log.Fatal("URL_PATH env var must start with '/'")
}
// we just convert to an int to check it is a number
// we use portStr below since that's what the http package wnats
_, err := strconv.Atoi(portStr)
Expand All @@ -22,7 +32,7 @@ func main() {
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if r.URL.Path == urlPathStr {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
Expand Down

0 comments on commit f7e5935

Please sign in to comment.