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

Web server now starts

Can serve static content, no dynamic stuff yet
This commit is contained in:
James Hillyerd
2012-10-20 23:03:59 -07:00
parent 81fea97a90
commit 442e8fbe14
8 changed files with 150 additions and 20 deletions

View File

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

View File

@@ -16,15 +16,29 @@ type SmtpConfig struct {
Domain string
}
type WebConfig struct {
Ip4address net.IP
Ip4port int
TemplatesDir string
PublicDir string
}
var smtpConfig *SmtpConfig
var webConfig *WebConfig
var Config *config.Config
// GetSmtpConfig returns a copy of the SmtpConfig
// GetSmtpConfig returns a copy of the SmtpConfig object
func GetSmtpConfig() SmtpConfig {
return *smtpConfig
}
// GetWebConfig returns a copy of the WebConfig object
func GetWebConfig() WebConfig {
return *webConfig
}
// LoadConfig loads the specified configuration file into inbucket.Config
// and performs validations on it.
func LoadConfig(filename string) error {
@@ -66,6 +80,11 @@ func LoadConfig(filename string) error {
}
err = parseSmtpConfig()
if err != nil {
return nil
}
err = parseWebConfig()
return err
}
@@ -106,6 +125,49 @@ func parseSmtpConfig() error {
return nil
}
// parseWebConfig trying to catch config errors early
func parseWebConfig() error {
webConfig = new(WebConfig)
// Parse IP4 address only, error on IP6.
option := "[web]ip4.address"
str, err := Config.String("web", "ip4.address")
if err != nil {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
addr := net.ParseIP(str)
if addr == nil {
return fmt.Errorf("Failed to parse %v '%v'", option, str)
}
addr = addr.To4()
if addr == nil {
return fmt.Errorf("Failed to parse %v '%v' not IPv4!", option, str)
}
webConfig.Ip4address = addr
option = "[web]ip4.port"
webConfig.Ip4port, err = Config.Int("web", "ip4.port")
if err != nil {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
option = "[web]templates.dir"
str, err = Config.String("web", "templates.dir")
if err != nil {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
webConfig.TemplatesDir = str
option = "[web]public.dir"
str, err = Config.String("web", "public.dir")
if err != nil {
return fmt.Errorf("Failed to parse %v: %v", option, err)
}
webConfig.PublicDir = str
return nil
}
// requireSection checks that a [section] is defined in the configuration file,
// appending a message if not.
func requireSection(messages *list.List, section string) {

View File

@@ -8,7 +8,7 @@ import (
"fmt"
"github.com/jhillyerd/inbucket"
"github.com/jhillyerd/inbucket/smtpd"
"log"
"github.com/jhillyerd/inbucket/web"
"os"
)
@@ -32,15 +32,11 @@ func main() {
os.Exit(1)
}
log.Println("Logger test")
inbucket.Trace("trace test")
inbucket.Info("info test")
inbucket.Warn("warn test")
inbucket.Error("error test")
// Startup SMTP server
server := smtpd.New()
server.Start()
go server.Start()
web.Start()
}
func init() {

View File

@@ -34,7 +34,7 @@ func (s *Server) Start() {
panic(err)
}
inbucket.Info("Listening on TCP4 %v", addr)
inbucket.Info("SMTP listening on TCP4 %v", addr)
ln, err := net.ListenTCP("tcp4", addr)
if err != nil {
inbucket.Error("Failed to start tcp4 listener: %v", err)

View File

@@ -1,4 +1,4 @@
package controllers
package web
import (
"github.com/jhillyerd/inbucket/app/smtpd"

View File

@@ -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"))
}
},
}

View File

@@ -1,4 +1,4 @@
package controllers
package web
import (
"github.com/jhillyerd/inbucket/app/inbucket"

56
web/server.go Normal file
View 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)
}
}