Add serve func for uigo

This commit is contained in:
Jannis Mattheis 2018-03-15 20:35:10 +01:00 committed by Jannis Mattheis
parent c76facacb6
commit 41259f3f77
1 changed files with 26 additions and 0 deletions

26
uigo/serve.go Normal file
View File

@ -0,0 +1,26 @@
package uigo
import (
"github.com/gobuffalo/packr"
"github.com/gin-gonic/gin"
"net/http"
)
var box = packr.NewBox("../ui/build")
// Register registers the ui on the root path.
func Register(r *gin.Engine) {
r.GET("/", serveFile("index.html", "text/html"))
r.GET("/index.html", serveFile("index.html", "text/html"))
r.GET("/manifest.json", serveFile("manifest.json", "application/json"))
r.GET("/service-worker.js", serveFile("service-worker.js", "text/javascript"))
r.GET("/assets-manifest.json", serveFile("asserts-manifest.json", "application/json"))
r.GET("/static/*any", gin.WrapH(http.FileServer(box)))
}
func serveFile(name, contentType string) gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Header("Content-Type", contentType)
ctx.String(200, box.String(name))
}
}