From 219862797e0eed58b82cc88f92b4e6944fdd10ab Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Mon, 12 Mar 2018 20:49:06 -0700 Subject: [PATCH] web: remove DataStore from Context and controllers for #81 --- cmd/inbucket/main.go | 2 +- pkg/rest/apiv1_controller.go | 10 +++++----- pkg/rest/apiv1_controller_test.go | 6 ++---- pkg/rest/testutils_test.go | 4 ++-- pkg/server/web/context.go | 7 ++----- pkg/server/web/server.go | 12 +++--------- pkg/webui/mailbox_controller.go | 12 ++++++------ 7 files changed, 21 insertions(+), 32 deletions(-) diff --git a/cmd/inbucket/main.go b/cmd/inbucket/main.go index ff01d3f..7020676 100644 --- a/cmd/inbucket/main.go +++ b/cmd/inbucket/main.go @@ -125,7 +125,7 @@ func main() { // Start HTTP server mm := &message.StoreManager{Store: ds} - web.Initialize(config.GetWebConfig(), shutdownChan, mm, ds, msgHub) + web.Initialize(config.GetWebConfig(), shutdownChan, mm, msgHub) webui.SetupRoutes(web.Router) rest.SetupRoutes(web.Router) go web.Start(rootCtx) diff --git a/pkg/rest/apiv1_controller.go b/pkg/rest/apiv1_controller.go index 135669e..7b40201 100644 --- a/pkg/rest/apiv1_controller.go +++ b/pkg/rest/apiv1_controller.go @@ -24,7 +24,7 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( if err != nil { return err } - messages, err := ctx.MsgSvc.GetMetadata(name) + messages, err := ctx.Manager.GetMetadata(name) if err != nil { // This doesn't indicate empty, likely an IO error 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 { return err } - msg, err := ctx.MsgSvc.GetMessage(name, id) + msg, err := ctx.Manager.GetMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil @@ -105,7 +105,7 @@ func MailboxPurgeV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) return err } // Delete all messages - err = ctx.MsgSvc.PurgeMessages(name) + err = ctx.Manager.PurgeMessages(name) if err != nil { 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 } - r, err := ctx.MsgSvc.SourceReader(name, id) + r, err := ctx.Manager.SourceReader(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil @@ -146,7 +146,7 @@ func MailboxDeleteV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) if err != nil { return err } - err = ctx.MsgSvc.RemoveMessage(name, id) + err = ctx.Manager.RemoveMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil diff --git a/pkg/rest/apiv1_controller_test.go b/pkg/rest/apiv1_controller_test.go index 48d2841..b31cfcb 100644 --- a/pkg/rest/apiv1_controller_test.go +++ b/pkg/rest/apiv1_controller_test.go @@ -34,9 +34,8 @@ const ( func TestRestMailboxList(t *testing.T) { // Setup - ds := test.NewStore() mm := test.NewManager() - logbuf := setupWebServer(mm, ds) + logbuf := setupWebServer(mm) // Test invalid mailbox name w, err := testRestGet(baseURL + "/mailbox/foo@bar") @@ -165,9 +164,8 @@ func TestRestMailboxList(t *testing.T) { func TestRestMessage(t *testing.T) { // Setup - ds := test.NewStore() mm := test.NewManager() - logbuf := setupWebServer(mm, ds) + logbuf := setupWebServer(mm) // Test invalid mailbox name w, err := testRestGet(baseURL + "/mailbox/foo@bar/0001") diff --git a/pkg/rest/testutils_test.go b/pkg/rest/testutils_test.go index 4bdcae5..cfddede 100644 --- a/pkg/rest/testutils_test.go +++ b/pkg/rest/testutils_test.go @@ -194,7 +194,7 @@ func testRestGet(url string) (*httptest.ResponseRecorder, error) { return w, nil } -func setupWebServer(mm message.Manager, ds storage.Store) *bytes.Buffer { +func setupWebServer(mm message.Manager) *bytes.Buffer { // Capture log output buf := new(bytes.Buffer) log.SetOutput(buf) @@ -206,7 +206,7 @@ func setupWebServer(mm message.Manager, ds storage.Store) *bytes.Buffer { PublicDir: "../themes/bootstrap/public", } shutdownChan := make(chan bool) - web.Initialize(cfg, shutdownChan, mm, ds, &msghub.Hub{}) + web.Initialize(cfg, shutdownChan, mm, &msghub.Hub{}) SetupRoutes(web.Router) return buf diff --git a/pkg/server/web/context.go b/pkg/server/web/context.go index af5f6b2..9ec2ab8 100644 --- a/pkg/server/web/context.go +++ b/pkg/server/web/context.go @@ -9,16 +9,14 @@ import ( "github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/message" "github.com/jhillyerd/inbucket/pkg/msghub" - "github.com/jhillyerd/inbucket/pkg/storage" ) // Context is passed into every request handler function type Context struct { Vars map[string]string Session *sessions.Session - DataStore storage.Store MsgHub *msghub.Hub - MsgSvc message.Manager + Manager message.Manager WebConfig config.WebConfig IsJSON bool } @@ -61,9 +59,8 @@ func NewContext(req *http.Request) (*Context, error) { ctx := &Context{ Vars: vars, Session: sess, - DataStore: DataStore, MsgHub: msgHub, - MsgSvc: msgSvc, + Manager: manager, WebConfig: webConfig, IsJSON: headerMatch(req, "Accept", "application/json"), } diff --git a/pkg/server/web/server.go b/pkg/server/web/server.go index c76a2f7..b3e8610 100644 --- a/pkg/server/web/server.go +++ b/pkg/server/web/server.go @@ -16,19 +16,15 @@ import ( "github.com/jhillyerd/inbucket/pkg/log" "github.com/jhillyerd/inbucket/pkg/message" "github.com/jhillyerd/inbucket/pkg/msghub" - "github.com/jhillyerd/inbucket/pkg/storage" ) // Handler is a function type that handles an HTTP request in Inbucket type Handler func(http.ResponseWriter, *http.Request, *Context) error var ( - // DataStore is where all the mailboxes and messages live - DataStore storage.Store - // msgHub holds a reference to the message pub/sub system - msgHub *msghub.Hub - msgSvc message.Manager + msgHub *msghub.Hub + manager message.Manager // Router is shared between httpd, webui and rest packages. It sends // incoming requests to the correct handler function @@ -54,16 +50,14 @@ func Initialize( cfg config.WebConfig, shutdownChan chan bool, mm message.Manager, - ds storage.Store, mh *msghub.Hub) { webConfig = cfg globalShutdown = shutdownChan // NewContext() will use this DataStore for the web handlers - DataStore = ds msgHub = mh - msgSvc = mm + manager = mm // Content Paths log.Infof("HTTP templates mapped to %q", cfg.TemplateDir) diff --git a/pkg/webui/mailbox_controller.go b/pkg/webui/mailbox_controller.go index ed092e7..16aa305 100644 --- a/pkg/webui/mailbox_controller.go +++ b/pkg/webui/mailbox_controller.go @@ -72,7 +72,7 @@ func MailboxList(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er if err != nil { return err } - messages, err := ctx.MsgSvc.GetMetadata(name) + messages, err := ctx.Manager.GetMetadata(name) if err != nil { // This doesn't indicate empty, likely an IO error 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 { return err } - msg, err := ctx.MsgSvc.GetMessage(name, id) + msg, err := ctx.Manager.GetMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil @@ -135,7 +135,7 @@ func MailboxHTML(w http.ResponseWriter, req *http.Request, ctx *web.Context) (er if err != nil { return err } - msg, err := ctx.MsgSvc.GetMessage(name, id) + msg, err := ctx.Manager.GetMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil @@ -163,7 +163,7 @@ func MailboxSource(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( if err != nil { return err } - r, err := ctx.MsgSvc.SourceReader(name, id) + r, err := ctx.Manager.SourceReader(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) 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) return nil } - msg, err := ctx.MsgSvc.GetMessage(name, id) + msg, err := ctx.Manager.GetMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) 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) return nil } - msg, err := ctx.MsgSvc.GetMessage(name, id) + msg, err := ctx.Manager.GetMessage(name, id) if err == storage.ErrNotExist { http.NotFound(w, req) return nil