route/cmd/routed/main.go

70 lines
1.1 KiB
Go
Raw Normal View History

2017-01-18 09:57:18 +00:00
package main
import (
2017-01-22 18:16:18 +00:00
"crypto/tls"
2017-01-18 09:57:18 +00:00
"flag"
"log"
"math/rand"
"net"
"net/http"
"time"
2017-01-26 04:22:27 +00:00
"git.xeserv.us/xena/route/lib/routecrypto"
"git.xeserv.us/xena/route/server"
2017-04-28 23:28:03 +00:00
"github.com/Xe/ln"
"github.com/caarlos0/env"
2017-01-18 09:57:18 +00:00
"github.com/facebookgo/flagenv"
_ "github.com/joho/godotenv/autoload"
)
var (
2017-01-26 04:22:27 +00:00
sslCertKey = flag.String("ssl-cert-key", "", "if set encrypt SSL certs with this key")
2017-01-18 09:57:18 +00:00
)
func main() {
flag.Parse()
flagenv.Parse()
rand.Seed(time.Now().Unix())
2017-01-26 04:22:27 +00:00
certKey, _ := routecrypto.ParseKey(*sslCertKey)
2017-04-28 23:28:03 +00:00
scfg := server.Config{}
err := env.Parse(&scfg)
2017-01-18 09:57:18 +00:00
if err != nil {
2017-04-28 23:28:03 +00:00
ln.Fatal(ln.F{"err": err, "action": "env.Parse()"})
2017-01-18 09:57:18 +00:00
}
2017-04-28 23:28:03 +00:00
scfg.CertKey = certKey
2017-01-18 09:57:18 +00:00
2017-04-28 23:28:03 +00:00
s, err := server.New(scfg)
if err != nil {
log.Fatal(err)
2017-01-22 18:16:18 +00:00
}
2017-04-28 23:28:03 +00:00
go setupTLS(s, scfg)
l, err := net.Listen("tcp", scfg.WebAddr)
2017-01-18 09:57:18 +00:00
if err != nil {
log.Fatal(err)
}
defer l.Close()
hs := &http.Server{
Handler: s,
2017-04-28 23:28:03 +00:00
Addr: scfg.WebAddr,
2017-01-18 09:57:18 +00:00
}
hs.Serve(l)
}
2017-01-22 18:16:18 +00:00
2017-04-28 23:28:03 +00:00
func setupTLS(s *server.Server, scfg server.Config) {
2017-01-22 18:16:18 +00:00
hs := &http.Server{
Handler: s,
2017-04-28 23:28:03 +00:00
Addr: scfg.SSLAddr,
2017-01-22 18:16:18 +00:00
TLSConfig: &tls.Config{
2017-04-28 23:28:03 +00:00
GetCertificate: s.GetCertificate,
2017-01-22 18:16:18 +00:00
},
}
hs.ListenAndServeTLS("", "")
}