Skip to content

Commit

Permalink
updates for ezconf use
Browse files Browse the repository at this point in the history
  • Loading branch information
nicpottier committed Mar 5, 2018
1 parent debc5e7 commit d0914ab
Show file tree
Hide file tree
Showing 74 changed files with 5,576 additions and 13,884 deletions.
47 changes: 22 additions & 25 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@

[[constraint]]
branch = "master"
name = "github.com/koding/multiconfig"
name = "github.com/lib/pq"

[[constraint]]
branch = "master"
name = "github.com/lib/pq"
name = "github.com/nyaruka/gocommon"

[[constraint]]
branch = "master"
name = "github.com/nyaruka/gocommon"
name = "github.com/nyaruka/ezconf"

[[constraint]]
branch = "master"
Expand Down
5 changes: 2 additions & 3 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"strings"

"github.com/garyburd/redigo/redis"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/gocommon/urns"
)

// BackendConstructorFunc defines a function to create a particular backend type
type BackendConstructorFunc func(*config.Courier) Backend
type BackendConstructorFunc func(*Config) Backend

// Backend represents the part of Courier that deals with looking up and writing channels and results
type Backend interface {
Expand Down Expand Up @@ -81,7 +80,7 @@ type Backend interface {
}

// NewBackend creates the type of backend passed in
func NewBackend(config *config.Courier) (Backend, error) {
func NewBackend(config *Config) (Backend, error) {
backendFunc, found := registeredBackends[strings.ToLower(config.Backend)]
if !found {
return nil, fmt.Errorf("no such backend type: '%s'", config.Backend)
Expand Down
5 changes: 2 additions & 3 deletions backends/rapidpro/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/jmoiron/sqlx"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/chatbase"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/courier/queue"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
Expand Down Expand Up @@ -496,7 +495,7 @@ func (b *backend) RedisPool() *redis.Pool {
}

// NewBackend creates a new RapidPro backend
func newBackend(config *config.Courier) courier.Backend {
func newBackend(config *courier.Config) courier.Backend {
return &backend{
config: config,

Expand All @@ -506,7 +505,7 @@ func newBackend(config *config.Courier) courier.Backend {
}

type backend struct {
config *config.Courier
config *courier.Config

db *sqlx.DB
redisPool *redis.Pool
Expand Down
13 changes: 6 additions & 7 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/courier/queue"
"github.com/nyaruka/gocommon/urns"
"github.com/sirupsen/logrus"
Expand All @@ -42,8 +41,8 @@ func (m *mockS3Client) HeadBucket(*s3.HeadBucketInput) (*s3.HeadBucketOutput, er
return nil, nil
}

func testConfig() *config.Courier {
config := config.NewTest()
func testConfig() *courier.Config {
config := courier.NewConfig()
config.DB = "postgres://courier@localhost/courier_test?sslmode=disable"
config.Redis = "redis://localhost:6379/0"
return config
Expand Down Expand Up @@ -745,12 +744,12 @@ func TestMsgSuite(t *testing.T) {
}

var invalidConfigTestCases = []struct {
config config.Courier
config courier.Config
expectedError string
}{
{config: config.Courier{DB: ":foo"}, expectedError: "unable to parse DB URL"},
{config: config.Courier{DB: "mysql:test"}, expectedError: "only postgres is supported"},
{config: config.Courier{DB: "postgres://courier@localhost/courier", Redis: ":foo"}, expectedError: "unable to parse Redis URL"},
{config: courier.Config{DB: ":foo"}, expectedError: "unable to parse DB URL"},
{config: courier.Config{DB: "mysql:test"}, expectedError: "only postgres is supported"},
{config: courier.Config{DB: "postgres://courier@localhost/courier", Redis: ":foo"}, expectedError: "unable to parse Redis URL"},
}

func (ts *ServerTestSuite) TestInvalidConfigs() {
Expand Down
8 changes: 1 addition & 7 deletions cmd/courier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/evalphobia/logrus_sentry"
_ "github.com/lib/pq"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/config"
"github.com/sirupsen/logrus"

// load channel handler packages
Expand Down Expand Up @@ -56,12 +55,7 @@ import (
var version = "Dev"

func main() {
m := config.NewWithPath("courier.toml")
config := &config.Courier{}
err := m.Load(config)
if err != nil {
logrus.Fatalf("Error loading configuration: %s", err)
}
config := courier.LoadConfig("courier.toml")

// if we have a custom version, use it
if version != "Dev" {
Expand Down
9 changes: 2 additions & 7 deletions cmd/fuzzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ import (
"time"

"github.com/garyburd/redigo/redis"
"github.com/nyaruka/courier/config"
"github.com/nyaruka/courier"
)

func main() {
m := config.NewWithPath("courier.toml")
config := &config.Courier{}
err := m.Load(config)
if err != nil {
log.Fatalf("Error loading configuration: %s", err)
}
config := courier.LoadConfig("courier.toml")

// parse and test our redis config
redisURL, err := url.Parse(config.Redis)
Expand Down
66 changes: 66 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package courier

import "github.com/nyaruka/ezconf"

// Config is our top level configuration object
type Config struct {
Backend string `help:"the backend that will be used by courier (currently only rapidpro is supported)"`
SentryDSN string `help:"the DSN used for logging errors to Sentry"`
Domain string `help:"the domain courier is exposed on"`
Port int `help:"the port courier will listen on"`
DB string `help:"URL describing how to connect to the RapidPro database"`
Redis string `help:"URL describing how to connect to Redis"`
SpoolDir string `help:"the local directory where courier will write statuses or msgs that need to be retried (needs to be writable)"`
S3Region string `help:"the S3 region we will write attachments to"`
S3MediaBucket string `help:"the S3 bucket we will write attachments to"`
S3MediaPrefix string `help:"the prefix that will be added to attachment filenames"`
AWSAccessKeyID string `help:"the access key id to use when authenticating S3"`
AWSSecretAccessKey string `help:"the secret access key id to use when authenticating S3"`
MaxWorkers int `help:"the maximum number of go routines that will be used for sending (set to 0 to disable sending)"`
LibratoUsername string `help:"the username that will be used to authenticate to Librato"`
LibratoToken string `help:"the token that will be used to authenticate to Librato"`
StatusUsername string `help:"the username that is needed to authenticate against the /status endpoint"`
StatusPassword string `help:"the password that is needed to authenticate against the /status endpoint"`
LogLevel string `help:"the logging level courier should use"`
IgnoreDeliveryReports bool `help:"whether we ignore delivered status reports (errors will still be handled)"`
Version string `help:"the version that will be used in request and response headers"`

// IncludeChannels is the list of channels to enable, empty means include all
IncludeChannels []string

// ExcludeChannels is the list of channels to exclude, empty means exclude none
ExcludeChannels []string
}

// NewConfig returns a new default configuration object
func NewConfig() *Config {
return &Config{
Backend: "rapidpro",
Domain: "localhost",
Port: 8080,
DB: "postgres://courier@localhost/courier?sslmode=disable",
Redis: "redis://localhost:6379/0",
SpoolDir: "/var/spool/courier",
S3Region: "us-east-1",
S3MediaBucket: "courier-media",
S3MediaPrefix: "/media/",
AWSAccessKeyID: "missing_aws_access_key_id",
AWSSecretAccessKey: "missing_aws_secret_access_key",
MaxWorkers: 32,
LogLevel: "error",
Version: "Dev",
}
}

// LoadConfig loads our configuration from the passed in filename
func LoadConfig(filename string) *Config {
config := NewConfig()
loader := ezconf.NewLoader(
config,
"courier", "Courier - A fast message broker for SMS and IP messages",
[]string{filename},
)

loader.MustLoad()
return config
}
87 changes: 0 additions & 87 deletions config/courier.go

This file was deleted.

Loading

0 comments on commit d0914ab

Please sign in to comment.