From e9206ab9151edf9aac2f038cefe243c684d629c7 Mon Sep 17 00:00:00 2001 From: webbrain-one <295484252+webbrain-one@users.noreply.github.com> Date: Sun, 2 Aug 2026 22:11:42 +0300 Subject: [PATCH] Allow custom server port via environment variable Replaces the hardcoded 8081 port with a configurable environment variable. Defaults to 8081 if not set. Fixes #2 --- server.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/server.go b/server.go index df571f0..b046188 100644 --- a/server.go +++ b/server.go @@ -11,7 +11,7 @@ import ( const ( publicDir string = "./public" - port int = 8081 + defaultPort int = 8081 envFlagAllowCreate string = "GULL_ALLOW_CREATE" ) @@ -26,6 +26,14 @@ func main() { a.Initialize("sqlite3", "./data/data.db") defer a.DB.Close() + port := os.Getenv("PORT") + portNum := defaultPort + if port != "" { + if p, err := strconv.Atoi(port); err == nil { + portNum = p + } + } + r := mux.NewRouter() if allowCreate { r.HandleFunc("/", a.CreateShortURL).Methods("POST") @@ -35,6 +43,6 @@ func main() { r.PathPrefix("/").Handler(http.FileServer(http.Dir(publicDir))).Methods("GET") http.Handle("/", r) - log.Println("Server is listening on port", port) - log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil)) + log.Println("Server is listening on port", portNum) + log.Fatal(http.ListenAndServe(":"+strconv.Itoa(portNum), nil)) }