mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-29 13:21:10 +00:00
Simplify web router code (#541)
This commit is contained in:
parent
c1a8884d62
commit
8b476e7722
9 changed files with 34 additions and 167 deletions
|
@ -35,10 +35,9 @@ func main() {
|
||||||
app.Usage = "woodpecker server"
|
app.Usage = "woodpecker server"
|
||||||
app.Action = run
|
app.Action = run
|
||||||
app.Flags = flags
|
app.Flags = flags
|
||||||
app.Before = before
|
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,8 +45,8 @@ import (
|
||||||
"github.com/woodpecker-ci/woodpecker/server/remote"
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/router"
|
"github.com/woodpecker-ci/woodpecker/server/router"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware"
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/router/middleware/logger"
|
|
||||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||||
|
"github.com/woodpecker-ci/woodpecker/server/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func run(c *cli.Context) error {
|
func run(c *cli.Context) error {
|
||||||
|
@ -120,7 +120,7 @@ func run(c *cli.Context) error {
|
||||||
var webUIServe func(w http.ResponseWriter, r *http.Request)
|
var webUIServe func(w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
if proxyWebUI == "" {
|
if proxyWebUI == "" {
|
||||||
webUIServe = setupTree(c).ServeHTTP
|
webUIServe = web.New().ServeHTTP
|
||||||
} else {
|
} else {
|
||||||
origin, _ := url.Parse(proxyWebUI)
|
origin, _ := url.Parse(proxyWebUI)
|
||||||
|
|
||||||
|
@ -138,7 +138,7 @@ func run(c *cli.Context) error {
|
||||||
// setup the server and start the listener
|
// setup the server and start the listener
|
||||||
handler := router.Load(
|
handler := router.Load(
|
||||||
webUIServe,
|
webUIServe,
|
||||||
logger.Logger(time.RFC3339, true),
|
middleware.Logger(time.RFC3339, true),
|
||||||
middleware.Version,
|
middleware.Version,
|
||||||
middleware.Config(c),
|
middleware.Config(c),
|
||||||
middleware.Store(c, store_),
|
middleware.Store(c, store_),
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -43,7 +42,6 @@ import (
|
||||||
"github.com/woodpecker-ci/woodpecker/server/remote/gogs"
|
"github.com/woodpecker-ci/woodpecker/server/remote/gogs"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/store/datastore"
|
"github.com/woodpecker-ci/woodpecker/server/store/datastore"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/web"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setupStore(c *cli.Context) (store.Store, error) {
|
func setupStore(c *cli.Context) (store.Store, error) {
|
||||||
|
@ -303,18 +301,6 @@ func setupCoding(c *cli.Context) (remote.Remote, error) {
|
||||||
return coding.New(opts)
|
return coding.New(opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupTree(c *cli.Context) *gin.Engine {
|
|
||||||
tree := gin.New()
|
|
||||||
tree.UseRawPath = true
|
|
||||||
web.New(
|
|
||||||
web.WithSync(time.Hour*72),
|
|
||||||
web.WithDocs(c.String("docs")),
|
|
||||||
).Register(tree)
|
|
||||||
return tree
|
|
||||||
}
|
|
||||||
|
|
||||||
func before(c *cli.Context) error { return nil }
|
|
||||||
|
|
||||||
func setupMetrics(g *errgroup.Group, store_ store.Store) {
|
func setupMetrics(g *errgroup.Group, store_ store.Store) {
|
||||||
pendingJobs := promauto.NewGauge(prometheus.GaugeOpts{
|
pendingJobs := promauto.NewGauge(prometheus.GaugeOpts{
|
||||||
Namespace: "woodpecker",
|
Namespace: "woodpecker",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package logger
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
|
@ -29,7 +29,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load loads the router
|
// Load loads the router
|
||||||
func Load(serveHTTP func(w http.ResponseWriter, r *http.Request), middleware ...gin.HandlerFunc) http.Handler {
|
func Load(noRouteHandler http.HandlerFunc, middleware ...gin.HandlerFunc) http.Handler {
|
||||||
e := gin.New()
|
e := gin.New()
|
||||||
e.UseRawPath = true
|
e.UseRawPath = true
|
||||||
e.Use(gin.Recovery())
|
e.Use(gin.Recovery())
|
||||||
|
@ -46,17 +46,9 @@ func Load(serveHTTP func(w http.ResponseWriter, r *http.Request), middleware ...
|
||||||
e.Use(session.SetUser())
|
e.Use(session.SetUser())
|
||||||
e.Use(token.Refresh)
|
e.Use(token.Refresh)
|
||||||
|
|
||||||
e.NoRoute(func(c *gin.Context) {
|
e.NoRoute(gin.WrapF(noRouteHandler))
|
||||||
req := c.Request.WithContext(
|
|
||||||
web.WithUser(
|
|
||||||
c.Request.Context(),
|
|
||||||
session.User(c),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
serveHTTP(c.Writer, req)
|
|
||||||
})
|
|
||||||
|
|
||||||
e.GET("/web-config.js", web.WebConfig)
|
e.GET("/web-config.js", web.Config)
|
||||||
|
|
||||||
e.GET("/logout", api.GetLogout)
|
e.GET("/logout", api.GetLogout)
|
||||||
e.GET("/login", api.HandleLogin)
|
e.GET("/login", api.HandleLogin)
|
||||||
|
|
|
@ -29,7 +29,7 @@ import (
|
||||||
"github.com/woodpecker-ci/woodpecker/version"
|
"github.com/woodpecker-ci/woodpecker/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WebConfig(c *gin.Context) {
|
func Config(c *gin.Context) {
|
||||||
user := session.User(c)
|
user := session.User(c)
|
||||||
|
|
||||||
var csrf string
|
var csrf string
|
||||||
|
|
|
@ -1,42 +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 "time"
|
|
||||||
|
|
||||||
// Options defines website handler options.
|
|
||||||
type Options struct {
|
|
||||||
sync time.Duration
|
|
||||||
docs string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Option configures the website handler.
|
|
||||||
type Option func(*Options)
|
|
||||||
|
|
||||||
// WithSync configures the website handler with the duration value
|
|
||||||
// used to determine if the user account requires synchronization.
|
|
||||||
func WithSync(d time.Duration) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.sync = d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithDocs configures the website handler with the documentation
|
|
||||||
// website address, which should be included in the user interface.
|
|
||||||
func WithDocs(s string) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.docs = s
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,36 +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"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWithSync(t *testing.T) {
|
|
||||||
opts := new(Options)
|
|
||||||
WithSync(time.Minute)(opts)
|
|
||||||
if got, want := opts.sync, time.Minute; got != want {
|
|
||||||
t.Errorf("Want sync duration %v, got %v", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWithDocs(t *testing.T) {
|
|
||||||
opts := new(Options)
|
|
||||||
WithDocs("http://docs.drone.io")(opts)
|
|
||||||
if got, want := opts.docs, "http://docs.drone.io"; got != want {
|
|
||||||
t.Errorf("Want documentation url %q, got %q", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -15,7 +15,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -24,71 +23,40 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
|
||||||
"github.com/woodpecker-ci/woodpecker/web"
|
"github.com/woodpecker-ci/woodpecker/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Endpoint provides the website endpoints.
|
// etag is an identifier for a resource version
|
||||||
type Endpoint interface {
|
// it lets caches determine if resource is still the same and not send it again
|
||||||
// Register registers the provider endpoints.
|
var etag = fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String())))
|
||||||
Register(*gin.Engine)
|
|
||||||
|
// New returns a gin engine to serve the web frontend.
|
||||||
|
func New() *gin.Engine {
|
||||||
|
e := gin.New()
|
||||||
|
|
||||||
|
e.Use(setupCache)
|
||||||
|
|
||||||
|
h := http.FileServer(web.HttpFS())
|
||||||
|
e.GET("/favicon.svg", gin.WrapH(h))
|
||||||
|
e.GET("/assets/*filepath", gin.WrapH(h))
|
||||||
|
|
||||||
|
e.NoRoute(handleIndex)
|
||||||
|
|
||||||
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns the default website endpoint.
|
func handleIndex(c *gin.Context) {
|
||||||
func New(opt ...Option) Endpoint {
|
rw := c.Writer
|
||||||
opts := new(Options)
|
data := web.MustLookup("index.html")
|
||||||
for _, f := range opt {
|
|
||||||
f(opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &website{
|
|
||||||
fs: web.HttpFS(),
|
|
||||||
opts: opts,
|
|
||||||
data: web.MustLookup("index.html"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type website struct {
|
|
||||||
opts *Options
|
|
||||||
fs http.FileSystem
|
|
||||||
data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *website) Register(mux *gin.Engine) {
|
|
||||||
h := http.FileServer(w.fs)
|
|
||||||
h = setupCache(h)
|
|
||||||
mux.GET("/favicon.svg", gin.WrapH(h))
|
|
||||||
mux.GET("/assets/*filepath", gin.WrapH(h))
|
|
||||||
mux.NoRoute(gin.WrapF(w.handleIndex))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *website) handleIndex(rw http.ResponseWriter, r *http.Request) {
|
|
||||||
rw.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
rw.Header().Set("Content-Type", "text/html; charset=UTF-8")
|
||||||
rw.WriteHeader(200)
|
rw.WriteHeader(200)
|
||||||
if _, err := rw.Write(w.data); err != nil {
|
if _, err := rw.Write(data); err != nil {
|
||||||
log.Error().Err(err).Msg("can not write index.html")
|
log.Error().Err(err).Msg("can not write index.html")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupCache(h http.Handler) http.Handler {
|
func setupCache(c *gin.Context) {
|
||||||
data := []byte(time.Now().String())
|
c.Writer.Header().Set("Cache-Control", "public, max-age=31536000")
|
||||||
etag := fmt.Sprintf("%x", md5.Sum(data))
|
c.Writer.Header().Del("Expires")
|
||||||
|
c.Writer.Header().Set("ETag", etag)
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
type key int
|
|
||||||
|
|
||||||
const userKey key = 0
|
|
||||||
|
|
Loading…
Reference in a new issue