v icon indicating copy to clipboard operation
v copied to clipboard

Problem serving local files with vweb

Open serge-hulne opened this issue 2 years ago • 1 comments

Describe the bug

According to V's doc, using vweb, in order to serve static files one just has to add the following line to a server:

app.mount_static_folder_at(os.resource_abs_path('dist'), '/content')

So, for instance: the following server is supposed to serve the content of the local directory ./dist at the address: http://localhost:8080/content

The complet code being :

import os

struct App {
    vweb.Context
}

fn main() {
    mut app := &App{}
    println(os.resource_abs_path('dist'))
    app.mount_static_folder_at(os.resource_abs_path('dist'), '/content')
    vweb.run(&App{}, 8080)
}

["/"]
fn (mut app App) root() vweb.Result {
    return app.text('Hello from root')
}   


Expected Behavior

The server is supposed to serve the content of the local directory ./dist at the address: http://localhost:8080/content

Current Behavior

404 Not Found

Reproduction Steps

println(os.resource_abs_path('dist')) yields

C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2\dist

Which is correct, plus:

ls lists

----                 -------------         ------ ----
d-----         2/11/2023   6:45 PM                dist
d-----         2/11/2023   6:45 PM                src
-a----         2/11/2023   5:53 PM            139 .editorconfig
-a----         2/11/2023   5:53 PM            148 .gitattributes
-a----         2/11/2023   5:53 PM            237 .gitignore
-a----         2/11/2023   6:06 PM             85 v.mod
-a----         2/12/2023  12:56 PM        2037248 v2.exe


PS C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2> ls .\dist\


    Directory: C:\Users\serge\Documents\vue-tests\vue-tests-nomodules\v2\dist


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2/11/2023   6:32 PM            295 app.html
-a----         2/11/2023   6:42 PM            295 index.html

Possible Solution

No response

Additional Information/Context

Screenshot (257)

No response

V version

V 0.3.3 90591eb

Environment details (OS name and version, etc.)

Windows 11

serge-hulne avatar Feb 12 '23 12:02 serge-hulne

If you want to serve the content of a directory at a specified route you can do it like this:

// main.v
/*
Directory structure:
├── dist
│   ├── content
│   │   └── index.html
│   └── index.html
└── main.v
*/

module main

import os
import vweb

struct App {
	vweb.Context
}

fn main() {
	mut app := &App{}
	app.mount_static_folder_at(os.resource_abs_path('.'), '/')
	vweb.run(app, 8080)
}

['/']
pub fn (mut app App) index() vweb.Result {
	content := $tmpl('dist/index.html')
	return app.html(content)
}

['/content']
pub fn (mut app App) content() vweb.Result {
	content := $tmpl('dist/content/index.html')
	return app.html(content)
}

Will serve dist/index.html on localhost:8080. And dist/content/index.html on localhost:8080/content.

Let me know if this works for you @serge-hulne

ttytm avatar Jul 13 '23 07:07 ttytm