Browse Source

go.rice now serves static and template files

pull/12/head
andreimarcu 9 years ago
parent
commit
56e305bfcc
  1. 16
      display.go
  2. 10
      pages.go
  3. 26
      server.go
  4. 67
      templates.go

16
display.go

@ -11,12 +11,6 @@ import (
"github.com/zenazn/goji/web"
)
var imageTpl = pongo2.Must(pongo2.FromCache("templates/display/image.html"))
var audioTpl = pongo2.Must(pongo2.FromCache("templates/display/audio.html"))
var videoTpl = pongo2.Must(pongo2.FromCache("templates/display/video.html"))
var fileTpl = pongo2.Must(pongo2.FromCache("templates/display/file.html"))
var pdfTpl = pongo2.Must(pongo2.FromCache("templates/display/pdf.html"))
func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
fileName := c.URLParams["name"]
filePath := path.Join(Config.filesDir, fileName)
@ -42,15 +36,15 @@ func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
var tpl *pongo2.Template
if strings.HasPrefix(mimetype, "image/") {
tpl = imageTpl
tpl = Templates["display/image.html"]
} else if strings.HasPrefix(mimetype, "video/") {
tpl = videoTpl
tpl = Templates["display/video.html"]
} else if strings.HasPrefix(mimetype, "audio/") {
tpl = audioTpl
tpl = Templates["display/audio.html"]
} else if mimetype == "application/pdf" {
tpl = pdfTpl
tpl = Templates["display/pdf.html"]
} else {
tpl = fileTpl
tpl = Templates["display/file.html"]
}
err = tpl.ExecuteWriter(pongo2.Context{

10
pages.go

@ -7,12 +7,8 @@ import (
"github.com/zenazn/goji/web"
)
var indexTpl = pongo2.Must(pongo2.FromCache("templates/index.html"))
var notFoundTpl = pongo2.Must(pongo2.FromCache("templates/404.html"))
var oopsTpl = pongo2.Must(pongo2.FromCache("templates/oops.html"))
func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := indexTpl.ExecuteWriter(pongo2.Context{}, w)
err := Templates["index.html"].ExecuteWriter(pongo2.Context{}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
@ -20,14 +16,14 @@ func indexHandler(c web.C, w http.ResponseWriter, r *http.Request) {
func notFoundHandler(c web.C, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
err := notFoundTpl.ExecuteWriter(pongo2.Context{}, w)
err := Templates["404.html"].ExecuteWriter(pongo2.Context{}, w)
if err != nil {
oopsHandler(c, w, r)
}
}
func oopsHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err := oopsTpl.ExecuteWriter(pongo2.Context{}, w)
err := Templates["oops.html"].ExecuteWriter(pongo2.Context{}, w)
if err != nil {
oopsHandler(c, w, r)
}

26
server.go

@ -9,6 +9,7 @@ import (
"os"
"regexp"
"github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web/middleware"
@ -23,15 +24,16 @@ var Config struct {
siteURL string
}
var Templates = make(map[string]*pongo2.Template)
var TemplateSet *pongo2.TemplateSet
func setup() {
if Config.noLogs {
goji.Abandon(middleware.Logger)
}
// make directories if needed
var err error
err = os.MkdirAll(Config.filesDir, 0755)
err := os.MkdirAll(Config.filesDir, 0755)
if err != nil {
fmt.Println("Error: could not create files directory")
os.Exit(1)
@ -48,8 +50,19 @@ func setup() {
Config.siteURL = Config.siteURL + "/"
}
// Template Globals
pongo2.DefaultSet.Globals["sitename"] = Config.siteName
// Template setup
p2l, err := NewPongo2Loader("templates")
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
}
TemplateSet := pongo2.NewSet("templates", p2l)
TemplateSet.Globals["sitename"] = Config.siteName
err = populateTemplatesMap(TemplateSet, Templates)
if err != nil {
fmt.Println("Error: could not load templates")
os.Exit(1)
}
// Routing setup
nameRe := regexp.MustCompile(`^/(?P<name>[a-z0-9-\.]+)$`)
@ -62,8 +75,9 @@ func setup() {
goji.Put("/upload", uploadPutHandler)
goji.Put("/upload/:name", uploadPutHandler)
staticBox := rice.MustFindBox("static")
goji.Get("/static/*", http.StripPrefix("/static/",
http.FileServer(http.Dir("static/"))))
http.FileServer(staticBox.HTTPBox())))
goji.Get(nameRe, fileDisplayHandler)
goji.Get(selifRe, fileServeHandler)
goji.NotFound(notFoundHandler)

67
templates.go

@ -0,0 +1,67 @@
package main
import (
"bytes"
"io"
"path"
"path/filepath"
"github.com/GeertJohan/go.rice"
"github.com/flosch/pongo2"
)
type Pongo2Loader struct {
box *rice.Box
}
func NewPongo2Loader(boxName string) (*Pongo2Loader, error) {
fs := &Pongo2Loader{}
p2l, err := rice.FindBox(boxName)
if err != nil {
return nil, err
}
fs.box = p2l
return fs, nil
}
func (fs *Pongo2Loader) Get(path string) (io.Reader, error) {
myBytes, err := fs.box.Bytes(path)
if err != nil {
return nil, err
}
return bytes.NewReader(myBytes), nil
}
func (fs *Pongo2Loader) Abs(base, name string) string {
me := path.Join(filepath.Dir(base), name)
return me
}
func populateTemplatesMap(tSet *pongo2.TemplateSet, tMap map[string]*pongo2.Template) error {
templates := [...]string{
"index.html",
"404.html",
"oops.html",
"display/audio.html",
"display/image.html",
"display/video.html",
"display/pdf.html",
"display/file.html",
}
for _, tName := range templates {
tpl, err := tSet.FromFile(tName)
if err != nil {
return err
}
tMap[tName] = tpl
}
return nil
}
Loading…
Cancel
Save