1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 17:47:03 +00:00

Reorganize packages pt 1

End goal: simplify build process
This commit is contained in:
James Hillyerd
2012-10-22 15:20:33 -07:00
parent 21a9211c0f
commit 4e5c0ce4d8
13 changed files with 53 additions and 47 deletions

View File

@@ -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,

View File

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

View File

@@ -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,

View File

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

View File

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