mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
Can now list mailbox contents via REST
This commit is contained in:
1
bin/mailbox-list.sh
Executable file
1
bin/mailbox-list.sh
Executable file
@@ -0,0 +1 @@
|
|||||||
|
curl -i -H "Accept: application/json" --noproxy localhost http://localhost:9000/mailbox/list/$1
|
||||||
@@ -5,18 +5,37 @@ import (
|
|||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
"github.com/jhillyerd/inbucket/smtpd"
|
"github.com/jhillyerd/inbucket/smtpd"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Vars map[string]string
|
Vars map[string]string
|
||||||
Session *sessions.Session
|
Session *sessions.Session
|
||||||
DataStore smtpd.DataStore
|
DataStore smtpd.DataStore
|
||||||
|
IsJson bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) Close() {
|
func (c *Context) Close() {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// headerMatch returns true if the request header specified by name contains
|
||||||
|
// the specified value. Case is ignored.
|
||||||
|
func headerMatch(req *http.Request, name string, value string) bool {
|
||||||
|
name = http.CanonicalHeaderKey(name)
|
||||||
|
value = strings.ToLower(value)
|
||||||
|
|
||||||
|
if header := req.Header[name]; header != nil {
|
||||||
|
for _, hv := range header {
|
||||||
|
if value == strings.ToLower(hv) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func NewContext(req *http.Request) (*Context, error) {
|
func NewContext(req *http.Request) (*Context, error) {
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
sess, err := sessionStore.Get(req, "inbucket")
|
sess, err := sessionStore.Get(req, "inbucket")
|
||||||
@@ -25,6 +44,7 @@ func NewContext(req *http.Request) (*Context, error) {
|
|||||||
Vars: vars,
|
Vars: vars,
|
||||||
Session: sess,
|
Session: sess,
|
||||||
DataStore: ds,
|
DataStore: ds,
|
||||||
|
IsJson: headerMatch(req, "Accept", "application/json"),
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx, err
|
return ctx, err
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type JsonMessageHeader struct {
|
||||||
|
From, Subject, Date string
|
||||||
|
}
|
||||||
|
|
||||||
func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
||||||
name := req.FormValue("name")
|
name := req.FormValue("name")
|
||||||
if len(name) == 0 {
|
if len(name) == 0 {
|
||||||
@@ -37,11 +41,23 @@ func MailboxList(w http.ResponseWriter, req *http.Request, ctx *Context) (err er
|
|||||||
}
|
}
|
||||||
log.LogTrace("Got %v messsages", len(messages))
|
log.LogTrace("Got %v messsages", len(messages))
|
||||||
|
|
||||||
return RenderPartial("mailbox/_list.html", w, map[string]interface{}{
|
if ctx.IsJson {
|
||||||
"ctx": ctx,
|
jmessages := make([]*JsonMessageHeader, len(messages))
|
||||||
"name": name,
|
for i, msg := range messages {
|
||||||
"messages": messages,
|
jmessages[i] = &JsonMessageHeader{
|
||||||
})
|
From: msg.From(),
|
||||||
|
Subject: msg.Subject(),
|
||||||
|
Date: msg.Date().String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return RenderJson(w, jmessages)
|
||||||
|
} else {
|
||||||
|
return RenderPartial("mailbox/_list.html", w, map[string]interface{}{
|
||||||
|
"ctx": ctx,
|
||||||
|
"name": name,
|
||||||
|
"messages": messages,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
func MailboxShow(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
||||||
|
|||||||
13
web/rest.go
Normal file
13
web/rest.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RenderJson(w http.ResponseWriter, data interface{}) error {
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
|
w.Header().Set("Expires", "-1")
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
return enc.Encode(data)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user