diff --git a/server/web/template.go b/server/web/template.go deleted file mode 100644 index b48675429..000000000 --- a/server/web/template.go +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright 2018 Drone.IO Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package web - -import ( - "bytes" - "encoding/json" - "fmt" - "html/template" - "strings" - - "golang.org/x/net/html" -) - -// default func map with json parser. -var funcMap = template.FuncMap{ - "json": func(v interface{}) template.JS { - a, _ := json.Marshal(v) - return template.JS(a) - }, -} - -// helper function creates a new template from the text string. -func mustCreateTemplate(text string) *template.Template { - templ, err := createTemplate(text) - if err != nil { - panic(err) - } - return templ -} - -// helper function creates a new template from the text string. -func createTemplate(text string) (*template.Template, error) { - templ, err := template.New("_").Funcs(funcMap).Parse(partials) - if err != nil { - return nil, err - } - return templ.Parse( - injectPartials(text), - ) -} - -// helper function that parses the html file and injects -// named partial templates. -func injectPartials(s string) string { - w := new(bytes.Buffer) - r := bytes.NewBufferString(s) - t := html.NewTokenizer(r) - for { - tt := t.Next() - if tt == html.ErrorToken { - break - } - if tt == html.CommentToken { - txt := string(t.Text()) - txt = strings.TrimSpace(txt) - seg := strings.Split(txt, ":") - if len(seg) == 2 && seg[0] == "drone" { - fmt.Fprintf(w, "{{ template %q . }}", seg[1]) - continue - } - } - w.Write(t.Raw()) - } - return w.String() -} - -const partials = ` -{{define "user"}} -{{ if .user }} - -{{ end }} -{{end}} - -{{define "csrf"}} -{{ if .csrf -}} - -{{- end }} -{{end}} - -{{define "version"}} - - -{{end}} - -{{define "docs"}} -{{ if .docs -}} - -{{- end }} -{{end}} -` diff --git a/server/web/template_test.go b/server/web/template_test.go deleted file mode 100644 index 6ad8a2850..000000000 --- a/server/web/template_test.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2018 Drone.IO Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package web - -import ( - "testing" -) - -func Test_injectPartials(t *testing.T) { - got, want := injectPartials(before), after - if got != want { - t.Errorf("Want html %q, got %q", want, got) - } -} - -var before = ` - - - - - - - - -` - -var after = ` - - - - {{ template "version" . }} - {{ template "user" . }} - {{ template "csrf" . }} - - -` diff --git a/server/web/web.go b/server/web/web.go index 880d8478f..6a153f7f4 100644 --- a/server/web/web.go +++ b/server/web/web.go @@ -18,7 +18,6 @@ import ( "context" "crypto/md5" "fmt" - "html/template" "net/http" "time" @@ -26,8 +25,6 @@ import ( "github.com/rs/zerolog/log" "github.com/woodpecker-ci/woodpecker/server/model" - "github.com/woodpecker-ci/woodpecker/shared/token" - "github.com/woodpecker-ci/woodpecker/version" "github.com/woodpecker-ci/woodpecker/web" ) @@ -47,16 +44,14 @@ func New(opt ...Option) Endpoint { return &website{ fs: web.HttpFS(), opts: opts, - tmpl: mustCreateTemplate( - string(web.MustLookup("index.html")), - ), + data: web.MustLookup("index.html"), } } type website struct { opts *Options fs http.FileSystem - tmpl *template.Template + data []byte } func (w *website) Register(mux *gin.Engine) { @@ -68,33 +63,11 @@ func (w *website) Register(mux *gin.Engine) { } func (w *website) handleIndex(rw http.ResponseWriter, r *http.Request) { - var csrf string - var user, _ = ToUser(r.Context()) - if user != nil { - csrf, _ = token.New( - token.CsrfToken, - user.Login, - ).Sign(user.Hash) - } - var syncing bool - if user != nil { - syncing = time.Unix(user.Synced, 0).Add(w.opts.sync).Before(time.Now()) - } - params := map[string]interface{}{ - "user": user, - "csrf": csrf, - "syncing": syncing, - "version": version.String(), - } rw.Header().Set("Content-Type", "text/html; charset=UTF-8") - - if err := w.tmpl.Execute(rw, params); err != nil { - rw.WriteHeader(http.StatusInternalServerError) - log.Error().Err(err).Msg("execute template") - return - } - rw.WriteHeader(200) + if _, err := rw.Write(w.data); err != nil { + log.Error().Err(err).Msg("can not write index.html") + } } func setupCache(h http.Handler) http.Handler { @@ -116,12 +89,6 @@ func WithUser(c context.Context, user *model.User) context.Context { return context.WithValue(c, userKey, user) } -// ToUser returns a user from the context. -func ToUser(c context.Context) (*model.User, bool) { - user, ok := c.Value(userKey).(*model.User) - return user, ok -} - type key int const userKey key = 0