mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 09:37:02 +00:00
Reorganize packages pt 1
End goal: simplify build process
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package inbucket
|
||||
package log
|
||||
|
||||
import (
|
||||
"log"
|
||||
@@ -1,10 +1,12 @@
|
||||
package inbucket
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"io/ioutil"
|
||||
"net/mail"
|
||||
"os"
|
||||
@@ -40,13 +42,13 @@ type DataStore struct {
|
||||
// NewDataStore creates a new DataStore object. It uses the inbucket.Config object to
|
||||
// construct it's path.
|
||||
func NewDataStore() *DataStore {
|
||||
path, err := Config.String("datastore", "path")
|
||||
path, err := inbucket.Config.String("datastore", "path")
|
||||
if err != nil {
|
||||
Error("Error getting datastore path: %v", err)
|
||||
log.Error("Error getting datastore path: %v", err)
|
||||
return nil
|
||||
}
|
||||
if path == "" {
|
||||
Error("No value configured for datastore path")
|
||||
log.Error("No value configured for datastore path")
|
||||
return nil
|
||||
}
|
||||
mailPath := filepath.Join(path, "mail")
|
||||
@@ -60,7 +62,7 @@ func (ds *DataStore) MailboxFor(emailAddress string) (*Mailbox, error) {
|
||||
dir := HashMailboxName(name)
|
||||
path := filepath.Join(ds.mailPath, dir)
|
||||
if err := os.MkdirAll(path, 0770); err != nil {
|
||||
Error("Failed to create directory %v, %v", path, err)
|
||||
log.Error("Failed to create directory %v, %v", path, err)
|
||||
return nil, err
|
||||
}
|
||||
return &Mailbox{store: ds, name: name, dirName: dir, path: path}, nil
|
||||
@@ -86,7 +88,7 @@ func (mb *Mailbox) GetMessages() ([]*Message, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
Trace("Scanning %v files for %v", len(files), mb)
|
||||
log.Trace("Scanning %v files for %v", len(files), mb)
|
||||
|
||||
messages := make([]*Message, 0, len(files))
|
||||
for _, f := range files {
|
||||
@@ -103,7 +105,7 @@ func (mb *Mailbox) GetMessages() ([]*Message, error) {
|
||||
}
|
||||
file.Close()
|
||||
msg.mailbox = mb
|
||||
Trace("Found: %v", msg)
|
||||
log.Trace("Found: %v", msg)
|
||||
messages = append(messages, msg)
|
||||
}
|
||||
}
|
||||
@@ -124,7 +126,7 @@ func (mb *Mailbox) GetMessage(id string) (*Message, error) {
|
||||
}
|
||||
file.Close()
|
||||
msg.mailbox = mb
|
||||
Trace("Found: %v", msg)
|
||||
log.Trace("Found: %v", msg)
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
@@ -259,12 +261,12 @@ func (m *Message) Close() error {
|
||||
|
||||
// Delete this Message from disk by removing both the gob and raw files
|
||||
func (m *Message) Delete() error {
|
||||
Trace("Deleting %v", m.gobPath())
|
||||
log.Trace("Deleting %v", m.gobPath())
|
||||
err := os.Remove(m.gobPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Trace("Deleting %v", m.rawPath())
|
||||
log.Trace("Deleting %v", m.rawPath())
|
||||
return os.Remove(m.rawPath())
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"bytes"
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"net"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -87,7 +87,7 @@ func (ss *Session) String() string {
|
||||
* 5. Goto 2
|
||||
*/
|
||||
func (s *Server) startSession(id int, conn net.Conn) {
|
||||
inbucket.Info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
|
||||
log.Info("Connection from %v, starting session <%v>", conn.RemoteAddr(), id)
|
||||
defer conn.Close()
|
||||
|
||||
ss := NewSession(s, id, conn)
|
||||
@@ -279,8 +279,8 @@ func (ss *Session) dataHandler() {
|
||||
msgSize := 0
|
||||
|
||||
// Get a Mailbox and a new Message for each recipient
|
||||
mailboxes := make([]*inbucket.Mailbox, ss.recipients.Len())
|
||||
messages := make([]*inbucket.Message, ss.recipients.Len())
|
||||
mailboxes := make([]*Mailbox, ss.recipients.Len())
|
||||
messages := make([]*Message, ss.recipients.Len())
|
||||
i := 0
|
||||
for e := ss.recipients.Front(); e != nil; e = e.Next() {
|
||||
recip := e.Value.(string)
|
||||
@@ -476,17 +476,17 @@ func (ss *Session) ooSeq(cmd string) {
|
||||
|
||||
// Session specific logging methods
|
||||
func (ss *Session) trace(msg string, args ...interface{}) {
|
||||
inbucket.Trace("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
log.Trace("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
func (ss *Session) info(msg string, args ...interface{}) {
|
||||
inbucket.Info("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
log.Info("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
func (ss *Session) warn(msg string, args ...interface{}) {
|
||||
inbucket.Warn("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
log.Warn("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
func (ss *Session) error(msg string, args ...interface{}) {
|
||||
inbucket.Error("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
log.Error("%v<%v> %v", ss.remoteHost, ss.id, fmt.Sprintf(msg, args...))
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package smtpd
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"net"
|
||||
)
|
||||
|
||||
@@ -12,12 +13,12 @@ type Server struct {
|
||||
maxRecips int
|
||||
maxIdleSeconds int
|
||||
maxMessageBytes int
|
||||
dataStore *inbucket.DataStore
|
||||
dataStore *DataStore
|
||||
}
|
||||
|
||||
// Init a new Server object
|
||||
func New() *Server {
|
||||
ds := inbucket.NewDataStore()
|
||||
ds := NewDataStore()
|
||||
// TODO Make more of these configurable
|
||||
return &Server{domain: inbucket.GetSmtpConfig().Domain, maxRecips: 100, maxIdleSeconds: 300,
|
||||
dataStore: ds, maxMessageBytes: 2048000}
|
||||
@@ -29,15 +30,15 @@ func (s *Server) Start() {
|
||||
addr, err := net.ResolveTCPAddr("tcp4", fmt.Sprintf("%v:%v",
|
||||
cfg.Ip4address, cfg.Ip4port))
|
||||
if err != nil {
|
||||
inbucket.Error("Failed to build tcp4 address: %v", err)
|
||||
log.Error("Failed to build tcp4 address: %v", err)
|
||||
// TODO More graceful early-shutdown procedure
|
||||
panic(err)
|
||||
}
|
||||
|
||||
inbucket.Info("SMTP listening on TCP4 %v", addr)
|
||||
log.Info("SMTP listening on TCP4 %v", addr)
|
||||
ln, err := net.ListenTCP("tcp4", addr)
|
||||
if err != nil {
|
||||
inbucket.Error("Failed to start tcp4 listener: %v", err)
|
||||
log.Error("Failed to start tcp4 listener: %v", err)
|
||||
// TODO More graceful early-shutdown procedure
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package inbucket
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -1,4 +1,4 @@
|
||||
package inbucket
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -79,7 +79,7 @@ func TestParseInlineHtml(t *testing.T) {
|
||||
// readMessage is a test utility function to fetch a mail.Message object.
|
||||
func readMessage(filename string) *mail.Message {
|
||||
// Open test email for parsing
|
||||
raw, err := os.Open(filepath.Join("test-data", filename))
|
||||
raw, err := os.Open(filepath.Join("..", "test-data", filename))
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Failed to open test data: %v", err))
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package inbucket
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
@@ -1,4 +1,4 @@
|
||||
package inbucket
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"github.com/stretchrcom/testify/assert"
|
||||
@@ -3,14 +3,14 @@ package web
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/smtpd"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Vars map[string]string
|
||||
Session *sessions.Session
|
||||
DataStore *inbucket.DataStore
|
||||
DataStore *smtpd.DataStore
|
||||
}
|
||||
|
||||
func (c *Context) Close() {
|
||||
@@ -20,7 +20,7 @@ func (c *Context) Close() {
|
||||
func NewContext(req *http.Request) (*Context, error) {
|
||||
vars := mux.Vars(req)
|
||||
sess, err := sessionStore.Get(req, "inbucket")
|
||||
ds := inbucket.NewDataStore()
|
||||
ds := smtpd.NewDataStore()
|
||||
ctx := &Context{
|
||||
Vars: vars,
|
||||
Session: sess,
|
||||
|
||||
@@ -2,7 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"html/template"
|
||||
"time"
|
||||
)
|
||||
@@ -30,7 +30,7 @@ func reverse(name string, things ...interface{}) string {
|
||||
// Grab the route
|
||||
u, err := Router.Get(name).URL(strs...)
|
||||
if err != nil {
|
||||
inbucket.Error("Failed to reverse route: %v", err)
|
||||
log.Error("Failed to reverse route: %v", err)
|
||||
return "/ROUTE-ERROR"
|
||||
}
|
||||
return u.Path
|
||||
|
||||
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -37,7 +38,7 @@ func MailboxList(w http.ResponseWriter, req *http.Request, ctx *Context) (err er
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inbucket.Trace("Got %v messsages", len(messages))
|
||||
log.Trace("Got %v messsages", len(messages))
|
||||
|
||||
return RenderPartial("mailbox/_list.html", w, map[string]interface{}{
|
||||
"ctx": ctx,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"net/http"
|
||||
"thegoods.biz/httpbuf"
|
||||
"time"
|
||||
@@ -21,8 +22,8 @@ var sessionStore sessions.Store
|
||||
|
||||
func setupRoutes(cfg inbucket.WebConfig) {
|
||||
Router = mux.NewRouter()
|
||||
inbucket.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
inbucket.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
log.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
log.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
|
||||
r := Router
|
||||
// Static content
|
||||
@@ -47,7 +48,7 @@ func Start() {
|
||||
sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
|
||||
|
||||
addr := fmt.Sprintf("%v:%v", cfg.Ip4address, cfg.Ip4port)
|
||||
inbucket.Info("HTTP listening on TCP4 %v", addr)
|
||||
log.Info("HTTP listening on TCP4 %v", addr)
|
||||
s := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: Router,
|
||||
@@ -57,7 +58,7 @@ func Start() {
|
||||
|
||||
err := s.ListenAndServe()
|
||||
if err != nil {
|
||||
inbucket.Error("HTTP server failed: %v", err)
|
||||
log.Error("HTTP server failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Create the context
|
||||
ctx, err := NewContext(req)
|
||||
if err != nil {
|
||||
inbucket.Error("Failed to create context: %v", err)
|
||||
log.Error("Failed to create context: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -74,7 +75,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// Run the handler, grab the error, and report it
|
||||
buf := new(httpbuf.Buffer)
|
||||
inbucket.Trace("Web: %v %v %v %v", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
||||
log.Trace("Web: %v %v %v %v", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
||||
err = h(buf, req, ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -83,7 +84,7 @@ func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
// Save the session
|
||||
if err = ctx.Session.Save(req, buf); err != nil {
|
||||
inbucket.Error("Failed to save session: %v", err)
|
||||
log.Error("Failed to save session: %v", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path"
|
||||
@@ -19,7 +20,7 @@ var cachedPartials = map[string]*template.Template{}
|
||||
func RenderTemplate(name string, w http.ResponseWriter, data interface{}) error {
|
||||
t, err := ParseTemplate(name, false)
|
||||
if err != nil {
|
||||
inbucket.Error("Error in template '%v': %v", name, err)
|
||||
log.Error("Error in template '%v': %v", name, err)
|
||||
return err
|
||||
}
|
||||
w.Header().Set("Expires", "-1")
|
||||
@@ -31,7 +32,7 @@ func RenderTemplate(name string, w http.ResponseWriter, data interface{}) error
|
||||
func RenderPartial(name string, w http.ResponseWriter, data interface{}) error {
|
||||
t, err := ParseTemplate(name, true)
|
||||
if err != nil {
|
||||
inbucket.Error("Error in template '%v': %v", name, err)
|
||||
log.Error("Error in template '%v': %v", name, err)
|
||||
return err
|
||||
}
|
||||
w.Header().Set("Expires", "-1")
|
||||
@@ -51,7 +52,7 @@ func ParseTemplate(name string, partial bool) (*template.Template, error) {
|
||||
cfg := inbucket.GetWebConfig()
|
||||
tempPath := strings.Replace(name, "/", string(filepath.Separator), -1)
|
||||
tempFile := filepath.Join(cfg.TemplateDir, tempPath)
|
||||
inbucket.Trace("Parsing template %v", tempFile)
|
||||
log.Trace("Parsing template %v", tempFile)
|
||||
|
||||
var err error
|
||||
var t *template.Template
|
||||
@@ -71,10 +72,10 @@ func ParseTemplate(name string, partial bool) (*template.Template, error) {
|
||||
// Allows us to disable caching for theme development
|
||||
if cfg.TemplateCache {
|
||||
if partial {
|
||||
inbucket.Trace("Caching partial %v", name)
|
||||
log.Trace("Caching partial %v", name)
|
||||
cachedTemplates[name] = t
|
||||
} else {
|
||||
inbucket.Trace("Caching template %v", name)
|
||||
log.Trace("Caching template %v", name)
|
||||
cachedTemplates[name] = t
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user