mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 18:17:03 +00:00
The main index template renders now!
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user