From b0496778e82016e5d2aab100a2f58784d106e230 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Fri, 11 Sep 2020 11:48:18 +0000 Subject: [PATCH] feat(): serve static assets --- etc/default/webhookd.env | 4 ++++ pkg/api/routes.go | 1 + pkg/api/static.go | 19 +++++++++++++++++++ pkg/config/config.go | 1 + 4 files changed, 25 insertions(+) create mode 100644 pkg/api/static.go diff --git a/etc/default/webhookd.env b/etc/default/webhookd.env index 4d440f7..04f662d 100644 --- a/etc/default/webhookd.env +++ b/etc/default/webhookd.env @@ -38,6 +38,10 @@ # Example: `/etc/webhookd/github_deploy_key.pem` #WHD_SCRIPTS_GIT_KEY= +# Static file directory to serve on /static path, disabled by default +# Example: `./var/www` +#WHD_STATIC_DIR= + # Trust store URI, disabled by default # Enable HTTP signature verification if set. # Example: `/etc/webhookd/pubkey.pem` diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 5da6721..b48fadb 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -30,6 +30,7 @@ type Routes []Route var routes = Routes{ route("/", index, middleware.Methods("GET", "POST")), + route("/static/", static("/static/"), middleware.Methods("GET")), route("/healthz", healthz, middleware.Methods("GET")), route("/varz", varz, middleware.Methods("GET")), } diff --git a/pkg/api/static.go b/pkg/api/static.go new file mode 100644 index 0000000..2a610dd --- /dev/null +++ b/pkg/api/static.go @@ -0,0 +1,19 @@ +package api + +import ( + "net/http" + + "github.com/ncarlier/webhookd/pkg/config" +) + +func static(prefix string) HandlerFunc { + return func(conf *config.Config) http.Handler { + if conf.StaticDir != "" { + fs := http.FileServer(http.Dir(conf.StaticDir)) + return http.StripPrefix(prefix, fs) + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "404 page not found", http.StatusNotFound) + }) + } +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 3eb4650..d8247db 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,6 +13,7 @@ type Config struct { ScriptDir string `flag:"scripts" desc:"Scripts location" default:"scripts"` PasswdFile string `flag:"passwd-file" desc:"Password file for basic HTTP authentication" default:".htpasswd"` LogDir string `flag:"log-dir" desc:"Hook execution logs location" default:""` + StaticDir string `flag:"static-dir" desc:"Static file directory to serve on /static path" default:""` NotificationURI string `flag:"notification-uri" desc:"Notification URI"` TrustStoreFile string `flag:"trust-store-file" desc:"Trust store used by HTTP signature verifier (.pem or .p12)"` }