-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce OMNIVORE_AUTH_TOKEN_FILEPATH to be able to read the s…
…ecret from a file. Refactor config setup
- Loading branch information
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ package main | |
import "omnivore-as-rss/internal" | ||
|
||
func main() { | ||
internal.InitConfig() | ||
internal.Serve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package internal | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var Cfg Config | ||
|
||
type Config struct { | ||
OmnivoreAuthToken string | ||
} | ||
|
||
func InitConfig() { | ||
omnivoreToken , err := getOmnivoreToken() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
c := Config { | ||
OmnivoreAuthToken: omnivoreToken, | ||
} | ||
|
||
Cfg = c | ||
} | ||
|
||
func getOmnivoreToken() (string, error) { | ||
secretEnv := os.Getenv("OMNIVORE_AUTH_TOKEN") | ||
secretFilePath := os.Getenv("OMNIVORE_AUTH_TOKEN_FILEPATH") | ||
|
||
if secretFilePath != "" { | ||
log.Println("Reading secret from file") | ||
dat, err := os.ReadFile(secretFilePath) | ||
if err != nil { | ||
log.Println(err) | ||
return "", errors.New("unable to read Omnivore secret file") | ||
} | ||
|
||
token := string(dat) | ||
// Clean up secret string to avoid errors | ||
token = strings.Replace(token, " ", "", -1) | ||
token = strings.Replace(token, "\t", "", -1) | ||
token = strings.Replace(token, "\n", "", -1) | ||
|
||
return token, nil | ||
} else if secretEnv != "" { | ||
log.Println("Reading secret from env var") | ||
return secretEnv, nil | ||
} | ||
|
||
return "", errors.New("omnivore authentication is needed for this service to work, please set environment variable or secret file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters