2016-02-07 23:54:55 -08:00
|
|
|
package main
|
|
|
|
|
2016-02-08 00:15:22 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-06-22 23:01:21 -07:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2016-05-14 21:30:58 -07:00
|
|
|
"time"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
2016-02-11 11:57:16 -08:00
|
|
|
"mcquay.me/vain"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
)
|
|
|
|
|
2016-06-22 23:01:21 -07:00
|
|
|
const usage = "vaind <dbname>"
|
2016-02-08 00:15:22 -08:00
|
|
|
|
|
|
|
type config struct {
|
2016-06-03 13:42:05 -07:00
|
|
|
Port int
|
|
|
|
Insecure bool
|
2016-04-17 18:26:45 -07:00
|
|
|
|
|
|
|
Cert string
|
|
|
|
Key string
|
2016-04-23 22:16:40 -07:00
|
|
|
|
|
|
|
Static string
|
2016-05-14 21:30:58 -07:00
|
|
|
|
|
|
|
EmailTimeout time.Duration `envconfig:"email_timeout"`
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
SMTPHost string `envconfig:"smtp_host"`
|
|
|
|
SMTPPort int `envconfig:"smtp_port"`
|
|
|
|
|
|
|
|
From string
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
|
2016-02-07 23:54:55 -08:00
|
|
|
func main() {
|
2016-06-22 23:01:21 -07:00
|
|
|
log.SetFlags(log.Lshortfile)
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:15:22 -08:00
|
|
|
c := &config{
|
2016-05-14 21:30:58 -07:00
|
|
|
Port: 4040,
|
|
|
|
EmailTimeout: 5 * time.Minute,
|
2016-06-03 13:42:05 -07:00
|
|
|
SMTPPort: 25,
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
if err := envconfig.Process("vain", c); err != nil {
|
2016-02-08 00:15:22 -08:00
|
|
|
fmt.Fprintf(os.Stderr, "problem processing environment: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
switch os.Args[1] {
|
2016-06-15 02:35:40 -07:00
|
|
|
case "env", "e":
|
|
|
|
fmt.Printf("VAIN_PORT: %v\n", c.Port)
|
|
|
|
fmt.Printf("VAIN_INSECURE: %v\n", c.Insecure)
|
|
|
|
fmt.Printf("VAIN_CERT: %v\n", c.Cert)
|
|
|
|
fmt.Printf("VAIN_KEY: %v\n", c.Key)
|
|
|
|
fmt.Printf("VAIN_STATIC: %v\n", c.Static)
|
|
|
|
fmt.Printf("VAIN_EMAIL_TIMEOUT: %v\n", c.EmailTimeout)
|
|
|
|
fmt.Printf("VAIN_SMTP_HOST: %v\n", c.SMTPHost)
|
|
|
|
fmt.Printf("VAIN_SMTP_PORT: %v\n", c.SMTPPort)
|
|
|
|
fmt.Printf("VAIN_FROM: %v\n", c.From)
|
|
|
|
os.Exit(0)
|
|
|
|
case "help", "h":
|
2016-02-14 22:19:41 -08:00
|
|
|
fmt.Printf("%s\n", usage)
|
|
|
|
os.Exit(0)
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
|
|
|
}
|
2016-06-15 02:35:40 -07:00
|
|
|
log.Printf("%+v", c)
|
|
|
|
|
2016-06-22 23:01:21 -07:00
|
|
|
db, err := vain.NewMemDB(os.Args[1])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "couldn't open db: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
s := <-sigs
|
|
|
|
log.Printf("signal: %+v", s)
|
|
|
|
if err := db.Sync(); err != nil {
|
|
|
|
log.Printf("problem syncing db to disk: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
m, err := vain.NewMail(c.From, c.SMTPHost, c.SMTPPort)
|
2016-06-03 13:42:05 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "problem initializing mailer: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:15:22 -08:00
|
|
|
hostname := "localhost"
|
|
|
|
if hn, err := os.Hostname(); err != nil {
|
2016-05-23 23:54:35 -07:00
|
|
|
log.Printf("problem getting hostname: %v", err)
|
2016-02-08 00:15:22 -08:00
|
|
|
} else {
|
|
|
|
hostname = hn
|
|
|
|
}
|
|
|
|
log.Printf("serving at: http://%s:%d/", hostname, c.Port)
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
vain.NewServer(sm, db, m, c.Static, c.EmailTimeout, c.Insecure)
|
2016-02-08 00:15:22 -08:00
|
|
|
addr := fmt.Sprintf(":%d", c.Port)
|
2016-04-17 18:26:45 -07:00
|
|
|
|
|
|
|
if c.Cert == "" || c.Key == "" {
|
|
|
|
log.Printf("INSECURE MODE")
|
|
|
|
if err := http.ListenAndServe(addr, sm); err != nil {
|
|
|
|
log.Printf("problem with http server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := http.ListenAndServeTLS(addr, c.Cert, c.Key, sm); err != nil {
|
|
|
|
log.Printf("problem with http server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-02-08 00:15:22 -08:00
|
|
|
}
|
2016-02-07 23:54:55 -08:00
|
|
|
}
|