mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-23 20:47:03 +00:00
Web server now starts
Can serve static content, no dynamic stuff yet
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package controllers
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket/app/smtpd"
|
||||
|
||||
@@ -1,19 +1,35 @@
|
||||
package controllers
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/robfig/revel"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"html/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rev.TRACE.Println("Registering helpers")
|
||||
rev.Funcs["friendlyTime"] = func(t time.Time) template.HTML {
|
||||
var TemplateFuncs = template.FuncMap{
|
||||
// Reversable routing function
|
||||
"reverse": func(name string, things ...interface{}) string {
|
||||
// Convert the things to strings
|
||||
strs := make([]string, len(things))
|
||||
for i, th := range things {
|
||||
strs[i] = fmt.Sprint(th)
|
||||
}
|
||||
// Grab the route
|
||||
u, err := Router.Get(name).URL(strs...)
|
||||
if err != nil {
|
||||
inbucket.Error("Failed to reverse route: %v", err)
|
||||
return "/ROUTE-ERROR"
|
||||
}
|
||||
return u.Path
|
||||
},
|
||||
// Friendly date & time rendering
|
||||
"friendlyTime": func(t time.Time) template.HTML {
|
||||
ty, tm, td := t.Date()
|
||||
ny, nm, nd := time.Now().Date()
|
||||
if (ty == ny) && (tm == nm) && (td == nd) {
|
||||
return template.HTML(t.Format("03:04:05 PM"))
|
||||
}
|
||||
return template.HTML(t.Format("Mon Jan 2, 2006"))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package controllers
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/jhillyerd/inbucket/app/inbucket"
|
||||
|
||||
56
web/server.go
Normal file
56
web/server.go
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
The web package contains all the code to provide Inbucket's web GUI
|
||||
*/
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jhillyerd/inbucket"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
/*
|
||||
type WebServer struct {
|
||||
thing string
|
||||
}
|
||||
|
||||
// NewServer() returns a new web.Server instance
|
||||
func NewWebServer() *Server {
|
||||
return &WebServer{}
|
||||
}
|
||||
*/
|
||||
|
||||
var Router *mux.Router
|
||||
|
||||
func setupRoutes(cfg inbucket.WebConfig) {
|
||||
r := mux.NewRouter()
|
||||
Router = r
|
||||
inbucket.Info("Theme templates mapped to '%v'", cfg.TemplatesDir)
|
||||
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))))
|
||||
}
|
||||
|
||||
// Start() the web server
|
||||
func Start() {
|
||||
cfg := inbucket.GetWebConfig()
|
||||
setupRoutes(cfg)
|
||||
addr := fmt.Sprintf("%v:%v", cfg.Ip4address, cfg.Ip4port)
|
||||
inbucket.Info("HTTP listening on TCP4 %v", addr)
|
||||
|
||||
s := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: Router,
|
||||
ReadTimeout: 60 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
err := s.ListenAndServe()
|
||||
if err != nil {
|
||||
inbucket.Error("HTTP server failed: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user