Compare commits
5 commits
177556399d
...
a8705c5df3
Author | SHA1 | Date | |
---|---|---|---|
Lilian Jónsdóttir | a8705c5df3 | ||
Lilian Jónsdóttir | f82809bfb1 | ||
Lilian Jónsdóttir | 1275ae6b7d | ||
Lilian Jónsdóttir | 95fac5c405 | ||
Lilian Jónsdóttir | d771ddb3d3 |
|
@ -29,6 +29,9 @@ func main() {
|
|||
}
|
||||
|
||||
// and finally, start the server
|
||||
app.Logger.Printf("Listening on port %d", app.ListenPort)
|
||||
srv.ListenAndServe()
|
||||
app.Logger.Printf("Starting HTTP Server on port %d", app.ListenPort)
|
||||
err := srv.ListenAndServe()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Failed to start HTTP Server!", "err", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ var Middleware []func(next http.Handler) http.Handler = []func(next http.Handler
|
|||
middleware.RealIP,
|
||||
// plus custom request logger
|
||||
func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
app.Logger.Info("REQUEST", "url", r.URL, "ip", r.RemoteAddr, "useragent", r.UserAgent())
|
||||
next.ServeHTTP(w, r)
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
app.Logger.Info("REQUEST", "url", request.URL, "ip", request.RemoteAddr, "useragent", request.UserAgent())
|
||||
next.ServeHTTP(writer, request)
|
||||
})
|
||||
},
|
||||
middleware.Recoverer,
|
||||
|
|
|
@ -29,8 +29,9 @@ func routes(app *config.AppConfig) http.Handler {
|
|||
mux.Get(handler.Handles, handler.Handler)
|
||||
}
|
||||
|
||||
// Setup home handler
|
||||
// Setup extra handlers
|
||||
mux.Get("/", handlers.HomeHandler)
|
||||
mux.Get("/robots.txt", handlers.RobotHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import (
|
|||
// Handler holds data required for handlers.
|
||||
type Handler struct {
|
||||
Handles string
|
||||
Handler func(w http.ResponseWriter, r *http.Request)
|
||||
Handler func(writer http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
var app *config.AppConfig
|
||||
|
@ -27,14 +27,14 @@ var Handlers = []Handler{
|
|||
Handles: "/about",
|
||||
Handler: makeBasicHandler("about"),
|
||||
},
|
||||
{
|
||||
Handles: "/projects",
|
||||
Handler: makeLinksHandler("projects"),
|
||||
},
|
||||
{
|
||||
Handles: "/apps",
|
||||
Handler: makeLinksHandler("apps"),
|
||||
},
|
||||
{
|
||||
Handles: "/projects",
|
||||
Handler: makeLinksHandler("projects"),
|
||||
},
|
||||
}
|
||||
|
||||
// Initialise the handlers package.
|
||||
|
@ -43,7 +43,7 @@ func Initialise(a *config.AppConfig) {
|
|||
}
|
||||
|
||||
// HomeHandler handles /, generating data from Handlers
|
||||
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func HomeHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
page := "home.page.tmpl"
|
||||
d := models.TemplateData{}
|
||||
|
||||
|
@ -72,20 +72,33 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
|||
d.LinkMap = make(map[string][]models.Link)
|
||||
d.LinkMap["Pages"] = pages
|
||||
app.Logger.Debug("handling home with some data", "data", &d)
|
||||
render.RenderTemplateWithData(w, "home.page.tmpl", &d)
|
||||
render.RenderTemplateWithData(writer, "home.page.tmpl", &d)
|
||||
}
|
||||
|
||||
// RobotHandler creates a handler for robots.txt out of existing Handlers
|
||||
func RobotHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
robots := fmt.Sprintf("User-agent: %s\nAllow: /\n", request.UserAgent())
|
||||
|
||||
for _, handler := range Handlers {
|
||||
robots += fmt.Sprintf("Allow: %s\n", handler.Handles)
|
||||
}
|
||||
|
||||
robots += "Disallow: /*\n"
|
||||
|
||||
fmt.Fprint(writer, robots)
|
||||
}
|
||||
|
||||
// makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl
|
||||
func makeBasicHandler(name string) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
func makeBasicHandler(name string) func(writer http.ResponseWriter, request *http.Request) {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
pageName := name + ".page.tmpl"
|
||||
render.RenderTemplate(w, pageName)
|
||||
render.RenderTemplate(writer, pageName)
|
||||
}
|
||||
}
|
||||
|
||||
// makeLinksHandler returns a handler for links.tmpl with template data from `name`
|
||||
func makeLinksHandler(name string) func(w http.ResponseWriter, r *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
func makeLinksHandler(name string) func(writer http.ResponseWriter, request *http.Request) {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
page := "links.tmpl"
|
||||
template, err := render.GetTemplateFromCache(page)
|
||||
if err != nil {
|
||||
|
@ -100,6 +113,6 @@ func makeLinksHandler(name string) func(w http.ResponseWriter, r *http.Request)
|
|||
}
|
||||
|
||||
app.Logger.Debug("handling a links page", "data", &data)
|
||||
render.RenderTemplateWithData(w, page, &data)
|
||||
render.RenderTemplateWithData(writer, page, &data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,15 +26,96 @@ func Initialise(a *config.AppConfig) {
|
|||
app = a
|
||||
if app.UseCache {
|
||||
var err error
|
||||
app.TemplateCache, err = GenerateNewTemplateCache()
|
||||
app.TemplateCache, err = generateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateNewTemplateCache generates a new template cache.
|
||||
func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
||||
// RenderTemplate renders requested template (t), pulling from cache.
|
||||
func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||
// TODO: implement this better
|
||||
if !app.UseCache {
|
||||
regenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err)
|
||||
}
|
||||
|
||||
data, err := GetOrGenerateTemplateData(filename)
|
||||
if err != nil {
|
||||
app.Logger.Error(err)
|
||||
}
|
||||
|
||||
app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data)
|
||||
err = template.Execute(data, w)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Failed to execute template %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||
if !app.UseCache {
|
||||
regenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err)
|
||||
}
|
||||
|
||||
app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data)
|
||||
err = template.Execute(data, w)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Failed to execute template %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemplateFromCache gets templates from cache
|
||||
func GetTemplateFromCache(filename string) (*models.TemplateCacheItem, error) {
|
||||
if template, ok := app.TemplateCache.Cache[filename]; ok {
|
||||
return &template, nil
|
||||
} else {
|
||||
return &models.TemplateCacheItem{}, errors.New("Couldn't load template from cache")
|
||||
}
|
||||
}
|
||||
|
||||
// GetOrGenerateTemplateData gets template data from file, or generate simple
|
||||
func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) {
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
return &models.TemplateData{}, err
|
||||
}
|
||||
|
||||
data, err := td.LoadTemplateData(filename)
|
||||
if err == nil {
|
||||
app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", &data)
|
||||
if _, ok := data.StringMap["GeneratedAt"]; !ok {
|
||||
data.StringMap["GeneratedAt"] = template.GeneratedAt.Format(time.UnixDate)
|
||||
}
|
||||
} else {
|
||||
app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err)
|
||||
data = td.MakeBasicTemplateData(template.GeneratedAt)
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
||||
// regenerateTemplateCache regenerates the template cache
|
||||
func regenerateTemplateCache() {
|
||||
c, err := generateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
app.TemplateCache = c
|
||||
|
||||
}
|
||||
|
||||
// generateNewTemplateCache generates a new template cache.
|
||||
func generateNewTemplateCache() (models.TemplateCache, error) {
|
||||
// start with an empty map
|
||||
cache := models.TemplateCache{}
|
||||
cache.Cache = map[string]models.TemplateCacheItem{}
|
||||
|
@ -80,83 +161,3 @@ func GenerateNewTemplateCache() (models.TemplateCache, error) {
|
|||
// All was good, so return the cache, and no error
|
||||
return cache, nil
|
||||
}
|
||||
|
||||
// RenderTemplate renders requested template (t), pulling from cache.
|
||||
func RenderTemplate(w http.ResponseWriter, filename string) {
|
||||
// TODO: implement this better
|
||||
if !app.UseCache {
|
||||
RegenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err)
|
||||
}
|
||||
|
||||
data, err := GetOrGenerateTemplateData(filename)
|
||||
if err != nil {
|
||||
app.Logger.Error(err)
|
||||
}
|
||||
|
||||
app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data)
|
||||
err = template.Execute(data, w)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Failed to execute template %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
func RenderTemplateWithData(w http.ResponseWriter, filename string, data *models.TemplateData) {
|
||||
if !app.UseCache {
|
||||
RegenerateTemplateCache()
|
||||
}
|
||||
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Tried loading %s from the cache, but %s!", filename, err)
|
||||
}
|
||||
|
||||
app.Logger.Debug(fmt.Sprintf("Executing template %s", filename), "data", &data)
|
||||
err = template.Execute(data, w)
|
||||
if err != nil {
|
||||
app.Logger.Fatalf("Failed to execute template %s: %s", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
func RegenerateTemplateCache() {
|
||||
c, err := GenerateNewTemplateCache()
|
||||
if err != nil {
|
||||
app.Logger.Fatal("Error generating template cache, bailing out!")
|
||||
}
|
||||
app.TemplateCache = c
|
||||
|
||||
}
|
||||
|
||||
// GetTemplateFromCache gets templates from cache
|
||||
func GetTemplateFromCache(filename string) (*models.TemplateCacheItem, error) {
|
||||
if template, ok := app.TemplateCache.Cache[filename]; ok {
|
||||
return &template, nil
|
||||
} else {
|
||||
return &models.TemplateCacheItem{}, errors.New("Couldn't load template from cache")
|
||||
}
|
||||
}
|
||||
|
||||
// GetOrGenerateTemplateData gets template data from file, or generate simple
|
||||
func GetOrGenerateTemplateData(filename string) (*models.TemplateData, error) {
|
||||
template, err := GetTemplateFromCache(filename)
|
||||
if err != nil {
|
||||
return &models.TemplateData{}, err
|
||||
}
|
||||
|
||||
data, err := td.LoadTemplateData(filename)
|
||||
if err == nil {
|
||||
app.Logger.Debug(fmt.Sprintf("Loaded data for template %s.", filename), "data", &data)
|
||||
if _, ok := data.StringMap["GeneratedAt"]; !ok {
|
||||
data.StringMap["GeneratedAt"] = template.GeneratedAt.Format(time.UnixDate)
|
||||
}
|
||||
} else {
|
||||
app.Logger.Info(fmt.Sprintf("Loading template data for %s failed, using default template data.", filename), "err", err)
|
||||
data = td.MakeBasicTemplateData(template.GeneratedAt)
|
||||
}
|
||||
|
||||
return &data, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue