2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-07-31 19:15:05 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2017-09-08 00:43:33 +00:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2017-07-31 19:15:05 +00:00
|
|
|
"net/http"
|
2017-09-08 00:43:33 +00:00
|
|
|
"path/filepath"
|
2017-07-31 19:15:05 +00:00
|
|
|
"time"
|
|
|
|
|
2019-08-27 11:01:29 +00:00
|
|
|
"github.com/laszlocph/woodpecker/model"
|
|
|
|
"github.com/laszlocph/woodpecker/shared/token"
|
|
|
|
"github.com/laszlocph/woodpecker/version"
|
2019-11-12 20:08:17 +00:00
|
|
|
"github.com/laszlocph/woodpecker/web/dist"
|
2017-07-31 19:15:05 +00:00
|
|
|
|
|
|
|
"github.com/dimfeld/httptreemux"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Endpoint provides the website endpoints.
|
|
|
|
type Endpoint interface {
|
|
|
|
// Register registers the provider endpoints.
|
|
|
|
Register(*httptreemux.ContextMux)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns the default website endpoint.
|
2017-09-20 19:29:57 +00:00
|
|
|
func New(opt ...Option) Endpoint {
|
|
|
|
opts := new(Options)
|
|
|
|
for _, f := range opt {
|
|
|
|
f(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.path != "" {
|
|
|
|
return fromPath(opts)
|
|
|
|
}
|
|
|
|
|
2017-09-08 00:43:33 +00:00
|
|
|
return &website{
|
2017-09-20 19:29:57 +00:00
|
|
|
fs: dist.New(),
|
|
|
|
opts: opts,
|
|
|
|
tmpl: mustCreateTemplate(
|
2017-09-08 00:43:33 +00:00
|
|
|
string(dist.MustLookup("/index.html")),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 19:29:57 +00:00
|
|
|
func fromPath(opts *Options) *website {
|
|
|
|
f := filepath.Join(opts.path, "index.html")
|
2017-09-08 00:43:33 +00:00
|
|
|
b, err := ioutil.ReadFile(f)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return &website{
|
2017-09-20 19:29:57 +00:00
|
|
|
fs: http.Dir(opts.path),
|
|
|
|
tmpl: mustCreateTemplate(string(b)),
|
|
|
|
opts: opts,
|
2017-09-08 00:43:33 +00:00
|
|
|
}
|
2017-07-31 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:43:33 +00:00
|
|
|
type website struct {
|
2017-09-20 19:29:57 +00:00
|
|
|
opts *Options
|
|
|
|
fs http.FileSystem
|
|
|
|
tmpl *template.Template
|
2017-09-08 00:43:33 +00:00
|
|
|
}
|
2017-07-31 19:15:05 +00:00
|
|
|
|
|
|
|
func (w *website) Register(mux *httptreemux.ContextMux) {
|
2017-09-08 00:43:33 +00:00
|
|
|
h := http.FileServer(w.fs)
|
2017-07-31 19:15:05 +00:00
|
|
|
h = setupCache(h)
|
2019-11-13 18:50:54 +00:00
|
|
|
mux.Handler("GET", "/favicon.svg", h)
|
2017-09-08 00:43:33 +00:00
|
|
|
mux.Handler("GET", "/static/*filepath", h)
|
|
|
|
mux.NotFoundHandler = w.handleIndex
|
2017-07-31 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 00:43:33 +00:00
|
|
|
func (w *website) handleIndex(rw http.ResponseWriter, r *http.Request) {
|
2017-07-31 19:15:05 +00:00
|
|
|
rw.WriteHeader(200)
|
|
|
|
|
|
|
|
var csrf string
|
|
|
|
var user, _ = ToUser(r.Context())
|
|
|
|
if user != nil {
|
|
|
|
csrf, _ = token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
}
|
2017-09-20 19:29:57 +00:00
|
|
|
var syncing bool
|
|
|
|
if user != nil {
|
|
|
|
syncing = time.Unix(user.Synced, 0).Add(w.opts.sync).Before(time.Now())
|
|
|
|
}
|
2017-07-31 19:15:05 +00:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"user": user,
|
|
|
|
"csrf": csrf,
|
2017-09-20 19:29:57 +00:00
|
|
|
"syncing": syncing,
|
2019-11-12 20:08:17 +00:00
|
|
|
"version": version.String(),
|
2017-07-31 19:15:05 +00:00
|
|
|
}
|
|
|
|
rw.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
2017-09-08 00:43:33 +00:00
|
|
|
|
2017-09-20 19:29:57 +00:00
|
|
|
w.tmpl.Execute(rw, params)
|
2017-07-31 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupCache(h http.Handler) http.Handler {
|
|
|
|
data := []byte(time.Now().String())
|
|
|
|
etag := fmt.Sprintf("%x", md5.Sum(data))
|
|
|
|
|
|
|
|
return http.HandlerFunc(
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
|
|
w.Header().Del("Expires")
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUser returns a context with the current authenticated user.
|
|
|
|
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
|