Skip to content
Permalink
Browse files

Reorg asset generation in subpackages

  • Loading branch information...
victorcoder committed Jan 10, 2019
1 parent fa323c2 commit 0c02ce077217a4d6feb5dfff66b20f839b363d06
@@ -38,8 +38,8 @@ doc:
ghp-import -p public

gen:
go generate ./templates
go generate ./static
go generate ./dkron/templates
go generate ./dkron/assets

test:
@bash --norc -i ./scripts/test
@@ -0,0 +1,8 @@
// +build dev

package assets

import "net/http"

// Templates contains project templates.
var Assets http.FileSystem = http.Dir("../../static")

Large diffs are not rendered by default.

@@ -0,0 +1,3 @@
package assets

//go:generate vfsgendev -source="github.com/victorcoder/dkron/dkron/assets".Assets
@@ -5,16 +5,14 @@ import (
"html/template"
"io/ioutil"
"net/http"
"strings"

"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"github.com/victorcoder/dkron/static"
"github.com/victorcoder/dkron/templates"
"github.com/victorcoder/dkron/dkron/assets"
"github.com/victorcoder/dkron/dkron/templates"
)

const (
tmplPath = "templates"
dashboardPathPrefix = "dashboard"
assetsPrefix = "static"
apiPathPrefix = "v1"
@@ -64,8 +62,7 @@ func (a *Agent) DashboardRoutes(r *gin.RouterGroup) {
}
})

r.StaticFS("static", static.Assets)
//r.GET("/static/*asset", servePublic)
r.StaticFS("static", assets.Assets)

dashboard := r.Group("/" + dashboardPathPrefix)
dashboard.GET("/", a.dashboardIndexHandler)
@@ -134,71 +131,27 @@ func mustLoadTemplate(path string) []byte {
func CreateMyRender() multitemplate.Render {
r := multitemplate.New()

status := mustLoadTemplate(tmplPath + "/status.html.tmpl")
dash := mustLoadTemplate(tmplPath + "/dashboard.html.tmpl")
status := mustLoadTemplate("/status.html.tmpl")
dash := mustLoadTemplate("/dashboard.html.tmpl")

r.AddFromStringsFuncs("index", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate(tmplPath+"/index.html.tmpl")))
string(mustLoadTemplate("/index.html.tmpl")))

r.AddFromStringsFuncs("jobs", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate(tmplPath+"/jobs.html.tmpl")))
string(mustLoadTemplate("/jobs.html.tmpl")))

r.AddFromStringsFuncs("executions", funcMap(),
string(dash),
string(status),
string(mustLoadTemplate(tmplPath+"/executions.html.tmpl")))
string(mustLoadTemplate("/executions.html.tmpl")))

return r
}

//var Assets http.FileSystem = http.Dir("../static")
//var Templates http.FileSystem = http.Dir("../templates")

//go:generate vfsgendev -source="github.com/victorcoder/dkron/static".Assets
//go:generate vfsgendev -source="github.com/victorcoder/dkron/templates".Templates
func servePublic(c *gin.Context) {
path := c.Request.URL.Path

path = strings.Replace(path, "/", "", 1)
split := strings.Split(path, ".")
suffix := split[len(split)-1]

res, err := 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))
}

func funcMap() template.FuncMap {
funcs := template.FuncMap{
"toString": func(value []byte) string {
@@ -0,0 +1,3 @@
package templates

//go:generate vfsgendev -source="github.com/victorcoder/dkron/dkron/templates".Templates
@@ -0,0 +1,8 @@
// +build dev

package templates

import "net/http"

// Templates contains project templates.
var Templates http.FileSystem = http.Dir("../../templates")

Some generated files are not rendered by default. Learn more.

This file was deleted.

0 comments on commit 0c02ce0

Please sign in to comment.
You can’t perform that action at this time.