1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

web: remove DataStore from Context and controllers for #81

This commit is contained in:
James Hillyerd
2018-03-12 20:49:06 -07:00
parent 10bc07a18e
commit 219862797e
7 changed files with 21 additions and 32 deletions

View File

@@ -125,7 +125,7 @@ func main() {
// Start HTTP server // Start HTTP server
mm := &message.StoreManager{Store: ds} mm := &message.StoreManager{Store: ds}
web.Initialize(config.GetWebConfig(), shutdownChan, mm, ds, msgHub) web.Initialize(config.GetWebConfig(), shutdownChan, mm, msgHub)
webui.SetupRoutes(web.Router) webui.SetupRoutes(web.Router)
rest.SetupRoutes(web.Router) rest.SetupRoutes(web.Router)
go web.Start(rootCtx) go web.Start(rootCtx)

View File

@@ -24,7 +24,7 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
if err != nil { if err != nil {
return err return err
} }
messages, err := ctx.MsgSvc.GetMetadata(name) messages, err := ctx.Manager.GetMetadata(name)
if err != nil { if err != nil {
// This doesn't indicate empty, likely an IO error // This doesn't indicate empty, likely an IO error
return fmt.Errorf("Failed to get messages for %v: %v", name, err) return fmt.Errorf("Failed to get messages for %v: %v", name, err)
@@ -54,7 +54,7 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
if err != nil { if err != nil {
return err return err
} }
msg, err := ctx.MsgSvc.GetMessage(name, id) msg, err := ctx.Manager.GetMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -105,7 +105,7 @@ func MailboxPurgeV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
return err return err
} }
// Delete all messages // Delete all messages
err = ctx.MsgSvc.PurgeMessages(name) err = ctx.Manager.PurgeMessages(name)
if err != nil { if err != nil {
return fmt.Errorf("Mailbox(%q) purge failed: %v", name, err) return fmt.Errorf("Mailbox(%q) purge failed: %v", name, err)
} }
@@ -123,7 +123,7 @@ func MailboxSourceV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
return err return err
} }
r, err := ctx.MsgSvc.SourceReader(name, id) r, err := ctx.Manager.SourceReader(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -146,7 +146,7 @@ func MailboxDeleteV1(w http.ResponseWriter, req *http.Request, ctx *web.Context)
if err != nil { if err != nil {
return err return err
} }
err = ctx.MsgSvc.RemoveMessage(name, id) err = ctx.Manager.RemoveMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil

View File

@@ -34,9 +34,8 @@ const (
func TestRestMailboxList(t *testing.T) { func TestRestMailboxList(t *testing.T) {
// Setup // Setup
ds := test.NewStore()
mm := test.NewManager() mm := test.NewManager()
logbuf := setupWebServer(mm, ds) logbuf := setupWebServer(mm)
// Test invalid mailbox name // Test invalid mailbox name
w, err := testRestGet(baseURL + "/mailbox/foo@bar") w, err := testRestGet(baseURL + "/mailbox/foo@bar")
@@ -165,9 +164,8 @@ func TestRestMailboxList(t *testing.T) {
func TestRestMessage(t *testing.T) { func TestRestMessage(t *testing.T) {
// Setup // Setup
ds := test.NewStore()
mm := test.NewManager() mm := test.NewManager()
logbuf := setupWebServer(mm, ds) logbuf := setupWebServer(mm)
// Test invalid mailbox name // Test invalid mailbox name
w, err := testRestGet(baseURL + "/mailbox/foo@bar/0001") w, err := testRestGet(baseURL + "/mailbox/foo@bar/0001")

View File

@@ -194,7 +194,7 @@ func testRestGet(url string) (*httptest.ResponseRecorder, error) {
return w, nil return w, nil
} }
func setupWebServer(mm message.Manager, ds storage.Store) *bytes.Buffer { func setupWebServer(mm message.Manager) *bytes.Buffer {
// Capture log output // Capture log output
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
log.SetOutput(buf) log.SetOutput(buf)
@@ -206,7 +206,7 @@ func setupWebServer(mm message.Manager, ds storage.Store) *bytes.Buffer {
PublicDir: "../themes/bootstrap/public", PublicDir: "../themes/bootstrap/public",
} }
shutdownChan := make(chan bool) shutdownChan := make(chan bool)
web.Initialize(cfg, shutdownChan, mm, ds, &msghub.Hub{}) web.Initialize(cfg, shutdownChan, mm, &msghub.Hub{})
SetupRoutes(web.Router) SetupRoutes(web.Router)
return buf return buf

View File

@@ -9,16 +9,14 @@ import (
"github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/message" "github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/msghub" "github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/storage"
) )
// Context is passed into every request handler function // Context is passed into every request handler function
type Context struct { type Context struct {
Vars map[string]string Vars map[string]string
Session *sessions.Session Session *sessions.Session
DataStore storage.Store
MsgHub *msghub.Hub MsgHub *msghub.Hub
MsgSvc message.Manager Manager message.Manager
WebConfig config.WebConfig WebConfig config.WebConfig
IsJSON bool IsJSON bool
} }
@@ -61,9 +59,8 @@ func NewContext(req *http.Request) (*Context, error) {
ctx := &Context{ ctx := &Context{
Vars: vars, Vars: vars,
Session: sess, Session: sess,
DataStore: DataStore,
MsgHub: msgHub, MsgHub: msgHub,
MsgSvc: msgSvc, Manager: manager,
WebConfig: webConfig, WebConfig: webConfig,
IsJSON: headerMatch(req, "Accept", "application/json"), IsJSON: headerMatch(req, "Accept", "application/json"),
} }

View File

@@ -16,19 +16,15 @@ import (
"github.com/jhillyerd/inbucket/pkg/log" "github.com/jhillyerd/inbucket/pkg/log"
"github.com/jhillyerd/inbucket/pkg/message" "github.com/jhillyerd/inbucket/pkg/message"
"github.com/jhillyerd/inbucket/pkg/msghub" "github.com/jhillyerd/inbucket/pkg/msghub"
"github.com/jhillyerd/inbucket/pkg/storage"
) )
// Handler is a function type that handles an HTTP request in Inbucket // Handler is a function type that handles an HTTP request in Inbucket
type Handler func(http.ResponseWriter, *http.Request, *Context) error type Handler func(http.ResponseWriter, *http.Request, *Context) error
var ( var (
// DataStore is where all the mailboxes and messages live
DataStore storage.Store
// msgHub holds a reference to the message pub/sub system // msgHub holds a reference to the message pub/sub system
msgHub *msghub.Hub msgHub *msghub.Hub
msgSvc message.Manager manager message.Manager
// Router is shared between httpd, webui and rest packages. It sends // Router is shared between httpd, webui and rest packages. It sends
// incoming requests to the correct handler function // incoming requests to the correct handler function
@@ -54,16 +50,14 @@ func Initialize(
cfg config.WebConfig, cfg config.WebConfig,
shutdownChan chan bool, shutdownChan chan bool,
mm message.Manager, mm message.Manager,
ds storage.Store,
mh *msghub.Hub) { mh *msghub.Hub) {
webConfig = cfg webConfig = cfg
globalShutdown = shutdownChan globalShutdown = shutdownChan
// NewContext() will use this DataStore for the web handlers // NewContext() will use this DataStore for the web handlers
DataStore = ds
msgHub = mh msgHub = mh
msgSvc = mm manager = mm
// Content Paths // Content Paths
log.Infof("HTTP templates mapped to %q", cfg.TemplateDir) log.Infof("HTTP templates mapped to %q", cfg.TemplateDir)

View File

@@ -72,7 +72,7 @@ func MailboxList(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er
if err != nil { if err != nil {
return err return err
} }
messages, err := ctx.MsgSvc.GetMetadata(name) messages, err := ctx.Manager.GetMetadata(name)
if err != nil { if err != nil {
// This doesn't indicate empty, likely an IO error // This doesn't indicate empty, likely an IO error
return fmt.Errorf("Failed to get messages for %v: %v", name, err) return fmt.Errorf("Failed to get messages for %v: %v", name, err)
@@ -94,7 +94,7 @@ func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er
if err != nil { if err != nil {
return err return err
} }
msg, err := ctx.MsgSvc.GetMessage(name, id) msg, err := ctx.Manager.GetMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -135,7 +135,7 @@ func MailboxHTML(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er
if err != nil { if err != nil {
return err return err
} }
msg, err := ctx.MsgSvc.GetMessage(name, id) msg, err := ctx.Manager.GetMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -163,7 +163,7 @@ func MailboxSource(w http.ResponseWriter, req *http.Request, ctx *web.Context) (
if err != nil { if err != nil {
return err return err
} }
r, err := ctx.MsgSvc.SourceReader(name, id) r, err := ctx.Manager.SourceReader(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -198,7 +198,7 @@ func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *web.Co
http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther) http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther)
return nil return nil
} }
msg, err := ctx.MsgSvc.GetMessage(name, id) msg, err := ctx.Manager.GetMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil
@@ -241,7 +241,7 @@ func MailboxViewAttach(w http.ResponseWriter, req *http.Request, ctx *web.Contex
http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther) http.Redirect(w, req, web.Reverse("RootIndex"), http.StatusSeeOther)
return nil return nil
} }
msg, err := ctx.MsgSvc.GetMessage(name, id) msg, err := ctx.Manager.GetMessage(name, id)
if err == storage.ErrNotExist { if err == storage.ErrNotExist {
http.NotFound(w, req) http.NotFound(w, req)
return nil return nil