mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-19 02:27:03 +00:00
Follow meta-linter recommendations for all of Inbucket
- Rename BUILD_DATE to BUILDDATE in goxc - Update travis config - Follow linter recommendations for inbucket.go - Follow linter recommendations for config package - Follow linter recommendations for log package - Follow linter recommendations for pop3d package - Follow linter recommendations for smtpd package - Follow linter recommendations for web package - Fix Id -> ID in templates - Add shebang to REST demo scripts - Add or refine many comments
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
/*
|
||||
The web package contains all the code to provide Inbucket's web GUI
|
||||
*/
|
||||
// Package web provides Inbucket's web GUI and RESTful API
|
||||
package web
|
||||
|
||||
import (
|
||||
@@ -19,12 +17,18 @@ import (
|
||||
|
||||
type handler func(http.ResponseWriter, *http.Request, *Context) error
|
||||
|
||||
var webConfig config.WebConfig
|
||||
var DataStore smtpd.DataStore
|
||||
var Router *mux.Router
|
||||
var listener net.Listener
|
||||
var sessionStore sessions.Store
|
||||
var shutdown bool
|
||||
var (
|
||||
// DataStore is where all the mailboxes and messages live
|
||||
DataStore smtpd.DataStore
|
||||
|
||||
// Router sends incoming requests to the correct handler function
|
||||
Router *mux.Router
|
||||
|
||||
webConfig config.WebConfig
|
||||
listener net.Listener
|
||||
sessionStore sessions.Store
|
||||
shutdown bool
|
||||
)
|
||||
|
||||
// Initialize sets up things for unit tests or the Start() method
|
||||
func Initialize(cfg config.WebConfig, ds smtpd.DataStore) {
|
||||
@@ -39,8 +43,8 @@ func Initialize(cfg config.WebConfig, ds smtpd.DataStore) {
|
||||
}
|
||||
|
||||
func setupRoutes(cfg config.WebConfig) {
|
||||
log.LogInfo("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
log.LogInfo("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
log.Infof("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
log.Infof("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
|
||||
r := mux.NewRouter()
|
||||
// Static content
|
||||
@@ -55,7 +59,7 @@ func setupRoutes(cfg config.WebConfig) {
|
||||
r.Path("/mailbox/{name}").Handler(handler(MailboxList)).Name("MailboxList").Methods("GET")
|
||||
r.Path("/mailbox/{name}").Handler(handler(MailboxPurge)).Name("MailboxPurge").Methods("DELETE")
|
||||
r.Path("/mailbox/{name}/{id}").Handler(handler(MailboxShow)).Name("MailboxShow").Methods("GET")
|
||||
r.Path("/mailbox/{name}/{id}/html").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
|
||||
r.Path("/mailbox/{name}/{id}/html").Handler(handler(MailboxHTML)).Name("MailboxHtml").Methods("GET")
|
||||
r.Path("/mailbox/{name}/{id}/source").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
||||
r.Path("/mailbox/{name}/{id}").Handler(handler(MailboxDelete)).Name("MailboxDelete").Methods("DELETE")
|
||||
r.Path("/mailbox/dattach/{name}/{id}/{num}/{file}").Handler(handler(MailboxDownloadAttach)).Name("MailboxDownloadAttach").Methods("GET")
|
||||
@@ -66,9 +70,9 @@ func setupRoutes(cfg config.WebConfig) {
|
||||
http.Handle("/", Router)
|
||||
}
|
||||
|
||||
// Start() the web server
|
||||
// Start begins listening for HTTP requests
|
||||
func Start() {
|
||||
addr := fmt.Sprintf("%v:%v", webConfig.Ip4address, webConfig.Ip4port)
|
||||
addr := fmt.Sprintf("%v:%v", webConfig.IP4address, webConfig.IP4port)
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: nil,
|
||||
@@ -77,30 +81,33 @@ func Start() {
|
||||
}
|
||||
|
||||
// We don't use ListenAndServe because it lacks a way to close the listener
|
||||
log.LogInfo("HTTP listening on TCP4 %v", addr)
|
||||
log.Infof("HTTP listening on TCP4 %v", addr)
|
||||
var err error
|
||||
listener, err = net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.LogError("HTTP failed to start TCP4 listener: %v", err)
|
||||
log.Errorf("HTTP failed to start TCP4 listener: %v", err)
|
||||
// TODO More graceful early-shutdown procedure
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = server.Serve(listener)
|
||||
if shutdown {
|
||||
log.LogTrace("HTTP server shutting down on request")
|
||||
log.Tracef("HTTP server shutting down on request")
|
||||
} else if err != nil {
|
||||
log.LogError("HTTP server failed: %v", err)
|
||||
log.Errorf("HTTP server failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop shuts down the HTTP server
|
||||
func Stop() {
|
||||
log.LogTrace("HTTP shutdown requested")
|
||||
log.Tracef("HTTP shutdown requested")
|
||||
shutdown = true
|
||||
if listener != nil {
|
||||
listener.Close()
|
||||
if err := listener.Close(); err != nil {
|
||||
log.Errorf("Error closing HTTP listener: %v", err)
|
||||
}
|
||||
} else {
|
||||
log.LogError("HTTP listener was nil during shutdown")
|
||||
log.Errorf("HTTP listener was nil during shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +116,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Create the context
|
||||
ctx, err := NewContext(req)
|
||||
if err != nil {
|
||||
log.LogError("Failed to create context: %v", err)
|
||||
log.Errorf("Failed to create context: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -117,21 +124,23 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// Run the handler, grab the error, and report it
|
||||
buf := new(httpbuf.Buffer)
|
||||
log.LogTrace("Web: %v %v %v %v", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
||||
log.Tracef("Web: %v %v %v %v", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
||||
err = h(buf, req, ctx)
|
||||
if err != nil {
|
||||
log.LogError("Error handling %v: %v", req.RequestURI, err)
|
||||
log.Errorf("Error handling %v: %v", req.RequestURI, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Save the session
|
||||
if err = ctx.Session.Save(req, buf); err != nil {
|
||||
log.LogError("Failed to save session: %v", err)
|
||||
log.Errorf("Failed to save session: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Apply the buffered response to the writer
|
||||
buf.Apply(w)
|
||||
if _, err = buf.Apply(w); err != nil {
|
||||
log.Errorf("Failed to write response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user