feat(): serve static assets

This commit is contained in:
Nicolas Carlier 2020-09-11 11:48:18 +00:00
parent 5dc21a0f53
commit b0496778e8
4 changed files with 25 additions and 0 deletions

View File

@ -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`

View File

@ -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")),
}

19
pkg/api/static.go Normal file
View File

@ -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)
})
}
}

View File

@ -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)"`
}