MailHog/main.go

78 lines
1.9 KiB
Go
Raw Normal View History

2014-04-16 22:29:23 +00:00
package main
import (
"flag"
"log"
"net"
2014-04-20 14:35:59 +00:00
"os"
"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-16 22:29:23 +00:00
)
var conf *mailhog.Config
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,
Hostname: hostname,
2014-04-20 14:35:59 +00:00
MongoUri: mongouri,
MongoDb: mongodb,
MongoColl: mongocoll,
}
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()
go smtp_listen()
for {
select {
case <-exitCh:
log.Printf("Received exit signal")
os.Exit(0)
}
}
}
func web_listen() {
log.Printf("[HTTP] Binding to address: %s\n", conf.HTTPBindAddr)
http.Start(exitCh, conf)
}
func smtp_listen() (*net.TCPListener) {
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()
go smtp.StartSession(conn.(*net.TCPConn), conf)
2014-04-16 22:29:23 +00:00
}
}