Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix envvars recognition #17

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func serve(ctx context.Context) error {
so := serveropts.NewServerOptions(serverOpts, k.String("config"))

// pass the logger options to the job queue
so.Config.Settings.JobQueue.Logger.Debug = k.Bool(debugFlag)
so.Config.Settings.JobQueue.Logger.Pretty = k.Bool(prettyFlag)
so.Config.Settings.River.Logger.Debug = k.Bool(debugFlag)
so.Config.Settings.River.Logger.Pretty = k.Bool(prettyFlag)

return river.Start(ctx, so.Config.Settings.JobQueue)
return river.Start(ctx, so.Config.Settings.River)
}
20 changes: 10 additions & 10 deletions config/.env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
RIVERBOAT_REFRESHINTERVAL="10m"
RIVERBOAT_JOBQUEUE_DATABASEHOST="postgres://postgres:[email protected]:5432/jobs?sslmode=disable"
RIVERBOAT_JOBQUEUE_QUEUES=""
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_DEVMODE="true"
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_TESTDIR="fixtures/email"
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_TOKEN=""
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_FROMEMAIL="[email protected]"
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_ENABLED="true"
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_BASEURL="http://localhost:1337"
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_ENDPOINT="query"
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_DEBUG="false"
RIVERBOAT_RIVER_DATABASEHOST="postgres://postgres:[email protected]:5432/jobs?sslmode=disable"
RIVERBOAT_RIVER_QUEUES=""
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_DEVMODE="true"
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_TESTDIR="fixtures/email"
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_TOKEN=""
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_FROMEMAIL="[email protected]"
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_ENABLED="true"
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_BASEURL="http://localhost:1337"
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_ENDPOINT="query"
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_DEBUG="false"
6 changes: 3 additions & 3 deletions config/config.example.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jobQueue:
refreshInterval: 600000000000
river:
databaseHost: postgres://postgres:[email protected]:5432/jobs?sslmode=disable
queues: null
workers:
Expand All @@ -9,9 +10,8 @@ jobQueue:
enabled: true
endpoint: query
emailWorker:
email:
config:
devMode: true
fromEmail: [email protected]
testDir: fixtures/email
token: ""
refreshInterval: 600000000000
28 changes: 16 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config

