From 3bae489e35a767fac5a8bba8867fc9ecb1c19b5a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 21 Mar 2022 19:05:18 +0200 Subject: [PATCH] examples: make the vweb_assets.v example more robust (always change work folder to the folder of the executable). Document the vweb.Context.handle_static behaviour. --- examples/vweb/vweb_assets/vweb_assets.v | 2 ++ vlib/vweb/vweb.v | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/vweb/vweb_assets/vweb_assets.v b/examples/vweb/vweb_assets/vweb_assets.v index 058f45922b..1033f7ea8f 100644 --- a/examples/vweb/vweb_assets/vweb_assets.v +++ b/examples/vweb/vweb_assets/vweb_assets.v @@ -1,5 +1,6 @@ module main +import os import vweb // import vweb.assets import time @@ -16,6 +17,7 @@ fn main() { mut app := &App{} app.serve_static('/favicon.ico', 'favicon.ico') // Automatically make available known static mime types found in given directory. + os.chdir(os.dir(os.executable())) ? app.handle_static('assets', true) vweb.run(app, port) } diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index ad11e36b55..49a984bb3c 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -628,8 +628,14 @@ fn (mut ctx Context) scan_static_directory(directory_path string, mount_path str } } -// Handles a directory static +// handle_static is used to mark a folder (relative to the current working folder) +// as one that contains only static resources (css files, images etc). // If `root` is set the mount path for the dir will be in '/' +// Usage: +// ```v +// os.chdir( os.executable() ) ? +// app.handle_static('assets', true) +// ``` pub fn (mut ctx Context) handle_static(directory_path string, root bool) bool { if ctx.done || !os.exists(directory_path) { return false