Skip to content

Commit

Permalink
Add option to not fallback to in-memory storage from mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Easters committed Dec 12, 2019
1 parent 50f74a1 commit b13e215
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ import (
// DefaultConfig is the default config
func DefaultConfig() *Config {
return &Config{
SMTPBindAddr: "0.0.0.0:1025",
APIBindAddr: "0.0.0.0:8025",
Hostname: "mailhog.example",
MongoURI: "127.0.0.1:27017",
MongoDb: "mailhog",
MongoColl: "messages",
MaildirPath: "",
StorageType: "memory",
CORSOrigin: "",
WebPath: "",
MessageChan: make(chan *data.Message),
OutgoingSMTP: make(map[string]*OutgoingSMTP),
SMTPBindAddr: "0.0.0.0:1025",
APIBindAddr: "0.0.0.0:8025",
Hostname: "mailhog.example",
MongoURI: "127.0.0.1:27017",
MongoDb: "mailhog",
MongoColl: "messages",
MongoFallback: true,
MaildirPath: "",
StorageType: "memory",
CORSOrigin: "",
WebPath: "",
MessageChan: make(chan *data.Message),
OutgoingSMTP: make(map[string]*OutgoingSMTP),
}
}

Expand All @@ -38,6 +39,7 @@ type Config struct {
MongoURI string
MongoDb string
MongoColl string
MongoFallback bool
StorageType string
CORSOrigin string
MaildirPath string
Expand Down Expand Up @@ -78,6 +80,9 @@ func Configure() *Config {
log.Println("Using MongoDB message storage")
s := storage.CreateMongoDB(cfg.MongoURI, cfg.MongoDb, cfg.MongoColl)
if s == nil {
if !cfg.MongoFallback {
log.Fatal("MongoDB storage unavailable")
}
log.Println("MongoDB storage unavailable, reverting to in-memory storage")
cfg.Storage = storage.CreateInMemory()
} else {
Expand Down Expand Up @@ -124,6 +129,7 @@ func RegisterFlags() {
flag.StringVar(&cfg.MongoURI, "mongo-uri", envconf.FromEnvP("MH_MONGO_URI", "127.0.0.1:27017").(string), "MongoDB URI, e.g. 127.0.0.1:27017")
flag.StringVar(&cfg.MongoDb, "mongo-db", envconf.FromEnvP("MH_MONGO_DB", "mailhog").(string), "MongoDB database, e.g. mailhog")
flag.StringVar(&cfg.MongoColl, "mongo-coll", envconf.FromEnvP("MH_MONGO_COLLECTION", "messages").(string), "MongoDB collection, e.g. messages")
flag.BoolVar(&cfg.MongoFallback, "mongo-fallback", envconf.FromEnvP("MH_MONGO_FALLBACK", true).(bool), "Fallback to in-memory storage if MongoDB is unavailable")
flag.StringVar(&cfg.CORSOrigin, "cors-origin", envconf.FromEnvP("MH_CORS_ORIGIN", "").(string), "CORS Access-Control-Allow-Origin header for API endpoints")
flag.StringVar(&cfg.MaildirPath, "maildir-path", envconf.FromEnvP("MH_MAILDIR_PATH", "").(string), "Maildir path (if storage type is 'maildir')")
flag.BoolVar(&cfg.InviteJim, "invite-jim", envconf.FromEnvP("MH_INVITE_JIM", false).(bool), "Decide whether to invite Jim (beware, he causes trouble)")
Expand Down

0 comments on commit b13e215

Please sign in to comment.