Add TLS support

Fixes #20.

Change-Id: I9c6b4ff3195f05e8a3c17d6704ee06b1f77db562
This commit is contained in:
Stephen McQuay 2016-04-17 18:26:45 -07:00
parent 680eecb111
commit cce3166bdd
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
2 changed files with 20 additions and 4 deletions

View File

@ -55,6 +55,9 @@ const usage = "vaind [init] <dbname>"
type config struct {
Port int
Cert string
Key string
}
func main() {
@ -114,8 +117,17 @@ func main() {
sm := http.NewServeMux()
vain.NewServer(sm, db)
addr := fmt.Sprintf(":%d", c.Port)
if err := http.ListenAndServe(addr, sm); err != nil {
log.Printf("problem with http server: %v", err)
os.Exit(1)
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)
}
}
}

View File

@ -130,7 +130,11 @@ func (s *Server) register(w http.ResponseWriter, req *http.Request) {
http.Error(w, err.Message, err.Code)
return
}
log.Printf("http://%s/api/v0/confirm/%+v", req.Host, tok)
proto := "https"
if req.TLS == nil {
proto = "http"
}
log.Printf("%s://%s/api/v0/confirm/%+v", proto, req.Host, tok)
fmt.Fprintf(w, "please check your email\n")
}