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 { // Public configurable fields. MailgunURL string // format is like "key-1a2b3c@https://api.mailgun.net/v3/mg.example.com" 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"), }) }) 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) }