MailHog/main.go

82 lines
2 KiB
Go
Raw Normal View History

2014-04-16 22:29:23 +00:00
package main
import (
"flag"
"github.com/ian-kent/MailHog/mailhog"
2014-04-20 14:35:59 +00:00
"github.com/ian-kent/MailHog/mailhog/http"
2014-04-16 22:59:25 +00:00
"github.com/ian-kent/MailHog/mailhog/smtp"
2014-04-20 19:33:42 +00:00
"github.com/ian-kent/MailHog/mailhog/storage"
2014-04-20 16:12:32 +00:00
"log"
"net"
"os"
2014-04-16 22:29:23 +00:00
)
var conf *mailhog.Config
2014-04-20 19:33:42 +00:00
var mongo *storage.MongoDB
2014-04-20 14:35:59 +00:00
var exitCh chan int
2014-04-16 22:29:23 +00:00
func config() {
2014-04-20 14:35:59 +00:00
var smtpbindaddr, httpbindaddr, hostname, mongouri, mongodb, mongocoll string
2014-04-16 22:29:23 +00:00
2014-04-20 14:35:59 +00:00
flag.StringVar(&smtpbindaddr, "smtpbindaddr", "0.0.0.0:1025", "SMTP bind interface and port, e.g. 0.0.0.0:1025 or just :1025")
flag.StringVar(&httpbindaddr, "httpbindaddr", "0.0.0.0:8025", "HTTP bind interface and port, e.g. 0.0.0.0:8025 or just :8025")
2014-04-16 22:59:25 +00:00
flag.StringVar(&hostname, "hostname", "mailhog.example", "Hostname for EHLO/HELO response, e.g. mailhog.example")
2014-04-20 14:35:59 +00:00
flag.StringVar(&mongouri, "mongouri", "127.0.0.1:27017", "MongoDB URI, e.g. 127.0.0.1:27017")
flag.StringVar(&mongodb, "mongodb", "mailhog", "MongoDB database, e.g. mailhog")
flag.StringVar(&mongocoll, "mongocoll", "messages", "MongoDB collection, e.g. messages")
2014-04-16 22:59:25 +00:00
2014-04-16 22:29:23 +00:00
flag.Parse()
conf = &mailhog.Config{
2014-04-20 14:35:59 +00:00
SMTPBindAddr: smtpbindaddr,
HTTPBindAddr: httpbindaddr,
2014-04-20 16:12:32 +00:00
Hostname: hostname,
MongoUri: mongouri,
MongoDb: mongodb,
MongoColl: mongocoll,
}
2014-04-20 19:33:42 +00:00
mongo = storage.CreateMongoDB(conf)
2014-04-16 22:29:23 +00:00
}
func main() {
config()
2014-04-20 14:35:59 +00:00
exitCh = make(chan int)
go web_listen()
2014-04-20 16:12:32 +00:00
go smtp_listen()
2014-04-20 14:35:59 +00:00
for {
select {
2014-04-20 16:12:32 +00:00
case <-exitCh:
log.Printf("Received exit signal")
os.Exit(0)
2014-04-20 14:35:59 +00:00
}
}
}
func web_listen() {
log.Printf("[HTTP] Binding to address: %s\n", conf.HTTPBindAddr)
2014-04-20 19:33:42 +00:00
http.Start(exitCh, conf, mongo)
2014-04-20 14:35:59 +00:00
}
2014-04-20 16:12:32 +00:00
func smtp_listen() *net.TCPListener {
2014-04-20 14:35:59 +00:00
log.Printf("[SMTP] Binding to address: %s\n", conf.SMTPBindAddr)
ln, err := net.Listen("tcp", conf.SMTPBindAddr)
if err != nil {
log.Fatalf("[SMTP] Error listening on socket: %s\n", err)
}
2014-04-16 22:29:23 +00:00
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
2014-04-20 14:35:59 +00:00
log.Printf("[SMTP] Error accepting connection: %s\n", err)
2014-04-16 22:29:23 +00:00
continue
}
defer conn.Close()
2014-04-20 19:33:42 +00:00
go smtp.StartSession(conn.(*net.TCPConn), conf, mongo)
2014-04-16 22:29:23 +00:00
}
}