From ecf4d071c207cd1d9b3463b06ca6906ef26eaa0f Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Sun, 20 Apr 2014 17:09:06 +0100 Subject: [PATCH] Delete single message api --- mailhog/http/server.go | 24 +++++++++++++++++++++--- mailhog/storage/mongodb.go | 22 ++++++++++++++++++++++ mailhog/templates/js/controllers.go | 2 +- mailhog/templates/layout.go | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/mailhog/http/server.go b/mailhog/http/server.go index 33520ed..d8cf27d 100644 --- a/mailhog/http/server.go +++ b/mailhog/http/server.go @@ -3,6 +3,7 @@ package http import ( "encoding/json" "net/http" + "regexp" "github.com/ian-kent/MailHog/mailhog" "github.com/ian-kent/MailHog/mailhog/templates" "github.com/ian-kent/MailHog/mailhog/storage" @@ -43,12 +44,30 @@ func web_headers(w http.ResponseWriter) { } func api_messages(w http.ResponseWriter, r *http.Request) { + re, _ := regexp.Compile("/api/v1/messages/([0-9a-f]+)/delete") + match := re.FindStringSubmatch(r.URL.Path) + if len(match) > 0 { + api_delete_one(w, r, match[1]) + return + } + + // TODO start, limit messages, _ := storage.List(config, 0, 1000) bytes, _ := json.Marshal(messages) w.Header().Set("Content-Type", "text/json") w.Write(bytes) } +func api_delete_all(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/json") + storage.DeleteAll(config) +} + +func api_delete_one(w http.ResponseWriter, r *http.Request, id string) { + w.Header().Set("Content-Type", "text/json") + storage.DeleteOne(config, id) +} + func Start(exitCh chan int, conf *mailhog.Config) { exitChannel = exitCh config = conf @@ -57,8 +76,7 @@ func Start(exitCh chan int, conf *mailhog.Config) { http.HandleFunc("/js/controllers.js", web_jscontroller) http.HandleFunc("/images/hog.png", web_imgcontroller) http.HandleFunc("/", web_index) - http.HandleFunc("/api/v1/messages", api_messages) - //http.HandleFunc("/api/v1/messages/delete", api_delete_all) - //http.HandleFunc("/api/v1/messages/:message_id/delete", api_delete_message) + http.HandleFunc("/api/v1/messages/", api_messages) + http.HandleFunc("/api/v1/messages/delete", api_delete_all) http.ListenAndServe(conf.HTTPBindAddr, nil) } diff --git a/mailhog/storage/mongodb.go b/mailhog/storage/mongodb.go index 10ae3d9..296b7ee 100644 --- a/mailhog/storage/mongodb.go +++ b/mailhog/storage/mongodb.go @@ -40,6 +40,28 @@ func List(c *mailhog.Config, start int, limit int) (*data.Messages, error) { return messages, nil; } +func DeleteOne(c *mailhog.Config, id string) error { + session, err := mgo.Dial(c.MongoUri) + if(err != nil) { + log.Printf("Error connecting to MongoDB: %s", err) + return err + } + defer session.Close() + _, err = session.DB(c.MongoDb).C(c.MongoColl).RemoveAll(bson.M{"id": id}) + return err +} + +func DeleteAll(c *mailhog.Config) error { + session, err := mgo.Dial(c.MongoUri) + if(err != nil) { + log.Printf("Error connecting to MongoDB: %s", err) + return err + } + defer session.Close() + _, err = session.DB(c.MongoDb).C(c.MongoColl).RemoveAll(bson.M{}) + return err +} + func Load(c *mailhog.Config, id string) (*data.Message, error) { session, err := mgo.Dial(c.MongoUri) if(err != nil) { diff --git a/mailhog/templates/js/controllers.go b/mailhog/templates/js/controllers.go index e9befaa..ab12028 100644 --- a/mailhog/templates/js/controllers.go +++ b/mailhog/templates/js/controllers.go @@ -33,7 +33,7 @@ mailhogApp.controller('MailCtrl', function ($scope, $http) { } $scope.deleteOne = function(message) { - $http.post('/api/v1/messages/' + message._id + '/delete').success(function() { + $http.post('/api/v1/messages/' + message.Id + '/delete').success(function() { if($scope.preview._id == message._id) $scope.preview = null; $scope.refresh(); }); diff --git a/mailhog/templates/layout.go b/mailhog/templates/layout.go index b5d76dc..747d050 100644 --- a/mailhog/templates/layout.go +++ b/mailhog/templates/layout.go @@ -53,7 +53,7 @@ func Layout(content string) string {
  • Delete all messages
  • -
  • GitHub
  • +
  • GitHub
  • <%= content %>