import (
"fmt"
"strings"
"time"

Expand All @@ -23,8 +22,8 @@ var (
type Config struct {
// RefreshInterval determines how often to reload the config
RefreshInterval time.Duration `json:"refreshInterval" koanf:"refreshInterval" default:"10m"`
// JobQueue is the configuration for the job queue
JobQueue river.Config `koanf:"jobQueue" json:"jobQueue"`
// River is the configuration for the job queue
River river.Config `koanf:"river" json:"river"`
}

// Load is responsible for loading the configuration from a YAML file and environment variables.
Expand All @@ -44,18 +43,23 @@ func Load(cfgFile *string) (*Config, error) {

// parse yaml config
if err := k.Load(file.Provider(*cfgFile), yaml.Parser()); err != nil {
fmt.Println("failed to load config file", err)
} else {
// unmarshal the config
if err := k.Unmarshal("", &conf); err != nil {
panic(err)
}
panic(err)
}

// unmarshal the config
if err := k.Unmarshal("", &conf); err != nil {
panic(err)
}

// load env vars
if err := k.Load(env.Provider(envPrefix, ".", func(s string) string {
return strings.ReplaceAll(strings.ToLower(
strings.TrimPrefix(s, envPrefix)), "_", ".")
if err := k.Load(env.ProviderWithValue(envPrefix, ".", func(s string, v string) (string, interface{}) {
key := strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(s, envPrefix)), "_", ".")

if strings.Contains(v, ",") {
return key, strings.Split(v, ",")
}

return key, v
}), nil); err != nil {
panic(err)
}
Expand Down
20 changes: 10 additions & 10 deletions config/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ metadata:
{{- end }}
data:
RIVERBOAT_REFRESHINTERVAL: {{ .Values.riverboat.refreshInterval | default "10m" }}
RIVERBOAT_JOBQUEUE_DATABASEHOST: {{ .Values.riverboat.jobQueue.databaseHost | default "postgres://postgres:[email protected]:5432/jobs?sslmode=disable" }}
RIVERBOAT_JOBQUEUE_QUEUES: {{ .Values.riverboat.jobQueue.queues }}
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_DEVMODE: {{ .Values.riverboat.jobQueue.workers.emailWorker.devMode | default true }}
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_TESTDIR: {{ .Values.riverboat.jobQueue.workers.emailWorker.testDir | default "fixtures/email" }}
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_TOKEN: {{ .Values.riverboat.jobQueue.workers.emailWorker.token }}
RIVERBOAT_JOBQUEUE_WORKERS_EMAILWORKER_FROMEMAIL: {{ .Values.riverboat.jobQueue.workers.emailWorker.fromEmail | default "[email protected]" }}
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_ENABLED: {{ .Values.riverboat.jobQueue.workers.databaseWorker.config.enabled | default true }}
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_BASEURL: {{ .Values.riverboat.jobQueue.workers.databaseWorker.config.baseUrl | default "http://localhost:1337" }}
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_ENDPOINT: {{ .Values.riverboat.jobQueue.workers.databaseWorker.config.endpoint | default "query" }}
RIVERBOAT_JOBQUEUE_WORKERS_DATABASEWORKER_CONFIG_DEBUG: {{ .Values.riverboat.jobQueue.workers.databaseWorker.config.debug | default false }}
RIVERBOAT_RIVER_DATABASEHOST: {{ .Values.riverboat.river.databaseHost | default "postgres://postgres:[email protected]:5432/jobs?sslmode=disable" }}
RIVERBOAT_RIVER_QUEUES: {{ .Values.riverboat.river.queues }}
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_DEVMODE: {{ .Values.riverboat.river.workers.emailWorker.config.devMode | default true }}
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_TESTDIR: {{ .Values.riverboat.river.workers.emailWorker.config.testDir | default "fixtures/email" }}
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_TOKEN: {{ .Values.riverboat.river.workers.emailWorker.config.token }}
RIVERBOAT_RIVER_WORKERS_EMAILWORKER_CONFIG_FROMEMAIL: {{ .Values.riverboat.river.workers.emailWorker.config.fromEmail | default "[email protected]" }}
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_ENABLED: {{ .Values.riverboat.river.workers.databaseWorker.config.enabled | default true }}
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_BASEURL: {{ .Values.riverboat.river.workers.databaseWorker.config.baseUrl | default "http://localhost:1337" }}
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_ENDPOINT: {{ .Values.riverboat.river.workers.databaseWorker.config.endpoint | default "query" }}
RIVERBOAT_RIVER_WORKERS_DATABASEWORKER_CONFIG_DEBUG: {{ .Values.riverboat.river.workers.databaseWorker.config.debug | default false }}
42 changes: 21 additions & 21 deletions configgen/api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
|Name|Type|Description|Required|
|----|----|-----------|--------|
|**refreshInterval**|`integer`|||
|[**jobQueue**](#jobqueue)|`object`|Config is the configuration for the river server<br/>||
|[**river**](#river)|`object`|Config is the configuration for the river server<br/>||

**Additional Properties:** not allowed
<a name="jobqueue"></a>
## jobQueue: object
<a name="river"></a>
## river: object

Config is the configuration for the river server

Expand All @@ -19,17 +19,17 @@ Config is the configuration for the river server
|Name|Type|Description|Required|
|----|----|-----------|--------|
|**databaseHost**|`string`|DatabaseHost for connecting to the postgres database<br/>||
|[**queues**](#jobqueuequeues)|`array`|||
|[**workers**](#jobqueueworkers)|`object`|Workers that will be enabled on the server<br/>||
|[**queues**](#riverqueues)|`array`|||
|[**workers**](#riverworkers)|`object`|Workers that will be enabled on the server<br/>||

**Additional Properties:** not allowed
<a name="jobqueuequeues"></a>
### jobQueue\.queues: array
<a name="riverqueues"></a>
### river\.queues: array

**Items**

<a name="jobqueueworkers"></a>
### jobQueue\.workers: object
<a name="riverworkers"></a>
### river\.workers: object

Workers that will be enabled on the server

Expand All @@ -38,22 +38,22 @@ Workers that will be enabled on the server

|Name|Type|Description|Required|
|----|----|-----------|--------|
|[**emailWorker**](#jobqueueworkersemailworker)|`object`|||
|[**databaseWorker**](#jobqueueworkersdatabaseworker)|`object`|||
|[**emailWorker**](#riverworkersemailworker)|`object`|||
|[**databaseWorker**](#riverworkersdatabaseworker)|`object`|||

**Additional Properties:** not allowed
<a name="jobqueueworkersemailworker"></a>
#### jobQueue\.workers\.emailWorker: object
<a name="riverworkersemailworker"></a>
#### river\.workers\.emailWorker: object

**Properties**

|Name|Type|Description|Required|
|----|----|-----------|--------|
|[**email**](#jobqueueworkersemailworkeremail)|`object`|||
|[**config**](#riverworkersemailworkerconfig)|`object`|||

**Additional Properties:** not allowed
<a name="jobqueueworkersemailworkeremail"></a>
##### jobQueue\.workers\.emailWorker\.email: object
<a name="riverworkersemailworkerconfig"></a>
##### river\.workers\.emailWorker\.config: object

**Properties**

Expand All @@ -65,18 +65,18 @@ Workers that will be enabled on the server
|**fromEmail**|`string`|||

**Additional Properties:** not allowed
<a name="jobqueueworkersdatabaseworker"></a>
#### jobQueue\.workers\.databaseWorker: object
<a name="riverworkersdatabaseworker"></a>
#### river\.workers\.databaseWorker: object

**Properties**

|Name|Type|Description|Required|
|----|----|-----------|--------|
|[**config**](#jobqueueworkersdatabaseworkerconfig)|`object`|||
|[**config**](#riverworkersdatabaseworkerconfig)|`object`|||

**Additional Properties:** not allowed
<a name="jobqueueworkersdatabaseworkerconfig"></a>
##### jobQueue\.workers\.databaseWorker\.config: object
<a name="riverworkersdatabaseworkerconfig"></a>
##### river\.workers\.databaseWorker\.config: object

**Properties**

Expand Down
4 changes: 2 additions & 2 deletions configgen/riverboat.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"jobs.EmailWorker": {
"properties": {
"email": {
"config": {
"$ref": "#/$defs/jobs.EmailConfig",
"description": "the email configuration"
}
Expand Down Expand Up @@ -125,7 +125,7 @@
"refreshInterval": {
"type": "integer"
},
"jobQueue": {
"river": {
"$ref": "#/$defs/river.Config"
}
},
Expand Down
2 changes: 1 addition & 1 deletion internal/river/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func createWorkers(c Workers) (*river.Workers, error) {
workers := river.NewWorkers()

if err := river.AddWorkerSafely(workers, &jobs.EmailWorker{
EmailConfig: c.EmailWorker.EmailConfig,
Config: c.EmailWorker.Config,
},
); err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions pkg/jobs/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (EmailArgs) Kind() string { return "email" }
type EmailWorker struct {
river.WorkerDefaults[EmailArgs]

EmailConfig `koanf:"email" json:"email" jsonschema:"description=the email configuration"`
Config EmailConfig `koanf:"config" json:"config" jsonschema:"description=the email configuration"`
}

// EmailConfig contains the configuration for the email worker
Expand All @@ -41,11 +41,11 @@ type EmailConfig struct {

// validateEmailConfig validates the email configuration settings
func (w *EmailWorker) validateEmailConfig() error {
if w.DevMode && w.TestDir == "" {
if w.Config.DevMode && w.Config.TestDir == "" {
return newMissingRequiredArg("test directory", EmailArgs{}.Kind())
}

if !w.DevMode && w.Token == "" {
if !w.Config.DevMode && w.Config.Token == "" {
return newMissingRequiredArg("token", EmailArgs{}.Kind())
}

Expand All @@ -67,19 +67,19 @@ func (w *EmailWorker) Work(ctx context.Context, job *river.Job[EmailArgs]) error
// set the options for the resend client
opts := []resend.Option{}

if w.DevMode {
log.Debug().Str("directory", w.TestDir).Msg("running in dev mode")
if w.Config.DevMode {
log.Debug().Str("directory", w.Config.TestDir).Msg("running in dev mode")

opts = append(opts, resend.WithDevMode(w.TestDir))
opts = append(opts, resend.WithDevMode(w.Config.TestDir))
}

// if the from email is not set on the message, use the default from the worker config
if job.Args.Message.From == "" {
job.Args.Message.From = w.FromEmail
job.Args.Message.From = w.Config.FromEmail
}

// create the resend client
client, err := resend.New(w.Token, opts...)
client, err := resend.New(w.Config.Token, opts...)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/jobs/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (suite *TestSuite) TestEmailWorker() {
{
name: "happy path, dev mode",
worker: &jobs.EmailWorker{
EmailConfig: jobs.EmailConfig{
Config: jobs.EmailConfig{
DevMode: true,
TestDir: "test",
FromEmail: "[email protected]",
Expand All @@ -43,7 +43,7 @@ func (suite *TestSuite) TestEmailWorker() {
{
name: "missing test directory",
worker: &jobs.EmailWorker{
EmailConfig: jobs.EmailConfig{
Config: jobs.EmailConfig{
DevMode: true,
FromEmail: "[email protected]",
},
Expand All @@ -54,7 +54,7 @@ func (suite *TestSuite) TestEmailWorker() {
{
name: "missing from email",
worker: &jobs.EmailWorker{
EmailConfig: jobs.EmailConfig{
Config: jobs.EmailConfig{
DevMode: true,
TestDir: "test",
},
Expand All @@ -65,7 +65,7 @@ func (suite *TestSuite) TestEmailWorker() {
{
name: "happy path, missing from email but in message",
worker: &jobs.EmailWorker{
EmailConfig: jobs.EmailConfig{
Config: jobs.EmailConfig{
DevMode: true,
TestDir: "test",
},
Expand All @@ -75,7 +75,7 @@ func (suite *TestSuite) TestEmailWorker() {
{
name: "missing token",
worker: &jobs.EmailWorker{
EmailConfig: jobs.EmailConfig{
Config: jobs.EmailConfig{
DevMode: false,
FromEmail: "[email protected]",
},
Expand Down
1 change: 1 addition & 0 deletions test/email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
newman.WithSubject("test subject"),
newman.WithText("body"),
newman.WithTo([]string{"[email protected]"}),
newman.WithFrom("[email protected]"),
)

_, err := client.Insert(context.Background(), jobs.EmailArgs{
Expand Down