improve UI loader

This commit is contained in:
Brad Rydzewski
2017-09-07 17:43:33 -07:00
parent 7826888729
commit 49ea713a36
37 changed files with 8601 additions and 566 deletions

View File

@@ -4,12 +4,14 @@ import (
"context"
"crypto/md5"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"path/filepath"
"time"
"github.com/drone/drone-ui/dist"
"github.com/drone/drone/model"
"github.com/drone/drone/server/template"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/version"
@@ -24,23 +26,42 @@ type Endpoint interface {
// New returns the default website endpoint.
func New() Endpoint {
return new(website)
return &website{
fs: dist.New(),
templ: mustCreateTemplate(
string(dist.MustLookup("/index.html")),
),
}
}
type website struct{}
// FromPath returns the website endpoint that
// serves the webpage form disk at path p.
func FromPath(p string) Endpoint {
f := filepath.Join(p, "index.html")
b, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
return &website{
fs: http.Dir(p),
templ: mustCreateTemplate(string(b)),
}
}
type website struct {
fs http.FileSystem
templ *template.Template
}
func (w *website) Register(mux *httptreemux.ContextMux) {
r := dist.New()
h := http.FileServer(r)
h := http.FileServer(w.fs)
h = setupCache(h)
mux.Handler("GET", "/favicon-32x32.png", h)
mux.Handler("GET", "/favicon-16x16.png", h)
mux.Handler("GET", "/src/*filepath", h)
mux.Handler("GET", "/bower_components/*filepath", h)
mux.NotFoundHandler = handleIndex
mux.Handler("GET", "/favicon.png", h)
mux.Handler("GET", "/static/*filepath", h)
mux.NotFoundHandler = w.handleIndex
}
func handleIndex(rw http.ResponseWriter, r *http.Request) {
func (w *website) handleIndex(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(200)
var csrf string
@@ -57,7 +78,8 @@ func handleIndex(rw http.ResponseWriter, r *http.Request) {
"version": version.Version.String(),
}
rw.Header().Set("Content-Type", "text/html; charset=UTF-8")
template.T.ExecuteTemplate(rw, "index_polymer.html", params)
w.templ.Execute(rw, params)
}
func setupCache(h http.Handler) http.Handler {
@@ -100,3 +122,32 @@ func ToUser(c context.Context) (*model.User, bool) {
type key int
const userKey key = 0
// var partials = templ
// var templ = `
// {{define "user"}}
// <script>
// {{ if .user }}
// window.USER = {{ json .user }};
// {{ end }}
// </script>
// {{end}}
//
// {{define "csrf"}}
// <script>
// {{ if .csrf }}window.DRONE_CSRF = "{{ .csrf }}"{{ end }}
// </script>
// {{end}}
//
// {{define "version"}}
// <meta name="version" content="{{ .version }}">
// {{end}}
// `
// var funcMap = template.FuncMap{"json": marshal}
//
// // marshal is a template helper function to render data as json.
// func marshal(v interface{}) template.JS {
// a, _ := json.Marshal(v)
// return template.JS(a)
// }