mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Config file loading/validation implemented.
Builds, does not run!
This commit is contained in:
69
config.go
Normal file
69
config.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package inbucket
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/robfig/goconfig/config"
|
||||
"os"
|
||||
)
|
||||
|
||||
var Config *config.Config
|
||||
|
||||
// LoadConfig loads the specified configuration file into inbucket.Config
|
||||
// and performs validations on it.
|
||||
func LoadConfig(filename string) error {
|
||||
var err error
|
||||
Config, err = config.ReadDefault(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
messages := list.New()
|
||||
|
||||
// Validate sections
|
||||
requireSection(messages, "smtp")
|
||||
requireSection(messages, "web")
|
||||
requireSection(messages, "datastore")
|
||||
if messages.Len() > 0 {
|
||||
fmt.Fprintln(os.Stderr, "Error(s) validating configuration:")
|
||||
for e := messages.Front(); e != nil; e = e.Next() {
|
||||
fmt.Fprintln(os.Stderr, " -", e.Value.(string))
|
||||
}
|
||||
return fmt.Errorf("Failed to validate configuration")
|
||||
}
|
||||
|
||||
// Validate options
|
||||
requireOption(messages, "smtp", "ip4.address")
|
||||
requireOption(messages, "smtp", "ip4.port")
|
||||
requireOption(messages, "smtp", "domain")
|
||||
requireOption(messages, "web", "ip4.address")
|
||||
requireOption(messages, "web", "ip4.port")
|
||||
requireOption(messages, "web", "templates.dir")
|
||||
requireOption(messages, "web", "public.dir")
|
||||
requireOption(messages, "datastore", "path")
|
||||
if messages.Len() > 0 {
|
||||
fmt.Fprintln(os.Stderr, "Error(s) validating configuration:")
|
||||
for e := messages.Front(); e != nil; e = e.Next() {
|
||||
fmt.Fprintln(os.Stderr, " -", e.Value.(string))
|
||||
}
|
||||
return fmt.Errorf("Failed to validate configuration")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// requireSection checks that a [section] is defined in the configuration file,
|
||||
// appending a message if not.
|
||||
func requireSection(messages *list.List, section string) {
|
||||
if !Config.HasSection(section) {
|
||||
messages.PushBack(fmt.Sprintf("Config section [%v] is required", section))
|
||||
}
|
||||
}
|
||||
|
||||
// requireOption checks that 'option' is defined in [section] of the config file,
|
||||
// appending a message if not.
|
||||
func requireOption(messages *list.List, section string, option string) {
|
||||
if !Config.HasOption(section, option) {
|
||||
messages.PushBack(fmt.Sprintf("Config option '%v' is required in section [%v]", option, section))
|
||||
}
|
||||
}
|
||||
52
inbucketd/inbucketd.go
Normal file
52
inbucketd/inbucketd.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
This is the inbucket daemon launcher
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"os"
|
||||
)
|
||||
|
||||
var help = flag.Bool("help", false, "Displays this help")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if *help {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
// Load & Parse config
|
||||
if flag.NArg() != 1 {
|
||||
flag.Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
err := inbucket.LoadConfig(flag.Arg(0))
|
||||
configError(err)
|
||||
|
||||
// Startup SMTP server
|
||||
domain, err := inbucket.Config.String("smtp", "domain")
|
||||
configError(err)
|
||||
port, err := inbucket.Config.Int("smtp", "ip4.port")
|
||||
configError(err)
|
||||
server := smtpd.New(domain, port)
|
||||
go server.Start()
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintln(os.Stderr, "Usage of inbucketd [options] <conf file>:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
}
|
||||
|
||||
func configError(err error) {
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"bytes"
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket/app/inbucket"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"net"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
@@ -2,7 +2,7 @@ package smtpd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket/app/inbucket"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/robfig/revel"
|
||||
"net"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user