1
0
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:
James Hillyerd
2012-10-21 09:57:42 -07:00
parent 442e8fbe14
commit 9c94bb2ab1
11 changed files with 141 additions and 67 deletions

View File

@@ -32,7 +32,7 @@ ip4.port=9000
theme=integral
# Path to the selected themes template files
templates.dir=%(install.dir)s/themes/%(theme)s/templates
template.dir=%(install.dir)s/themes/%(theme)s/templates
# Path to the selected themes public (static) files
public.dir=%(install.dir)s/themes/%(theme)s/public

View File

@@ -19,7 +19,7 @@ type SmtpConfig struct {
type WebConfig struct {
Ip4address net.IP
Ip4port int
TemplatesDir string
TemplateDir string
PublicDir string
}
@@ -68,7 +68,7 @@ func LoadConfig(filename string) error {
requireOption(messages, "smtp", "domain")
requireOption(messages, "web", "ip4.address")
requireOption(messages, "web", "ip4.port")
requireOption(messages, "web", "templates.dir")
requireOption(messages, "web", "template.dir")
requireOption(messages, "web", "public.dir")
requireOption(messages, "datastore", "path")
if messages.Len() > 0 {
@@ -151,12 +151,12 @@ func parseWebConfig() error {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
option = "[web]templates.dir"
str, err = Config.String("web", "templates.dir")
option = "[web]template.dir"
str, err = Config.String("web", "template.dir")
if err != nil {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
webConfig.TemplatesDir = str
webConfig.TemplateDir = str
option = "[web]public.dir"
str, err = Config.String("web", "public.dir")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

View File

@@ -4,19 +4,21 @@ Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License
-->
{{define "col1menu"}}{{/* Used inside #content div */}}
<div id="colOne">
<div id="logo">
<h1><a href="#">inbucket</a></h1>
<h2>email testing service</h2>
</div>
</div>
{{end}}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{.title}}</title>
<link href="/public/stylesheets/main.css" rel="stylesheet" type="text/css" />
<title>{{template "title" .}}</title>
<link href="/public/main.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" type="image/png" href="/public/images/favicon.png">
<script src="/public/js/jquery-1.5.2.min.js" type="text/javascript" charset="utf-8"></script>
{{range .moreStyles}}
<link rel="stylesheet" type="text/css" href="/public/{{.}}">
{{end}}
{{range .moreScripts}}
<script src="/public/{{.}}" type="text/javascript" charset="utf-8"></script>
{{end}}
<script src="/public/jquery-1.5.2.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="header">
@@ -32,3 +34,12 @@ Released for free under a Creative Commons Attribution 2.5 License
</form>
</div>
<div id="content">
{{template "content" .}}
</div>
<div id="footer">
<p>Inbucket is an open source project hosted at
<a href="http://github.com/jhillyerd/inbucket/">github</a>.
Design by <a href="http://www.freecsstemplates.org/">FCT</a>.</p>
</div>
</body>
</html>

View File

@@ -1,8 +0,0 @@
</div>
<div id="footer">
<p>Inbucket is an open source project hosted at
<a href="http://github.com/jhillyerd/inbucket/">github</a>.
Design by <a href="http://www.freecsstemplates.org/">FCT</a>.</p>
</div>
</body>
</html>

View File

@@ -1,15 +1,11 @@
{{set "title" "Inbucket" .}}
{{template "header.html" .}}
{{template "menu.html" .}}
{{define "title"}}Inbucket{{end}}
{{define "content"}}
{{template "col1menu" .}}
<div id="colTwo">
{{template "errors.html" .}}
<p>Inbucket is an email testing service; it will accept email for any email
address and make it available to view without a password.</p>
<p>To view email for a particular address, enter the username portion
of the address into the box on the upper right and click <em>go</em>.</p>
</div>
{{template "footer.html" .}}
{{end}}

View File

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

View File

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