mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Add expvar JSON @ /debug/vars
Counters include: - Uptime - SMTP connections - SMTP connections (current) - SMTP messages delivered
This commit is contained in:
10
inbucket.go
10
inbucket.go
@@ -4,6 +4,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"expvar"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket/config"
|
||||
@@ -11,10 +12,13 @@ import (
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"github.com/jhillyerd/inbucket/web"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var help = flag.Bool("help", false, "Displays this help")
|
||||
|
||||
var startTime = time.Now()
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if *help {
|
||||
@@ -49,4 +53,10 @@ func init() {
|
||||
fmt.Fprintln(os.Stderr, "Usage of inbucket [options] <conf file>:")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
expvar.Publish("uptime", expvar.Func(uptime))
|
||||
}
|
||||
|
||||
func uptime() interface{} {
|
||||
return time.Since(startTime) / time.Second
|
||||
}
|
||||
|
||||
@@ -88,7 +88,9 @@ func (ss *Session) String() string {
|
||||
*/
|
||||
func (s *Server) startSession(id int, conn net.Conn) {
|
||||
log.Info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
|
||||
expConnectsCurrent.Add(1)
|
||||
defer conn.Close()
|
||||
defer expConnectsCurrent.Add(-1)
|
||||
|
||||
ss := NewSession(s, id, conn)
|
||||
ss.greet()
|
||||
@@ -316,6 +318,7 @@ func (ss *Session) dataHandler() {
|
||||
// Mail data complete
|
||||
for _, m := range messages {
|
||||
m.Close()
|
||||
expDeliveredTotal.Add(1)
|
||||
}
|
||||
ss.send("250 Mail accepted for delivery")
|
||||
ss.info("Message size %v bytes", msgSize)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"expvar"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket/config"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
@@ -16,6 +17,10 @@ type Server struct {
|
||||
dataStore *DataStore
|
||||
}
|
||||
|
||||
var expConnectsTotal = new(expvar.Int)
|
||||
var expConnectsCurrent = new(expvar.Int)
|
||||
var expDeliveredTotal = new(expvar.Int)
|
||||
|
||||
// Init a new Server object
|
||||
func New() *Server {
|
||||
ds := NewDataStore()
|
||||
@@ -49,7 +54,15 @@ func (s *Server) Start() {
|
||||
// or maybe attempt to restart smtpd
|
||||
panic(err)
|
||||
} else {
|
||||
expConnectsTotal.Add(1)
|
||||
go s.startSession(sid, conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
m := expvar.NewMap("smtp")
|
||||
m.Set("connectsTotal", expConnectsTotal)
|
||||
m.Set("connectsCurrent", expConnectsCurrent)
|
||||
m.Set("deliveredTotal", expDeliveredTotal)
|
||||
}
|
||||
|
||||
@@ -21,11 +21,10 @@ var Router *mux.Router
|
||||
var sessionStore sessions.Store
|
||||
|
||||
func setupRoutes(cfg config.WebConfig) {
|
||||
Router = mux.NewRouter()
|
||||
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
|
||||
r := Router
|
||||
r := mux.NewRouter()
|
||||
// Static content
|
||||
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/",
|
||||
http.FileServer(http.Dir(cfg.PublicDir))))
|
||||
@@ -38,6 +37,10 @@ func setupRoutes(cfg config.WebConfig) {
|
||||
r.Path("/mailbox/html/{name}/{id}").Handler(handler(MailboxHtml)).Name("MailboxHtml").Methods("GET")
|
||||
r.Path("/mailbox/source/{name}/{id}").Handler(handler(MailboxSource)).Name("MailboxSource").Methods("GET")
|
||||
r.Path("/mailbox/delete/{name}/{id}").Handler(handler(MailboxDelete)).Name("MailboxDelete").Methods("POST")
|
||||
|
||||
// Register w/ HTTP
|
||||
Router = r
|
||||
http.Handle("/", Router)
|
||||
}
|
||||
|
||||
// Start() the web server
|
||||
@@ -51,7 +54,7 @@ func Start() {
|
||||
log.Info("HTTP listening on TCP4 %v", addr)
|
||||
s := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: Router,
|
||||
Handler: nil,
|
||||
ReadTimeout: 60 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user