diff --git a/.env.example b/.env.example index 75a0c00..5d0a467 100644 --- a/.env.example +++ b/.env.example @@ -12,8 +12,9 @@ REDIRECT_URI=http://localhost:8081/api/auth/callback CLIENT_ID= CLIENT_SECRET= -# Listening port -PORT= +# Listen address (if the IP address is empty, it will default to 127.0.0.1) +# e.g. 127.0.0.1:8081 or :8081 +LISTEN_ADDRESS=:8081 # S3 S3_ENDPOINT= diff --git a/main.go b/main.go index f9b08dd..b0006bc 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ import ( "net/http" "os" "strconv" + "strings" "github.com/gin-gonic/gin" "github.com/joho/godotenv" @@ -61,7 +62,7 @@ func main() { databaseType := getEnv("DATABASE_TYPE", "sqlite") sqliteFile := getEnv("SQLITE_FILE", "stereo.db") boxyApiUrl := getEnv("BOXY_URL", "") - port := getEnv("PORT", "8081") + listenAddr := getEnv("LISTEN_ADDRESS", ":8081") imagePath := getEnv("IMAGE_PATH", os.TempDir()) @@ -134,6 +135,10 @@ func main() { boxyClientSecret := requireEnv("BOXY_CLIENT_SECRET") boxyClientAddress := getEnv("BOXY_CLIENT_ADDRESS", "") + port, err := strconv.Atoi(strings.Split(listenAddr, ":")[1]) + + fmt.Printf("Using port %d\n", port) + rawBody := map[string]any{ "port": port, "hostname": c.Domain, @@ -152,9 +157,9 @@ func main() { req.SetBasicAuth(boxyClientName, boxyClientSecret) - http.DefaultClient.Do(req) + resp, _ := http.DefaultClient.Do(req) - if req.Response.StatusCode != 200 { + if resp.StatusCode != 200 { log.Fatal("Could not register with Boxy.\nStatus Code: " + strconv.Itoa(req.Response.StatusCode)) return } @@ -163,6 +168,6 @@ func main() { } api.Register(&c) - fmt.Printf("Running on port %s\n", port) - c.Router.Run() + fmt.Printf("Listening on %s\n", listenAddr) + c.Router.Run(listenAddr) }