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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"),
|
||||
}
|
||||
|
||||
@@ -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
|
||||
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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user