Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
publicDir string = "./public"
port int = 8081
defaultPort int = 8081
envFlagAllowCreate string = "GULL_ALLOW_CREATE"
)

Expand All @@ -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")
Expand All @@ -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))
}