From d998b8099f8d5c612bf2b59cfb6f7c73996a65b8 Mon Sep 17 00:00:00 2001 From: Graham Miln Date: Sun, 22 May 2022 14:27:58 +0200 Subject: [PATCH] Add ability to serve static files. Added a configuration key, `service.staticpath`, to allow the serving of files from the provided path. By default, the path is empty and Vikunja's existing behaviour is unchanged. Providing a path via the configuration, adds a static file middleware to serve the path's contents from root (/). Being able to serve static files allows the api service to also serve the frontend content. This adds a simple option for deploying Vikunja without needing any other servers or proxies. Running a complete instance becomes: VIKUNJA_SERVICE_STATICPATH=/path/to/frontend ./vikunja Where /path/to/frontend is a copy of Vikunja's frontend static files. Future improvements could embed a copy of the frontend files via the fs.FS and embed package. Alternatively a reverse proxy could be offered using http/httputil's ReverseProxy; this would allow another server to maintain a default set of frontend files without embedding. --- CHANGELOG.md | 4 ++++ pkg/config/config.go | 2 ++ pkg/routes/routes.go | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c0343473..617b982af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). All releases can be found on https://code.vikunja.io/api/releases. +## [Unreleased] + +* Add static file support for simplified deployment + ## [0.18.1] - 2021-09-08 ### Fixed diff --git a/pkg/config/config.go b/pkg/config/config.go index 894dbd884..4a815339f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -47,6 +47,7 @@ const ( ServiceFrontendurl Key = `service.frontendurl` ServiceEnableCaldav Key = `service.enablecaldav` ServiceRootpath Key = `service.rootpath` + ServiceStaticpath Key = `service.staticpath` ServiceMaxItemsPerPage Key = `service.maxitemsperpage` // Deprecated: Use metrics.enabled ServiceEnableMetrics Key = `service.enablemetrics` @@ -274,6 +275,7 @@ func InitDefaultConfig() { ServiceEnableCaldav.setDefault(true) ServiceRootpath.setDefault(getBinaryDirLocation()) + ServiceStaticpath.setDefault("") ServiceMaxItemsPerPage.setDefault(50) ServiceEnableMetrics.setDefault(false) ServiceMotd.setDefault("") diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index d5f79a8c5..9c27f9c96 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -203,6 +203,11 @@ func RegisterRoutes(e *echo.Echo) { // healthcheck e.GET("/health", HealthcheckHandler) + // static files + if static := config.ServiceStaticpath.GetString(); static != "" { + e.Use(middleware.Static(static)) + } + // CORS_SHIT if config.CorsEnable.GetBool() { e.Use(middleware.CORSWithConfig(middleware.CORSConfig{