2014-04-20 18:49:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-11-22 18:45:14 +00:00
|
|
|
"net/smtp"
|
|
|
|
"strconv"
|
|
|
|
|
2014-06-23 16:41:03 +00:00
|
|
|
"github.com/ian-kent/Go-MailHog/mailhog/config"
|
2014-06-24 21:21:06 +00:00
|
|
|
"github.com/ian-kent/Go-MailHog/mailhog/data"
|
2014-06-23 16:41:03 +00:00
|
|
|
"github.com/ian-kent/Go-MailHog/mailhog/storage"
|
2014-06-24 21:21:06 +00:00
|
|
|
"github.com/ian-kent/go-log/log"
|
2014-06-23 18:21:20 +00:00
|
|
|
gotcha "github.com/ian-kent/gotcha/app"
|
|
|
|
"github.com/ian-kent/gotcha/http"
|
2014-04-20 18:49:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type APIv1 struct {
|
2014-06-24 21:21:06 +00:00
|
|
|
config *config.Config
|
2014-04-27 22:38:43 +00:00
|
|
|
eventlisteners []*EventListener
|
2014-06-24 21:21:06 +00:00
|
|
|
app *gotcha.App
|
2014-04-27 22:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EventListener struct {
|
2014-06-23 18:21:20 +00:00
|
|
|
session *http.Session
|
2014-06-24 21:21:06 +00:00
|
|
|
ch chan []byte
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 16:31:00 +00:00
|
|
|
type ReleaseConfig struct {
|
|
|
|
Email string
|
2014-06-24 21:21:06 +00:00
|
|
|
Host string
|
|
|
|
Port string
|
2014-04-27 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func CreateAPIv1(conf *config.Config, app *gotcha.App) *APIv1 {
|
2014-04-20 18:49:05 +00:00
|
|
|
log.Println("Creating API v1")
|
|
|
|
apiv1 := &APIv1{
|
2014-06-24 21:21:06 +00:00
|
|
|
config: conf,
|
2014-04-27 22:38:43 +00:00
|
|
|
eventlisteners: make([]*EventListener, 0),
|
2014-06-24 21:21:06 +00:00
|
|
|
app: app,
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
r := app.Router
|
2014-04-27 20:13:35 +00:00
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
r.Get("/api/v1/messages/?", apiv1.messages)
|
|
|
|
r.Delete("/api/v1/messages/?", apiv1.delete_all)
|
|
|
|
r.Get("/api/v1/messages/(?P<id>[0-9a-f]+)/?", apiv1.message)
|
|
|
|
r.Delete("/api/v1/messages/(?P<id>[0-9a-f]+)/?", apiv1.delete_one)
|
|
|
|
r.Get("/api/v1/messages/(?P<id>[0-9a-f]+)/download/?", apiv1.download)
|
|
|
|
r.Get("/api/v1/messages/(?P<id>[0-9a-f]+)/mime/part/(\\d+)/download/?", apiv1.download_part)
|
|
|
|
r.Post("/api/v1/messages/(?P<id>[0-9a-f]+)/release/?", apiv1.release_one)
|
|
|
|
r.Get("/api/v1/events/?", apiv1.eventstream)
|
2014-04-27 22:38:43 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2014-06-24 21:21:06 +00:00
|
|
|
case msg := <-apiv1.config.MessageChan:
|
|
|
|
log.Println("Got message in APIv1 event stream")
|
|
|
|
bytes, _ := json.MarshalIndent(msg, "", " ")
|
|
|
|
json := string(bytes)
|
|
|
|
log.Printf("Sending content: %s\n", json)
|
|
|
|
apiv1.broadcast(json)
|
2014-04-27 22:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-04-20 18:49:05 +00:00
|
|
|
|
|
|
|
return apiv1
|
|
|
|
}
|
|
|
|
|
2014-04-27 22:38:43 +00:00
|
|
|
func (apiv1 *APIv1) broadcast(json string) {
|
|
|
|
log.Println("[APIv1] BROADCAST /api/v1/events")
|
2014-06-24 21:21:06 +00:00
|
|
|
b := []byte(json)
|
2014-04-27 22:38:43 +00:00
|
|
|
for _, l := range apiv1.eventlisteners {
|
2014-06-23 18:21:20 +00:00
|
|
|
log.Printf("Sending to connection: %s\n", l.session.Request.RemoteAddr)
|
2014-06-24 21:21:06 +00:00
|
|
|
l.ch <- b
|
2014-04-27 22:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) eventstream(session *http.Session) {
|
2014-04-27 22:38:43 +00:00
|
|
|
log.Println("[APIv1] GET /api/v1/events")
|
2014-06-23 18:21:20 +00:00
|
|
|
|
|
|
|
apiv1.eventlisteners = append(apiv1.eventlisteners, &EventListener{
|
|
|
|
session,
|
2014-06-24 20:27:07 +00:00
|
|
|
session.Response.EventStream(),
|
2014-06-23 18:21:20 +00:00
|
|
|
})
|
2014-04-27 22:38:43 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) messages(session *http.Session) {
|
2014-04-20 18:49:05 +00:00
|
|
|
log.Println("[APIv1] GET /api/v1/messages")
|
|
|
|
|
|
|
|
// TODO start, limit
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
messages, _ := apiv1.config.Storage.(*storage.MongoDB).List(0, 1000)
|
|
|
|
bytes, _ := json.Marshal(messages)
|
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
|
|
|
session.Response.Write(bytes)
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
messages, _ := apiv1.config.Storage.(*storage.InMemory).List(0, 1000)
|
2014-06-24 21:21:06 +00:00
|
|
|
bytes, _ := json.Marshal(messages)
|
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
|
|
|
session.Response.Write(bytes)
|
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) message(session *http.Session) {
|
|
|
|
id := session.Stash["id"].(string)
|
2014-04-20 19:01:53 +00:00
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
|
|
|
|
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
bytes, _ := json.Marshal(message)
|
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
|
|
|
session.Response.Write(bytes)
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.InMemory).Load(id)
|
2014-06-24 21:21:06 +00:00
|
|
|
bytes, _ := json.Marshal(message)
|
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
|
|
|
session.Response.Write(bytes)
|
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
2014-04-26 10:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) download(session *http.Session) {
|
|
|
|
id := session.Stash["id"].(string)
|
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s\n", id)
|
2014-04-26 10:50:34 +00:00
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Headers.Add("Content-Type", "message/rfc822")
|
2014-06-24 21:21:06 +00:00
|
|
|
session.Response.Headers.Add("Content-Disposition", "attachment; filename=\""+id+".eml\"")
|
2014-04-26 10:50:34 +00:00
|
|
|
|
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
for h, l := range message.Content.Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
session.Response.Write([]byte(h + ": " + v + "\r\n"))
|
2014-04-26 10:50:34 +00:00
|
|
|
}
|
2014-06-24 21:21:06 +00:00
|
|
|
}
|
|
|
|
session.Response.Write([]byte("\r\n" + message.Content.Body))
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.InMemory).Load(id)
|
2014-06-24 21:21:06 +00:00
|
|
|
for h, l := range message.Content.Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
session.Response.Write([]byte(h + ": " + v + "\r\n"))
|
2014-04-26 10:50:34 +00:00
|
|
|
}
|
2014-06-24 21:21:06 +00:00
|
|
|
}
|
|
|
|
session.Response.Write([]byte("\r\n" + message.Content.Body))
|
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) download_part(session *http.Session) {
|
|
|
|
id := session.Stash["id"].(string)
|
|
|
|
part, _ := strconv.Atoi(session.Stash["part"].(string))
|
2014-04-27 18:59:14 +00:00
|
|
|
log.Printf("[APIv1] GET /api/v1/messages/%s/mime/part/%d/download\n", id, part)
|
|
|
|
|
|
|
|
// TODO extension from content-type?
|
|
|
|
|
2014-06-24 21:21:06 +00:00
|
|
|
session.Response.Headers.Add("Content-Disposition", "attachment; filename=\""+id+"-part-"+strconv.Itoa(part)+"\"")
|
2014-04-27 18:59:14 +00:00
|
|
|
|
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
|
|
|
for h, l := range message.MIME.Parts[part].Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
session.Response.Headers.Add(h, v)
|
2014-04-27 18:59:14 +00:00
|
|
|
}
|
2014-06-24 21:21:06 +00:00
|
|
|
}
|
|
|
|
session.Response.Write([]byte("\r\n" + message.MIME.Parts[part].Body))
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
message, _ := apiv1.config.Storage.(*storage.InMemory).Load(id)
|
2014-06-24 21:21:06 +00:00
|
|
|
for h, l := range message.MIME.Parts[part].Headers {
|
|
|
|
for _, v := range l {
|
|
|
|
session.Response.Headers.Add(h, v)
|
2014-04-27 18:59:14 +00:00
|
|
|
}
|
2014-06-24 21:21:06 +00:00
|
|
|
}
|
|
|
|
session.Response.Write([]byte("\r\n" + message.MIME.Parts[part].Body))
|
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
2014-04-27 18:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) delete_all(session *http.Session) {
|
2014-04-27 19:52:46 +00:00
|
|
|
log.Println("[APIv1] POST /api/v1/messages")
|
2014-04-20 18:49:05 +00:00
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
apiv1.config.Storage.(*storage.MongoDB).DeleteAll()
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
apiv1.config.Storage.(*storage.InMemory).DeleteAll()
|
2014-06-24 21:21:06 +00:00
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
|
|
|
return
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) release_one(session *http.Session) {
|
|
|
|
id := session.Stash["id"].(string)
|
2014-04-27 16:31:00 +00:00
|
|
|
log.Printf("[APIv1] POST /api/v1/messages/%s/release\n", id)
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
2014-04-27 16:31:00 +00:00
|
|
|
var msg = &data.Message{}
|
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
msg, _ = apiv1.config.Storage.(*storage.MongoDB).Load(id)
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
msg, _ = apiv1.config.Storage.(*storage.InMemory).Load(id)
|
2014-06-24 21:21:06 +00:00
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
|
|
|
return
|
2014-04-27 16:31:00 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
decoder := json.NewDecoder(session.Request.Body())
|
2014-04-27 16:31:00 +00:00
|
|
|
var cfg ReleaseConfig
|
|
|
|
err := decoder.Decode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error decoding request body: %s", err)
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Status = 500
|
|
|
|
session.Response.Write([]byte("Error decoding request body"))
|
2014-04-27 16:31:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Releasing to %s (via %s:%s)", cfg.Email, cfg.Host, cfg.Port)
|
2014-11-22 18:45:14 +00:00
|
|
|
log.Printf("Got message: %s", msg.ID)
|
2014-04-27 16:31:00 +00:00
|
|
|
|
|
|
|
bytes := make([]byte, 0)
|
|
|
|
for h, l := range msg.Content.Headers {
|
|
|
|
for _, v := range l {
|
2014-06-24 21:21:06 +00:00
|
|
|
bytes = append(bytes, []byte(h+": "+v+"\r\n")...)
|
2014-04-27 16:31:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-24 21:21:06 +00:00
|
|
|
bytes = append(bytes, []byte("\r\n"+msg.Content.Body)...)
|
2014-04-27 16:31:00 +00:00
|
|
|
|
2014-06-24 21:21:06 +00:00
|
|
|
err = smtp.SendMail(cfg.Host+":"+cfg.Port, nil, "nobody@"+apiv1.config.Hostname, []string{cfg.Email}, bytes)
|
2014-04-27 16:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to release message: %s", err)
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Status = 500
|
2014-04-27 16:31:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("Message released successfully")
|
|
|
|
}
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
func (apiv1 *APIv1) delete_one(session *http.Session) {
|
|
|
|
id := session.Stash["id"].(string)
|
2014-04-20 18:49:05 +00:00
|
|
|
log.Printf("[APIv1] POST /api/v1/messages/%s/delete\n", id)
|
|
|
|
|
2014-06-23 18:21:20 +00:00
|
|
|
session.Response.Headers.Add("Content-Type", "text/json")
|
2014-04-23 23:22:50 +00:00
|
|
|
switch apiv1.config.Storage.(type) {
|
2014-06-24 21:21:06 +00:00
|
|
|
case *storage.MongoDB:
|
|
|
|
apiv1.config.Storage.(*storage.MongoDB).DeleteOne(id)
|
2014-11-22 18:45:14 +00:00
|
|
|
case *storage.InMemory:
|
|
|
|
apiv1.config.Storage.(*storage.InMemory).DeleteOne(id)
|
2014-06-24 21:21:06 +00:00
|
|
|
default:
|
|
|
|
session.Response.Status = 500
|
2014-04-23 23:22:50 +00:00
|
|
|
}
|
2014-04-20 18:49:05 +00:00
|
|
|
}
|