Serve public assets

This commit is contained in:
Matej Kramny 2016-01-06 12:20:07 +01:00
parent 90f9295505
commit 219fd5b58a
2 changed files with 45 additions and 8 deletions

View File

@ -26,6 +26,8 @@ if [ "$1" == "ci_test" ]; then
EOF
fi
echo "Adding bindata"
go-bindata $BINDATA_ARGS config.json database/sql_migrations/ $(find ./public -type d -print)
echo "Building into build/"

View File

@ -1,8 +1,9 @@
package routes
import (
"github.com/castawaylabs/semaphore"
"github.com/castawaylabs/semaphore/util"
"github.com/gin-gonic/gin"
"strings"
)
// Declare all routes
@ -11,8 +12,7 @@ func Route(r *gin.Engine) {
c.String(200, "PONG")
})
// serve public/ folder
r.Group("/public", servePublic, serve404)
r.NoRoute(servePublic)
// set up the namespace
api := r.Group("/api")
@ -28,10 +28,45 @@ func Route(r *gin.Engine) {
}
func servePublic(c *gin.Context) {
// util.asset url
// util.Asset()
path := c.Request.URL.Path
if !strings.HasPrefix(path, "/public") {
c.Next()
return
}
func serve404(c *gin.Context) {
c.AbortWithStatus(404)
path = strings.Replace(path, "/", "", 1)
split := strings.Split(path, ".")
suffix := split[len(split)-1]
res, err := util.Asset(path)
if err != nil {
c.Next()
return
}
contentType := "text/plain"
switch suffix {
case "png":
contentType = "image/png"
case "jpg", "jpeg":
contentType = "image/jpeg"
case "gif":
contentType = "image/gif"
case "js":
contentType = "application/javascript"
case "css":
contentType = "text/css"
case "woff":
contentType = "application/x-font-woff"
case "ttf":
contentType = "application/x-font-ttf"
case "otf":
contentType = "application/x-font-otf"
case "html":
contentType = "text/html"
}
c.Writer.Header().Set("content-type", contentType)
c.String(200, string(res))
}