mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
The main index template renders now!
This commit is contained in:
31
web/app.go
31
web/app.go
@@ -1,31 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket/app/smtpd"
|
||||
"github.com/robfig/revel"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
*rev.Controller
|
||||
}
|
||||
|
||||
func (c Application) Index() rev.Result {
|
||||
return c.Render()
|
||||
}
|
||||
|
||||
type SmtpdPlugin struct {
|
||||
rev.EmptyPlugin
|
||||
server *smtpd.Server
|
||||
}
|
||||
|
||||
func (p SmtpdPlugin) OnAppStart() {
|
||||
domain := rev.Config.StringDefault("smtpd.domain", "localhost")
|
||||
port := rev.Config.IntDefault("smtpd.port", 2500)
|
||||
rev.INFO.Printf("SMTP Daemon plugin init {domain: %v, port: %v}", domain, port)
|
||||
p.server = smtpd.New(domain, port)
|
||||
go p.server.Start()
|
||||
}
|
||||
|
||||
func init() {
|
||||
rev.RegisterPlugin(SmtpdPlugin{})
|
||||
}
|
||||
25
web/context.go
Normal file
25
web/context.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/gorilla/sessions"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
func (c *Context) Close() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
func NewContext(req *http.Request) (*Context, error) {
|
||||
sess, err := sessionStore.Get(req, "inbucket")
|
||||
ctx := &Context{
|
||||
Session: sess,
|
||||
}
|
||||
if err != nil {
|
||||
return ctx, err
|
||||
}
|
||||
return ctx, err
|
||||
}
|
||||
9
web/root_controller.go
Normal file
9
web/root_controller.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func RootIndex(w http.ResponseWriter, req *http.Request, ctx *Context) (err error) {
|
||||
return T("root-index.html").Execute(w, nil)
|
||||
}
|
||||
@@ -6,11 +6,15 @@ package web
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"net/http"
|
||||
"thegoods.biz/httpbuf"
|
||||
"time"
|
||||
)
|
||||
|
||||
type handler func(http.ResponseWriter, *http.Request, *Context) error
|
||||
|
||||
/*
|
||||
type WebServer struct {
|
||||
thing string
|
||||
@@ -24,24 +28,31 @@ func NewWebServer() *Server {
|
||||
|
||||
var Router *mux.Router
|
||||
|
||||
var sessionStore sessions.Store
|
||||
|
||||
func setupRoutes(cfg inbucket.WebConfig) {
|
||||
r := mux.NewRouter()
|
||||
Router = r
|
||||
inbucket.Info("Theme templates mapped to '%v'", cfg.TemplatesDir)
|
||||
inbucket.Info("Theme templates mapped to '%v'", cfg.TemplateDir)
|
||||
inbucket.Info("Theme static content mapped to '%v'", cfg.PublicDir)
|
||||
|
||||
// Static content
|
||||
r.PathPrefix("/public/").Handler(http.StripPrefix("/public/",
|
||||
http.FileServer(http.Dir(cfg.PublicDir))))
|
||||
|
||||
// Root
|
||||
r.Path("/").Handler(handler(RootIndex))
|
||||
}
|
||||
|
||||
// Start() the web server
|
||||
func Start() {
|
||||
cfg := inbucket.GetWebConfig()
|
||||
setupRoutes(cfg)
|
||||
|
||||
sessionStore = sessions.NewCookieStore([]byte("something-very-secret"))
|
||||
|
||||
addr := fmt.Sprintf("%v:%v", cfg.Ip4address, cfg.Ip4port)
|
||||
inbucket.Info("HTTP listening on TCP4 %v", addr)
|
||||
|
||||
s := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: Router,
|
||||
@@ -54,3 +65,31 @@ func Start() {
|
||||
inbucket.Error("HTTP server failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ServeHTTP builds the context and passes onto the real handler
|
||||
func (h handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Create the context
|
||||
ctx, err := NewContext(req)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer ctx.Close()
|
||||
|
||||
// Run the handler, grab the error, and report it
|
||||
buf := new(httpbuf.Buffer)
|
||||
err = h(buf, req, ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Save the session
|
||||
if err = ctx.Session.Save(req, buf); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Apply the buffered response to the writer
|
||||
buf.Apply(w)
|
||||
}
|
||||
|
||||
33
web/template.go
Normal file
33
web/template.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"html/template"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var cachedTemplates = map[string]*template.Template{}
|
||||
var cachedMutex sync.Mutex
|
||||
|
||||
func T(name string) *template.Template {
|
||||
cachedMutex.Lock()
|
||||
defer cachedMutex.Unlock()
|
||||
|
||||
if t, ok := cachedTemplates[name]; ok {
|
||||
return t
|
||||
}
|
||||
|
||||
templateDir := inbucket.GetWebConfig().TemplateDir
|
||||
templateFile := filepath.Join(templateDir, name)
|
||||
inbucket.Trace("Parsing template %v", templateFile)
|
||||
|
||||
t := template.New("_base.html").Funcs(TemplateFuncs)
|
||||
t = template.Must(t.ParseFiles(
|
||||
filepath.Join(templateDir, "_base.html"),
|
||||
templateFile,
|
||||
))
|
||||
cachedTemplates[name] = t
|
||||
|
||||
return t
|
||||
}
|
||||
Reference in New Issue
Block a user