dethnote/src/server.go

74 lines
1.6 KiB
Go

package dethnote
import (
"net/http"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
)
// Server is the master struct for the web app.
type Server struct {
// smtp server config
smtp MailConfig
root string
debug bool
n *negroni.Negroni
mux *mux.Router
}
// NewServer initializes the server struct.
func NewServer(root string, debug bool) *Server {
return &Server{
root: root,
debug: debug,
}
}
// SetupHTTP configures the HTTP server.
func (s *Server) SetupHTTP() {
// Set up the router.
Log.Debug("Setting up the HTTP router...")
r := mux.NewRouter()
s.mux = r
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := s.Template(w, "index.gohtml", nil); err != nil {
WriteError(w, err)
}
})
r.HandleFunc("/create", s.CreateHandler)
r.HandleFunc("/verify-email", func(w http.ResponseWriter, r *http.Request) {
s.Template(w, "verify-email.gohtml", map[string]interface{}{
"HashPath": s.GetCookie(r, "hash_path"),
})
})
r.HandleFunc("/verify/{token}/{path:[A-Fa-f0-9/]+}", s.VerifyHandler)
r.HandleFunc("/tmp", func(w http.ResponseWriter, r *http.Request) {
err := s.Template(w, "mail/verify-email.gohtml", map[string]interface{}{
"Subject": "Verify Your Email",
"URLBase": URLBase(r),
"HashPath": s.GetCookie(r, "hash_path"),
})
if err != nil {
WriteError(w, err)
}
})
n := negroni.New(
negroni.NewRecovery(),
negroni.NewLogger(),
negroni.NewStatic(http.Dir("./public")),
)
s.n = n
n.UseHandler(s.mux)
}
// Run the server.
func (s *Server) Run(addr string) {
Log.Info("Listening at %s", addr)
http.ListenAndServe(addr, s.n)
}