diff --git a/examples/vweb/static_website/README.md b/examples/vweb/static_website/README.md new file mode 100644 index 00000000000000..dbeba683637d9c --- /dev/null +++ b/examples/vweb/static_website/README.md @@ -0,0 +1,9 @@ +# Host the vlang website + +Here is an example on how to use the vweb server's static capabilities, +to host a static website from the `dist/` folder. Just run the server, +it will be available at http://localhost:8080/ : + +```bash +v run server.v +``` diff --git a/examples/vweb/static_website/dist/another.html b/examples/vweb/static_website/dist/another.html new file mode 100644 index 00000000000000..a41a6515acdd79 --- /dev/null +++ b/examples/vweb/static_website/dist/another.html @@ -0,0 +1,13 @@ + + + + + + Website other page + + +

Hello, another world!

+

Welcome to this another page of a demo website.

+

Click here to go to main page.

+ + diff --git a/examples/vweb/static_website/dist/index.html b/examples/vweb/static_website/dist/index.html new file mode 100644 index 00000000000000..8108e67d4be1df --- /dev/null +++ b/examples/vweb/static_website/dist/index.html @@ -0,0 +1,13 @@ + + + + + + Website main page + + +

Hello, world!

+

Welcome to this demo website.

+

Click here to go to another page.

+ + diff --git a/examples/vweb/static_website/server.v b/examples/vweb/static_website/server.v new file mode 100644 index 00000000000000..3ceeda4d26d4eb --- /dev/null +++ b/examples/vweb/static_website/server.v @@ -0,0 +1,21 @@ +module main + +import x.vweb +import os + +pub struct Context { + vweb.Context +} + +pub struct App { + vweb.StaticHandler +} + +fn main() { + // make sure that the working folder is the one, containing the executable, + // so that 'dist' is a valid relative path from it later: + os.chdir(os.dir(os.executable()))! + mut app := &App{} + app.handle_static('dist', true)! + vweb.run[App, Context](mut app, 8080) +} diff --git a/vlib/x/vweb/README.md b/vlib/x/vweb/README.md index a8ad4847efcf75..cdd42789a088e4 100644 --- a/vlib/x/vweb/README.md +++ b/vlib/x/vweb/README.md @@ -284,7 +284,7 @@ pub fn (mut ctx Context) not_found() vweb.Result { } ``` -## Static files +## Static files and website Vweb also provides a way of handling static files. We can mount a folder at the root of our web app, or at a custom route. To start using static files we have to embed @@ -347,7 +347,12 @@ is available at `/`. // change the second argument to `true` to mount a folder at the app root app.handle_static('static', true)! ``` -We can now access `main.css` directly at http://localhost:8080/css/main.css +We can now access `main.css` directly at http://localhost:8080/css/main.css. + +If a request is made to the root of a static folder, vweb will look for an +`index.html` or `ìndex.htm` file and serve it if available. +Thus, it's also a good way to host a complete website. +A example is available [here](/examples/vweb/static_website). It is also possible to mount the `static` folder at a custom path.