Skip to content

Commit

Permalink
feat: introduce OMNIVORE_AUTH_TOKEN_FILEPATH to be able to read the s…
Browse files Browse the repository at this point in the history
…ecret from a file. Refactor config setup
  • Loading branch information
rruizt committed Apr 10, 2024
1 parent e4e29eb commit dd43707
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/omnivore-as-rss/omnivore-as-rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package main
import "omnivore-as-rss/internal"

func main() {
internal.InitConfig()
internal.Serve()
}
54 changes: 54 additions & 0 deletions internal/config.go
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")
}
3 changes: 1 addition & 2 deletions internal/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package internal
import (
"context"
"net/http"
"os"
"time"
"github.com/hasura/go-graphql-client"
)
Expand All @@ -13,7 +12,7 @@ var client *graphql.Client
func queryOmnivore() error {
if client == nil {
client = graphql.NewClient("https://api-prod.omnivore.app/api/graphql", nil).WithRequestModifier(func(r *http.Request) {
r.Header.Set("Authorization", os.Getenv("OMNIVORE_AUTH_TOKEN"))
r.Header.Set("Authorization", Cfg.OmnivoreAuthToken)
})
}

Expand Down

0 comments on commit dd43707

Please sign in to comment.