Delete single message api

This commit is contained in:
Ian Kent 2014-04-20 17:09:06 +01:00
parent 305672472a
commit ecf4d071c2
4 changed files with 45 additions and 5 deletions

View file

@ -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)
}

View file

@ -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) {

View file

@ -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();
});

View file

@ -53,7 +53,7 @@ func Layout(content string) string {
<li><a href="#" ng-click="deleteAll()">Delete all messages</a></li>
</ul>
</li>
<li><a target="_blank" href="https://github.com/ian-kent/MailHog">GitHub</a></li>
<li><a target="_blank" href="https://github.com/ian-kent/Go-MailHog">GitHub</a></li>
</ul>
</nav>
<%= content %>