From 5b2e5d329cd765f1f7fcc46d33bb8fe81833ce14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Ruiz?= Date: Sun, 21 Apr 2024 16:15:40 +0200 Subject: [PATCH] feat: make listening port configurable --- README.md | 1 + internal/config.go | 48 +++++++++++++++++++++++++++++++++++++++------- internal/server.go | 5 +++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8d4c783..99b7282 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ You can configure `omnivore-as-rss` with environment variables or command line f | ------------- | ------------- |-----------| | OMNIVORE_AUTH_TOKEN / -t | The API token from Omnivore | `00000000-0000-0000-0000-000000000000` | | OMNIVORE_AUTH_TOKEN_FILEPATH / -tf | The filepath of the file containing the API token from Omnivore | `/run/secrets/omnivore` | +| PORT / -p | The port where the application is going to listen (defaults to 8090) | `8090` | diff --git a/internal/config.go b/internal/config.go index 13ea6f4..4c7799e 100644 --- a/internal/config.go +++ b/internal/config.go @@ -12,42 +12,76 @@ var Cfg Config type Config struct { OmnivoreAuthToken string + LocalPort string } func InitConfig() { - omnivoreToken , err := getOmnivoreToken() + flagMap := extractFlags() + + omnivoreToken , err := getOmnivoreToken(flagMap) + localPort := getPort(flagMap) + if err != nil { log.Fatal(err) } c := Config { OmnivoreAuthToken: omnivoreToken, + LocalPort: localPort, } Cfg = c } -func getOmnivoreToken() (string, error) { - var omnivoreTokenFlag string - var secretFilePathFlag string +func extractFlags() map[string]string { + flagMap := map[string] string {} - flag.StringVar(&omnivoreTokenFlag, "t", "" , "the Omnivore API token") - flag.StringVar(&secretFilePathFlag, "tf", "" , "the path to the file with the Omnivore API Token") + var omnivoreToken string + var secretFilePath string + var port string + flag.StringVar(&omnivoreToken, "t", "" , "the Omnivore API token") + flag.StringVar(&secretFilePath, "tf", "" , "the path to the file with the Omnivore API Token") + flag.StringVar(&port, "p", "" , "the port where the service is going to listen") flag.Parse() + flagMap["t"] = omnivoreToken + flagMap["tf"] = secretFilePath + flagMap["p"] = port + + return flagMap +} + +func getPort(flagMap map[string] string) string { + port := flagMap["p"] + + if port == "" { + port = os.Getenv("PORT") + if port == "" { + port = "8090" + } + } + + return port +} + +func getOmnivoreToken(flagMap map[string]string) (string, error) { + + omnivoreTokenFlag := flagMap["t"] + omnivoreToken := os.Getenv("OMNIVORE_AUTH_TOKEN") if omnivoreToken == "" { omnivoreToken = omnivoreTokenFlag } + secretFilePathFlag := flagMap["tf"] secretFilePath := os.Getenv("OMNIVORE_AUTH_TOKEN_FILEPATH") if secretFilePath == "" { secretFilePath = secretFilePathFlag } if omnivoreToken != "" { - log.Println("Reading secret from env var") + log.Println("Reading secret from env var or flag") return omnivoreToken, nil } else if secretFilePath != "" { log.Println("Reading secret from file") diff --git a/internal/server.go b/internal/server.go index bc1b87d..5d4501f 100644 --- a/internal/server.go +++ b/internal/server.go @@ -9,8 +9,9 @@ import ( func Serve() { http.HandleFunc("/rss", rss) - log.Println("Starting web server: listening at port 8090") - http.ListenAndServe(":8090", nil) + localPort := Cfg.LocalPort + log.Println("Starting web server: listening at port ", localPort) + http.ListenAndServe(":" + localPort, nil) } func rss(w http.ResponseWriter, req *http.Request) {