From 219fd5b58ae4492daff2857d53e4ab1e108b5a65 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Wed, 6 Jan 2016 12:20:07 +0100 Subject: [PATCH] Serve public assets --- make.sh | 2 ++ routes/main.go | 51 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/make.sh b/make.sh index 629af0d0..15bf04ac 100755 --- a/make.sh +++ b/make.sh @@ -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/" diff --git a/routes/main.go b/routes/main.go index 42e04ae1..db24fa90 100644 --- a/routes/main.go +++ b/routes/main.go @@ -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 -func serve404(c *gin.Context) { - c.AbortWithStatus(404) + if !strings.HasPrefix(path, "/public") { + c.Next() + return + } + + 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)) }