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 <graham.miln@dssw.co.uk>
Reviewed-on: https://kolaente.dev/vikunja/api/pulls/1174
Reviewed-by: konrad <k@knt.li>
Co-authored-by: grahammiln <grahammiln@noreply.kolaente.de>
Co-committed-by: grahammiln <grahammiln@noreply.kolaente.de>
This commit is contained in:
grahammiln 2022-05-23 20:49:28 +00:00 committed by konrad
parent f5ebada913
commit acaa85083f
4 changed files with 25 additions and 1 deletions

View file

@ -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 # 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. # with a config file which will then be used.
rootpath: <rootpath> rootpath: <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 # The max number of items which can be returned per page
maxitemsperpage: 50 maxitemsperpage: 50
# Enable the caldav endpoint, see the docs for more details # Enable the caldav endpoint, see the docs for more details

View file

@ -161,6 +161,17 @@ Full path: `service.rootpath`
Environment path: `VIKUNJA_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: `<empty>`
Full path: `service.staticpath`
Environment path: `VIKUNJA_SERVICE_STATICPATH`
### maxitemsperpage ### maxitemsperpage
The max number of items which can be returned per page The max number of items which can be returned per page
@ -426,16 +437,18 @@ Full path: `database.sslmode`
Environment path: `VIKUNJA_DATABASE_SSLMODE` Environment path: `VIKUNJA_DATABASE_SSLMODE`
### sslcert ### sslcert
The path to the client cert. Only used with postgres. The path to the client cert. Only used with postgres.
Default: `<empty>` Default: `<empty>`
Full path: `database.sslcert` Full path: `database.sslcert`
Environment path: `VIKUNJA_DATABASE_SSLCERT` Environment path: `VIKUNJA_DATABASE_SSLCERT`
### sslkey ### sslkey
The path to the client key. Only used with postgres. The path to the client key. Only used with postgres.
@ -446,6 +459,7 @@ Full path: `database.sslkey`
Environment path: `VIKUNJA_DATABASE_SSLKEY` Environment path: `VIKUNJA_DATABASE_SSLKEY`
### sslrootcert ### sslrootcert
The path to the ca cert. Only used with postgres. The path to the ca cert. Only used with postgres.
@ -456,6 +470,7 @@ Full path: `database.sslrootcert`
Environment path: `VIKUNJA_DATABASE_SSLROOTCERT` Environment path: `VIKUNJA_DATABASE_SSLROOTCERT`
### tls ### tls
Enable SSL/TLS for mysql connections. Options: false, true, skip-verify, preferred Enable SSL/TLS for mysql connections. Options: false, true, skip-verify, preferred

View file

@ -47,6 +47,7 @@ const (
ServiceFrontendurl Key = `service.frontendurl` ServiceFrontendurl Key = `service.frontendurl`
ServiceEnableCaldav Key = `service.enablecaldav` ServiceEnableCaldav Key = `service.enablecaldav`
ServiceRootpath Key = `service.rootpath` ServiceRootpath Key = `service.rootpath`
ServiceStaticpath Key = `service.staticpath`
ServiceMaxItemsPerPage Key = `service.maxitemsperpage` ServiceMaxItemsPerPage Key = `service.maxitemsperpage`
// Deprecated: Use metrics.enabled // Deprecated: Use metrics.enabled
ServiceEnableMetrics Key = `service.enablemetrics` ServiceEnableMetrics Key = `service.enablemetrics`
@ -274,6 +275,7 @@ func InitDefaultConfig() {
ServiceEnableCaldav.setDefault(true) ServiceEnableCaldav.setDefault(true)
ServiceRootpath.setDefault(getBinaryDirLocation()) ServiceRootpath.setDefault(getBinaryDirLocation())
ServiceStaticpath.setDefault("")
ServiceMaxItemsPerPage.setDefault(50) ServiceMaxItemsPerPage.setDefault(50)
ServiceEnableMetrics.setDefault(false) ServiceEnableMetrics.setDefault(false)
ServiceMotd.setDefault("") ServiceMotd.setDefault("")

View file

@ -203,6 +203,11 @@ func RegisterRoutes(e *echo.Echo) {
// healthcheck // healthcheck
e.GET("/health", HealthcheckHandler) e.GET("/health", HealthcheckHandler)
// static files
if static := config.ServiceStaticpath.GetString(); static != "" {
e.Use(middleware.Static(static))
}
// CORS_SHIT // CORS_SHIT
if config.CorsEnable.GetBool() { if config.CorsEnable.GetBool() {
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ e.Use(middleware.CORSWithConfig(middleware.CORSConfig{