Skip to content

Commit

Permalink
Reorg asset generation in subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Jan 10, 2019
1 parent fa323c2 commit 0c02ce0
Show file tree
Hide file tree
Showing 11 changed files with 1,341 additions and 14,382 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions dkron/assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build dev

package assets

import "net/http"

// Templates contains project templates.
var Assets http.FileSystem = http.Dir("../../static")
1,308 changes: 1,308 additions & 0 deletions dkron/assets/assets_vfsdata.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions dkron/assets/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package assets

//go:generate vfsgendev -source="github.com/victorcoder/dkron/dkron/assets".Assets
63 changes: 8 additions & 55 deletions dkron/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions dkron/templates/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package templates

//go:generate vfsgendev -source="github.com/victorcoder/dkron/dkron/templates".Templates
8 changes: 8 additions & 0 deletions dkron/templates/templates.go
Original file line number Diff line number Diff line change
@@ -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 about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions static/assets.go

This file was deleted.

Loading

0 comments on commit 0c02ce0

Please sign in to comment.