Skip to content

Commit

Permalink
feat: make listening port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rruizt committed Apr 21, 2024
1 parent 06bdd9f commit 5b2e5d3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |



48 changes: 41 additions & 7 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5b2e5d3

Please sign in to comment.