Remove storage

This commit is contained in:
Ian Kent 2014-12-24 17:05:50 +00:00
parent ebbae16589
commit 37670fbc78
3 changed files with 0 additions and 155 deletions

View file

@ -1,62 +0,0 @@
package storage
import "github.com/ian-kent/Go-MailHog/data"
// InMemory is an in memory storage backend
type InMemory struct {
Messages map[string]*data.Message
MessageIndex []string
MessageRIndex map[string]int
}
// CreateInMemory creates a new in memory storage backend
func CreateInMemory() *InMemory {
return &InMemory{
Messages: make(map[string]*data.Message, 0),
MessageIndex: make([]string, 0),
MessageRIndex: make(map[string]int, 0),
}
}
// Store stores a message and returns its storage ID
func (memory *InMemory) Store(m *data.Message) (string, error) {
memory.Messages[string(m.ID)] = m
memory.MessageIndex = append(memory.MessageIndex, string(m.ID))
memory.MessageRIndex[string(m.ID)] = len(memory.MessageIndex) - 1
return string(m.ID), nil
}
// List lists stored messages by index
func (memory *InMemory) List(start int, limit int) (*data.Messages, error) {
if limit > len(memory.MessageIndex) {
limit = len(memory.MessageIndex)
}
var messages []data.Message
for _, m := range memory.MessageIndex[start:limit] {
messages = append(messages, *memory.Messages[m])
}
msgs := data.Messages(messages)
return &msgs, nil
}
// DeleteOne deletes an individual message by storage ID
func (memory *InMemory) DeleteOne(id string) error {
index := memory.MessageRIndex[string(id)]
delete(memory.Messages, string(id))
memory.MessageIndex = append(memory.MessageIndex[:index], memory.MessageIndex[index+1:]...)
delete(memory.MessageRIndex, string(id))
return nil
}
// DeleteAll deletes all in memory messages
func (memory *InMemory) DeleteAll() error {
memory.Messages = make(map[string]*data.Message, 0)
memory.MessageIndex = make([]string, 0)
memory.MessageRIndex = make(map[string]int, 0)
return nil
}
// Load returns an individual message by storage ID
func (memory *InMemory) Load(id string) (*data.Message, error) {
return memory.Messages[string(id)], nil
}

View file

@ -1,81 +0,0 @@
package storage
import (
"log"
"github.com/ian-kent/Go-MailHog/data"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)
// MongoDB represents MongoDB backed storage backend
type MongoDB struct {
Session *mgo.Session
Collection *mgo.Collection
}
// CreateMongoDB creates a MongoDB backed storage backend
func CreateMongoDB(uri, db, coll string) *MongoDB {
log.Printf("Connecting to MongoDB: %s\n", uri)
session, err := mgo.Dial(uri)
if err != nil {
log.Printf("Error connecting to MongoDB: %s", err)
return nil
}
return &MongoDB{
Session: session,
Collection: session.DB(db).C(coll),
}
}
// Store stores a message in MongoDB and returns its storage ID
func (mongo *MongoDB) Store(m *data.Message) (string, error) {
err := mongo.Collection.Insert(m)
if err != nil {
log.Printf("Error inserting message: %s", err)
return "", err
}
return string(m.ID), nil
}
// List returns a list of messages by index
func (mongo *MongoDB) List(start int, limit int) (*data.Messages, error) {
messages := &data.Messages{}
err := mongo.Collection.Find(bson.M{}).Skip(start).Limit(limit).Select(bson.M{
"id": 1,
"_id": 1,
"from": 1,
"to": 1,
"content.headers": 1,
"content.size": 1,
"created": 1,
}).All(messages)
if err != nil {
log.Printf("Error loading messages: %s", err)
return nil, err
}
return messages, nil
}
// DeleteOne deletes an individual message by storage ID
func (mongo *MongoDB) DeleteOne(id string) error {
_, err := mongo.Collection.RemoveAll(bson.M{"id": id})
return err
}
// DeleteAll deletes all messages stored in MongoDB
func (mongo *MongoDB) DeleteAll() error {
_, err := mongo.Collection.RemoveAll(bson.M{})
return err
}
// Load loads an individual message by storage ID
func (mongo *MongoDB) Load(id string) (*data.Message, error) {
result := &data.Message{}
err := mongo.Collection.Find(bson.M{"id": id}).One(&result)
if err != nil {
log.Printf("Error loading message: %s", err)
return nil, err
}
return result, nil
}

View file

@ -1,12 +0,0 @@
package storage
import "github.com/ian-kent/Go-MailHog/data"
// Storage represents a storage backend
type Storage interface {
Store(m *data.Message) (string, error)
List(start int, limit int) (*data.Messages, error)
DeleteOne(id string) error
DeleteAll() error
Load(id string) (*data.Message, error)
}