From acaa85083f2bebbc67608ae0f454ed5e9a3ef8a0 Mon Sep 17 00:00:00 2001 From: grahammiln Date: Mon, 23 May 2022 20:49:28 +0000 Subject: [PATCH] feat: ability to serve static files (#1174) Added the configuration key, `service.staticpath`, to serve files from the configuration path on root (/). Serving static files allows the api service to also serve the frontend content. This is 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. ## Implementation Providing a path, via the configuration or environment, adds a static file middleware to serve the path's contents from root (/). By default, the configuration path is empty and Vikunja's existing behaviour is unchanged. Co-authored-by: Graham Miln Reviewed-on: https://kolaente.dev/vikunja/api/pulls/1174 Reviewed-by: konrad Co-authored-by: grahammiln Co-committed-by: grahammiln --- config.yml.sample | 2 ++ docs/content/doc/setup/config.md | 17 ++++++++++++++++- pkg/config/config.go | 2 ++ pkg/routes/routes.go | 5 +++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/config.yml.sample b/config.yml.sample index c4c918ae74..0b368866b3 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -22,6 +22,8 @@ service: # Vikunja will also look in this path for a config file, so you could provide only this variable to point to a folder # with a config file which will then be used. rootpath: + # Path on the file system to serve static files from. Set to the path of the frontend files to host frontend alongside the api. + staticpath: "" # The max number of items which can be returned per page maxitemsperpage: 50 # Enable the caldav endpoint, see the docs for more details diff --git a/docs/content/doc/setup/config.md b/docs/content/doc/setup/config.md index d297f85160..9124b259bc 100644 --- a/docs/content/doc/setup/config.md +++ b/docs/content/doc/setup/config.md @@ -161,6 +161,17 @@ Full path: `service.rootpath` Environment path: `VIKUNJA_SERVICE_ROOTPATH` +### staticpath + +Path on the file system to serve static files from. Set to the path of the frontend files to host frontend alongside the api. + +Default: `` + +Full path: `service.staticpath` + +Environment path: `VIKUNJA_SERVICE_STATICPATH` + + ### maxitemsperpage The max number of items which can be returned per page @@ -426,16 +437,18 @@ Full path: `database.sslmode` Environment path: `VIKUNJA_DATABASE_SSLMODE` + ### sslcert The path to the client cert. Only used with postgres. - + Default: `` Full path: `database.sslcert` Environment path: `VIKUNJA_DATABASE_SSLCERT` + ### sslkey The path to the client key. Only used with postgres. @@ -446,6 +459,7 @@ Full path: `database.sslkey` Environment path: `VIKUNJA_DATABASE_SSLKEY` + ### sslrootcert The path to the ca cert. Only used with postgres. @@ -456,6 +470,7 @@ Full path: `database.sslrootcert` Environment path: `VIKUNJA_DATABASE_SSLROOTCERT` + ### tls Enable SSL/TLS for mysql connections. Options: false, true, skip-verify, preferred diff --git a/pkg/config/config.go b/pkg/config/config.go index dfde771ef4..ff40e2cb45 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 d5f79a8c5a..9c27f9c969 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{