mirror of
https://github.com/axllent/mailpit.git
synced 2026-03-03 05:47:01 +00:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b57e340389 | ||
|
|
b9043b6c39 | ||
|
|
5860171002 | ||
|
|
ad49bf2898 | ||
|
|
2d221a6b67 | ||
|
|
4f266cd3f3 | ||
|
|
9fc7202552 | ||
|
|
22a476ded5 | ||
|
|
54d3f6e3ad | ||
|
|
cbe61e3f2e | ||
|
|
3b65a8852e | ||
|
|
970a534d77 | ||
|
|
f7502b1c14 | ||
|
|
e0f7d88d61 | ||
|
|
fc8148bfb3 | ||
|
|
74fe6d55b4 | ||
|
|
47376d4db9 | ||
|
|
4b9b60f247 | ||
|
|
123b0f19db | ||
|
|
9fed08245a | ||
|
|
f807c166f7 | ||
|
|
9d257dd3c0 | ||
|
|
f74bb70499 | ||
|
|
802f6f5672 | ||
|
|
19966fad81 |
2
.dockerignore
Normal file
2
.dockerignore
Normal file
@@ -0,0 +1,2 @@
|
||||
/node_modules
|
||||
/mailpit
|
||||
37
.github/workflows/build-docker.yml
vendored
Normal file
37
.github/workflows/build-docker.yml
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
|
||||
name: Build docker images
|
||||
jobs:
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Get tag
|
||||
id: tag
|
||||
uses: dawidd6/action-get-tag@v1
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v2
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Login to DockerHub
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v3
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64,linux/arm64,linux/arm
|
||||
build-args: |
|
||||
"VERSION=${{ steps.tag.outputs.tag }}"
|
||||
push: true
|
||||
tags: axllent/mailpit:latest,axllent/mailpit:${{ steps.tag.outputs.tag }}
|
||||
1
.github/workflows/release-build.yml
vendored
1
.github/workflows/release-build.yml
vendored
@@ -38,6 +38,7 @@ jobs:
|
||||
goos: ${{ matrix.goos }}
|
||||
goarch: ${{ matrix.goarch }}
|
||||
binary_name: "mailpit"
|
||||
pre_command: export CGO_ENABLED=0
|
||||
asset_name: mailpit-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
extra_files: LICENSE README.md
|
||||
md5sum: false
|
||||
|
||||
33
CHANGELOG.md
33
CHANGELOG.md
@@ -3,6 +3,39 @@
|
||||
Notable changes to Mailpit will be documented in this file.
|
||||
|
||||
|
||||
## 0.0.9
|
||||
|
||||
### Bugfix
|
||||
- Include read status in search results
|
||||
|
||||
### Feature
|
||||
- HTTPS option for web UI
|
||||
|
||||
### Testing
|
||||
- Memory & physical database tests
|
||||
|
||||
|
||||
## 0.0.8
|
||||
|
||||
### Bugfix
|
||||
- Fix total/unread count after failed message inserts
|
||||
|
||||
### UI
|
||||
- Add project links to help in CLI
|
||||
|
||||
|
||||
## 0.0.7
|
||||
|
||||
### Bugfix
|
||||
- Command flag should be `--auth-file`
|
||||
|
||||
|
||||
## 0.0.6
|
||||
|
||||
### Bugfix
|
||||
- Disable CGO when building multi-arch binaries
|
||||
|
||||
|
||||
## 0.0.5
|
||||
|
||||
### Feature
|
||||
|
||||
20
Dockerfile
Normal file
20
Dockerfile
Normal file
@@ -0,0 +1,20 @@
|
||||
FROM golang:alpine as builder
|
||||
|
||||
ARG VERSION=dev
|
||||
|
||||
COPY . /app
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apk add --no-cache git npm && \
|
||||
npm install && npm run package && \
|
||||
CGO_ENABLED=0 go build -ldflags "-s -w -X github.com/axllent/mailpit/cmd.Version=${VERSION}" -o /mailpit
|
||||
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
COPY --from=builder /mailpit /mailpit
|
||||
|
||||
RUN apk add --no-cache tzdata
|
||||
|
||||
ENTRYPOINT ["/mailpit"]
|
||||
@@ -1,45 +0,0 @@
|
||||
# Building Mailpit from source
|
||||
|
||||
Go (>= version 1.8) and npm are required to compile mailpit from source.
|
||||
|
||||
```
|
||||
git clone git@github.com:axllent/mailpit.git
|
||||
cd mailpit
|
||||
```
|
||||
|
||||
## Building the UI
|
||||
|
||||
The Mailpit web user interface is built with node. In the project's root (top) directory run the following to install the required node modules:
|
||||
|
||||
|
||||
### Installing the node modules
|
||||
```
|
||||
npm install
|
||||
```
|
||||
|
||||
|
||||
### Building the web UI
|
||||
|
||||
```
|
||||
npm run build
|
||||
```
|
||||
|
||||
You can also run `npm run watch` which will watch for changes and rebuild the HTML/CSS/JS automatically when changes are detected.
|
||||
Please note that you must restart Mailpit (`go run .`) to run with the changes.
|
||||
|
||||
|
||||
## Build the mailpit binary
|
||||
|
||||
One you have the assets compiled, you can build mailpit as follows:
|
||||
```
|
||||
go build -ldflags "-s -w"
|
||||
```
|
||||
|
||||
## Building a stand-alone sendmail binary
|
||||
|
||||
This step is unnecessary, however if you do not intend to either symlink `sendmail` to mailpit or configure your existing sendmail to route mail to mailpit, you can optionally build a stand-alone sendmail binary.
|
||||
|
||||
```
|
||||
cd sendmail
|
||||
go build -ldflags "-s -w"
|
||||
```
|
||||
14
README.md
14
README.md
@@ -6,32 +6,34 @@ It acts as both an SMTP server, and provides a web interface to view all capture
|
||||
|
||||
Mailpit is inspired by [MailHog](#why-rewrite-mailhog), but much, much faster.
|
||||
|
||||

|
||||
|
||||
|
||||
## Features
|
||||
|
||||
- Runs completely on a single binary
|
||||
- SMTP server (default `0.0.0.0:1025`)
|
||||
- Web UI to view emails (HTML format, text, source and MIME attachments, default `0.0.0.0:8025`)
|
||||
- Optional HTTPS for web UI ([see wiki](https://github.com/axllent/mailpit/wiki/HTTPS))
|
||||
- Real-time web UI updates using web sockets for new mail
|
||||
- Optional basic authentication for web UI (see [wiki](https://github.com/axllent/mailpit/wiki/Basic-authentication))
|
||||
- Optional basic authentication for web UI ([see wiki](https://github.com/axllent/mailpit/wiki/Basic-authentication))
|
||||
- Email storage in either memory or disk (using [CloverDB](https://github.com/ostafen/clover)) - note that in-memory has a physical limit of 1MB per email
|
||||
- Configurable automatic email pruning (default keeps the most recent 500 emails)
|
||||
- Fast SMTP processing & storing - approximately 300-600 emails per second depending on CPU, network speed & email size
|
||||
- Can handle tens of thousands of emails
|
||||
- Multi-arch [Docker images](https://github.com/axllent/mailpit/wiki/Docker-images)
|
||||
|
||||
|
||||
## Planned features
|
||||
|
||||
- Optional HTTPS for web UI
|
||||
- Browser notifications for new mail (HTTPS only)
|
||||
- Docker container
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Download a pre-built binary in the [releases](https://github.com/axllent/mailpit/releases/latest). The `mailpit` can be placed in your `$PATH`, or simply run as `./mailpit`. See `mailpit -h` for options.
|
||||
|
||||
To build mailpit from source see [building from source](README-BUILDING.md).
|
||||
To build Mailpit from source see [building from source](https://github.com/axllent/mailpit/wiki/Building-from-source).
|
||||
|
||||
|
||||
### Configuring sendmail
|
||||
@@ -43,11 +45,11 @@ You can use `mailpit sendmail` as your sendmail configuration in `php.ini`:
|
||||
sendmail_path = /usr/local/bin/mailpit sendmail
|
||||
```
|
||||
|
||||
If mailpit is found on the same host as sendmail, you can symlink the mailpit binary to sendmail, eg: `ln -s /usr/local/bin/mailpit /usr/sbin/sendmail` (only if mailpit is running on default 1025 port).
|
||||
If Mailpit is found on the same host as sendmail, you can symlink the Mailpit binary to sendmail, eg: `ln -s /usr/local/bin/mailpit /usr/sbin/sendmail` (only if Mailpit is running on default 1025 port).
|
||||
|
||||
You can use your default system `sendmail` binary to route directly to port `1025` (configurable) by calling `/usr/sbin/sendmail -S localhost:1025`.
|
||||
|
||||
You can build a mailpit-specific sendmail binary from source ( see [building from source](README-BUILDING.md)).
|
||||
You can build a Mailpit-specific sendmail binary from source ( see [building from source](https://github.com/axllent/mailpit/wiki/Building-from-source)).
|
||||
|
||||
|
||||
## Why rewrite MailHog?
|
||||
|
||||
25
cmd/root.go
25
cmd/root.go
@@ -20,7 +20,11 @@ var rootCmd = &cobra.Command{
|
||||
Short: "Mailpit is an email testing tool for developers",
|
||||
Long: `Mailpit is an email testing tool for developers.
|
||||
|
||||
It acts as an SMTP server, and provides a web interface to view all captured emails.`,
|
||||
It acts as an SMTP server, and provides a web interface to view all captured emails.
|
||||
|
||||
Documentation:
|
||||
https://github.com/axllent/mailpit
|
||||
https://github.com/axllent/mailpit/wiki`,
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
if err := config.VerifyConfig(); err != nil {
|
||||
logger.Log().Error(err.Error())
|
||||
@@ -60,9 +64,12 @@ func SendmailExecute() {
|
||||
func init() {
|
||||
// hide autocompletion
|
||||
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
||||
// rootCmd.Flags().SortFlags = false
|
||||
// hide help
|
||||
rootCmd.Flags().SortFlags = false
|
||||
// hide help command
|
||||
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})
|
||||
// hide help flag
|
||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "This help")
|
||||
rootCmd.PersistentFlags().Lookup("help").Hidden = true
|
||||
|
||||
// defaults from envars if provided
|
||||
if len(os.Getenv("MP_DATA_DIR")) > 0 {
|
||||
@@ -80,11 +87,19 @@ func init() {
|
||||
if len(os.Getenv("MP_AUTH_FILE")) > 0 {
|
||||
config.AuthFile = os.Getenv("MP_AUTH_FILE")
|
||||
}
|
||||
if len(os.Getenv("MP_SSL_CERT")) > 0 {
|
||||
config.SSLCert = os.Getenv("MP_SSL_CERT")
|
||||
}
|
||||
if len(os.Getenv("MP_SSL_KEY")) > 0 {
|
||||
config.SSLKey = os.Getenv("MP_SSL_KEY")
|
||||
}
|
||||
|
||||
rootCmd.Flags().StringVarP(&config.DataDir, "data", "d", config.DataDir, "Optional path to store peristent data")
|
||||
rootCmd.Flags().StringVarP(&config.SMTPListen, "smtp", "s", config.SMTPListen, "SMTP bind interface and port")
|
||||
rootCmd.Flags().StringVarP(&config.HTTPListen, "listen", "l", config.HTTPListen, "HTTP bind interface and port for UI")
|
||||
rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages per mailbox")
|
||||
rootCmd.Flags().StringVarP(&config.AuthFile, "-auth-file", "a", config.AuthFile, "A username:bcryptpw mapping file")
|
||||
rootCmd.Flags().IntVarP(&config.MaxMessages, "max", "m", config.MaxMessages, "Max number of messages to store")
|
||||
rootCmd.Flags().StringVarP(&config.AuthFile, "auth-file", "a", config.AuthFile, "A password file for authentication (see wiki)")
|
||||
rootCmd.Flags().StringVar(&config.SSLCert, "ssl-cert", config.SSLCert, "SSL certificate - requires ssl-key (see wiki)")
|
||||
rootCmd.Flags().StringVar(&config.SSLKey, "ssl-key", config.SSLKey, "SSL key - requires ssl-cert (see wiki)")
|
||||
rootCmd.Flags().BoolVarP(&config.VerboseLogging, "verbose", "v", false, "Verbose logging")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"github.com/tg123/go-htpasswd"
|
||||
@@ -26,9 +28,10 @@ var (
|
||||
// NoLogging for tests
|
||||
NoLogging = false
|
||||
|
||||
// SSLCert @TODO
|
||||
// SSLCert file
|
||||
SSLCert string
|
||||
// SSLKey @TODO
|
||||
|
||||
// SSLKey file
|
||||
SSLKey string
|
||||
|
||||
// AuthFile for basic authentication
|
||||
@@ -49,6 +52,10 @@ func VerifyConfig() error {
|
||||
}
|
||||
|
||||
if AuthFile != "" {
|
||||
if !isFile(AuthFile) {
|
||||
return fmt.Errorf("password file not found: %s", AuthFile)
|
||||
}
|
||||
|
||||
a, err := htpasswd.New(AuthFile, htpasswd.DefaultSystems, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -56,5 +63,29 @@ func VerifyConfig() error {
|
||||
Auth = a
|
||||
}
|
||||
|
||||
if SSLCert != "" && SSLKey == "" || SSLCert == "" && SSLKey != "" {
|
||||
return errors.New("you must provide both an SSL certificate and a key")
|
||||
}
|
||||
|
||||
if SSLCert != "" {
|
||||
if !isFile(SSLCert) {
|
||||
return fmt.Errorf("SSL certificate not found: %s", SSLCert)
|
||||
}
|
||||
|
||||
if !isFile(SSLKey) {
|
||||
return fmt.Errorf("SSL key not found: %s", SSLKey)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsFile returns if a path is a file
|
||||
func isFile(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if os.IsNotExist(err) || !info.Mode().IsRegular() {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
BIN
screenshot.png
Normal file
BIN
screenshot.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 34 KiB |
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"net"
|
||||
"net/mail"
|
||||
"regexp"
|
||||
|
||||
"github.com/axllent/mailpit/config"
|
||||
"github.com/axllent/mailpit/logger"
|
||||
@@ -19,7 +20,15 @@ func mailHandler(origin net.Addr, from string, to []string, data []byte) error {
|
||||
}
|
||||
|
||||
if _, err := storage.Store(storage.DefaultMailbox, data); err != nil {
|
||||
logger.Log().Errorf("error storing message: %s", err.Error())
|
||||
// Value with size 4800709 exceeded 1048576 limit
|
||||
re := regexp.MustCompile(`(Value with size \d+ exceeded \d+ limit)`)
|
||||
tooLarge := re.FindStringSubmatch(err.Error())
|
||||
if len(tooLarge) > 0 {
|
||||
logger.Log().Errorf("[db] error storing message: %s", tooLarge[0])
|
||||
} else {
|
||||
logger.Log().Errorf("[db] error storing message")
|
||||
logger.Log().Errorf(err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -209,6 +209,8 @@ func Store(mailbox string, b []byte) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
statsAddNewMessage(mailbox)
|
||||
|
||||
// save the raw email in a separate collection
|
||||
raw := clover.NewDocument()
|
||||
raw.Set("_id", id)
|
||||
@@ -218,12 +220,11 @@ func Store(mailbox string, b []byte) (string, error) {
|
||||
if err != nil {
|
||||
// delete the summary because the data insert failed
|
||||
logger.Log().Debugf("[db] error inserting raw message, rolling back")
|
||||
_ = DeleteOneMessage(mailbox, id)
|
||||
DeleteOneMessage(mailbox, id)
|
||||
|
||||
return "", err
|
||||
}
|
||||
|
||||
statsAddNewMessage(mailbox)
|
||||
|
||||
count++
|
||||
if count%100 == 0 {
|
||||
logger.Log().Infof("100 messages added in %s", time.Since(per100start))
|
||||
@@ -326,12 +327,12 @@ func Search(mailbox, search string, start, limit int) ([]data.Summary, error) {
|
||||
results := []data.Summary{}
|
||||
|
||||
for _, d := range q {
|
||||
cs := &CloverStore{}
|
||||
cs := &data.Summary{}
|
||||
if err := d.Unmarshal(cs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results = append(results, cs.Summary(d.ObjectId()))
|
||||
cs.ID = d.ObjectId()
|
||||
results = append(results, *cs)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
@@ -350,25 +351,6 @@ func CountUnread(mailbox string) (int, error) {
|
||||
)
|
||||
}
|
||||
|
||||
// Summary generated a message summary. ID must be supplied
|
||||
// as this is not stored within the CloverStore but rather the
|
||||
// *clover.Document
|
||||
func (c *CloverStore) Summary(id string) data.Summary {
|
||||
s := data.Summary{
|
||||
ID: id,
|
||||
From: c.From,
|
||||
To: c.To,
|
||||
Cc: c.Cc,
|
||||
Bcc: c.Bcc,
|
||||
Subject: c.Subject,
|
||||
Created: c.Created,
|
||||
Size: c.Size,
|
||||
Attachments: c.Attachments,
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
// GetMessage returns a data.Message generated from the {mailbox}_data collection.
|
||||
// ID must be supplied as this is not stored within the CloverStore but rather the
|
||||
// *clover.Document
|
||||
@@ -399,10 +381,7 @@ func GetMessage(mailbox, id string) (*data.Message, error) {
|
||||
from = &mail.Address{Name: env.GetHeader("From")}
|
||||
}
|
||||
|
||||
date, err := env.Date()
|
||||
if err != nil {
|
||||
// date =
|
||||
}
|
||||
date, _ := env.Date()
|
||||
|
||||
obj := data.Message{
|
||||
ID: q.ObjectId(),
|
||||
@@ -522,11 +501,18 @@ func UnreadMessage(mailbox, id string) error {
|
||||
|
||||
// DeleteOneMessage will delete a single message from a mailbox
|
||||
func DeleteOneMessage(mailbox, id string) error {
|
||||
q, err := db.FindById(mailbox, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
unreadStatus := !q.Get("Read").(bool)
|
||||
|
||||
if err := db.DeleteById(mailbox, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
statsDeleteOneMessage(mailbox)
|
||||
statsDeleteOneMessage(mailbox, unreadStatus)
|
||||
|
||||
return db.DeleteById(mailbox+"_data", id)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,8 +20,10 @@ var (
|
||||
)
|
||||
|
||||
func TestTextEmailInserts(t *testing.T) {
|
||||
setup()
|
||||
setup(false)
|
||||
t.Log("Testing memory storage")
|
||||
|
||||
RepeatTest:
|
||||
start := time.Now()
|
||||
for i := 0; i < 1000; i++ {
|
||||
if _, err := Store(DefaultMailbox, testTextEmail); err != nil {
|
||||
@@ -55,11 +59,20 @@ func TestTextEmailInserts(t *testing.T) {
|
||||
t.Logf("deleted 1,000 text emails in %s\n", time.Since(delStart))
|
||||
|
||||
db.Close()
|
||||
if config.DataDir == "" {
|
||||
setup(true)
|
||||
t.Logf("Testing physical storage to %s", config.DataDir)
|
||||
defer os.RemoveAll(config.DataDir)
|
||||
goto RepeatTest
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMimeEmailInserts(t *testing.T) {
|
||||
setup()
|
||||
setup(false)
|
||||
t.Log("Testing memory storage")
|
||||
|
||||
RepeatTest:
|
||||
start := time.Now()
|
||||
for i := 0; i < 1000; i++ {
|
||||
if _, err := Store(DefaultMailbox, testMimeEmail); err != nil {
|
||||
@@ -95,11 +108,19 @@ func TestMimeEmailInserts(t *testing.T) {
|
||||
t.Logf("deleted 1,000 mime emails in %s\n", time.Since(delStart))
|
||||
|
||||
db.Close()
|
||||
if config.DataDir == "" {
|
||||
setup(true)
|
||||
t.Logf("Testing physical storage to %s", config.DataDir)
|
||||
defer os.RemoveAll(config.DataDir)
|
||||
goto RepeatTest
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetrieveMimeEmail(t *testing.T) {
|
||||
setup()
|
||||
setup(false)
|
||||
t.Log("Testing memory storage")
|
||||
|
||||
RepeatTest:
|
||||
id, err := Store(DefaultMailbox, testMimeEmail)
|
||||
if err != nil {
|
||||
t.Log("error ", err)
|
||||
@@ -128,11 +149,20 @@ func TestRetrieveMimeEmail(t *testing.T) {
|
||||
assertEqual(t, len(inlineData.Content), msg.Inline[0].Size, "inline attachment size does not match")
|
||||
|
||||
db.Close()
|
||||
|
||||
if config.DataDir == "" {
|
||||
setup(true)
|
||||
t.Logf("Testing physical storage to %s", config.DataDir)
|
||||
defer os.RemoveAll(config.DataDir)
|
||||
goto RepeatTest
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
setup()
|
||||
setup(false)
|
||||
t.Log("Testing memory storage")
|
||||
|
||||
RepeatTest:
|
||||
for i := 0; i < 1000; i++ {
|
||||
msg := enmime.Builder().
|
||||
From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%d@example.com", i)).
|
||||
@@ -198,10 +228,17 @@ func TestSearch(t *testing.T) {
|
||||
assertEqual(t, len(summaries), 200, "200 search results expected")
|
||||
|
||||
db.Close()
|
||||
|
||||
if config.DataDir == "" {
|
||||
setup(true)
|
||||
t.Logf("Testing physical storage to %s", config.DataDir)
|
||||
defer os.RemoveAll(config.DataDir)
|
||||
goto RepeatTest
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkImportText(b *testing.B) {
|
||||
setup()
|
||||
setup(false)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, err := Store(DefaultMailbox, testTextEmail); err != nil {
|
||||
@@ -214,7 +251,7 @@ func BenchmarkImportText(b *testing.B) {
|
||||
}
|
||||
|
||||
func BenchmarkImportMime(b *testing.B) {
|
||||
setup()
|
||||
setup(false)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, err := Store(DefaultMailbox, testMimeEmail); err != nil {
|
||||
@@ -225,9 +262,16 @@ func BenchmarkImportMime(b *testing.B) {
|
||||
db.Close()
|
||||
}
|
||||
|
||||
func setup() {
|
||||
func setup(dataDir bool) {
|
||||
config.NoLogging = true
|
||||
config.MaxMessages = 0
|
||||
|
||||
if dataDir {
|
||||
config.DataDir = fmt.Sprintf("%s-%d", path.Join(os.TempDir(), "mailpit-tests"), time.Now().UnixNano())
|
||||
} else {
|
||||
config.DataDir = ""
|
||||
}
|
||||
|
||||
if err := InitDB(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -243,7 +287,6 @@ func setup() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
|
||||
|
||||
@@ -63,13 +63,23 @@ func statsAddNewMessage(mailbox string) {
|
||||
statsLock.Unlock()
|
||||
}
|
||||
|
||||
// Deleting one will always mean it was read
|
||||
func statsDeleteOneMessage(mailbox string) {
|
||||
// Delete one message from the totals. If the message was unread,
|
||||
// then it will also deduct one from the Unread status.
|
||||
func statsDeleteOneMessage(mailbox string, unread bool) {
|
||||
statsLock.Lock()
|
||||
s, ok := mailboxStats[mailbox]
|
||||
if ok {
|
||||
// deduct from the totals
|
||||
if s.Total > 0 {
|
||||
s.Total = s.Total - 1
|
||||
}
|
||||
// only deduct if the original was unread
|
||||
if unread && s.Unread > 0 {
|
||||
s.Unread = s.Unread - 1
|
||||
}
|
||||
|
||||
mailboxStats[mailbox] = data.MailboxStats{
|
||||
Total: s.Total - 1,
|
||||
Total: s.Total,
|
||||
Unread: s.Unread,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user