Skip to content

Commit

Permalink
Improve separation between templates and static content
Browse files Browse the repository at this point in the history
- Move static content into "static" dir instead of "www".
- Stop using `router.NotFound` in favor of `router.ServeFiles` with a
  named prefix. This constrains the access to assets that start with
  "/static/", hence accomplishing one of the goals.

Relates to #2
  • Loading branch information
jmcfarlane committed Feb 29, 2016
1 parent 920d7b7 commit 8f29ea8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -15,7 +15,7 @@ Here's what I was trying to accomplish:
1. Use some templates as partials (`header.hmtl`, `footer.html`).
1. Serve static content in a manner similar to
[http.FileServer](https://golang.org/pkg/net/http/#example_FileServer).
1. ~~Exclude templates from the static files being served.~~
1. Exclude templates from the static files being served.
1. Support custom template functions.
1. Compile everything into a single static binary (including templates
and static files).
Expand All @@ -36,7 +36,7 @@ cd $GOPATH/src/github.com/jmcfarlane/golang-templates-example
go generate
go build && ./golang-templates-example
curl http://localhost:8080/hello/jack
xdg-open http://localhost:8080/golang.png
xdg-open http://localhost:8080/static/golang.png
```

**NOTE:**
Expand Down
6 changes: 3 additions & 3 deletions main.go
@@ -1,6 +1,6 @@
package main

//go:generate go-bindata-assetfs www/... templates/...
//go:generate go-bindata-assetfs static/... templates/...

import (
"html/template"
Expand Down Expand Up @@ -52,8 +52,8 @@ func main() {
// Example route that takes one rest style option
router.GET("/hello/:name", hello)

// Serve static assets via the "www" directory
router.NotFound = http.FileServer(assetFS())
// Serve static assets via the "static" directory
router.ServeFiles("/static/*filepath", assetFS())

// Serve this program forever
log.Fatal(http.ListenAndServe(":8080", router))
Expand Down
File renamed without changes

0 comments on commit 8f29ea8

Please sign in to comment.