create webdav -> parent_vfs -> ourdb_vfs example
This commit is contained in:
31
examples/vfs/example.vsh
Normal file
31
examples/vfs/example.vsh
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env -S v -n -w -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.vfs.webdav
|
||||
import freeflowuniverse.herolib.vfs.vfsnested
|
||||
import freeflowuniverse.herolib.vfs.ourdb_fs
|
||||
|
||||
high_level_vfs := vfsnested.new()
|
||||
|
||||
// lower level VFS Implementations that use OurDB
|
||||
mut vfs1 := ourdb_fs.new(
|
||||
data_dir: '/tmp/test_webdav_ourdbvfs/vfs1'
|
||||
metadata_dir: '/tmp/test_webdav_ourdbvfs/vfs1'
|
||||
)!
|
||||
mut vfs2 := ourdb_fs.new(
|
||||
data_dir: '/tmp/test_webdav_ourdbvfs/vfs2'
|
||||
metadata_dir: '/tmp/test_webdav_ourdbvfs/vfs2'
|
||||
)!
|
||||
mut vfs3 := ourdb_fs.new(
|
||||
data_dir: '/tmp/test_webdav_ourdbvfs/vfs3'
|
||||
metadata_dir: '/tmp/test_webdav_ourdbvfs/vfs3'
|
||||
)!
|
||||
|
||||
// Nest OurDB VFS instances at different paths
|
||||
high_level_vfs.add_vfs('/data', vfs1) or { panic(err) }
|
||||
high_level_vfs.add_vfs('/config', vfs2) or { panic(err) }
|
||||
high_level_vfs.add_vfs('/data/backup', vfs3) or { panic(err) } // Nested under /data
|
||||
|
||||
// Create WebDAV Server that uses high level VFS
|
||||
webdav_server := webdav.new_app(
|
||||
vfs: high_level_vfs
|
||||
)
|
||||
@@ -3,6 +3,7 @@ module webdav
|
||||
import vweb
|
||||
import freeflowuniverse.herolib.core.pathlib
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import freeflowuniverse.herolib.vfs.vfscore
|
||||
|
||||
@[heap]
|
||||
struct App {
|
||||
@@ -11,6 +12,7 @@ struct App {
|
||||
root_dir pathlib.Path @[vweb_global]
|
||||
pub mut:
|
||||
// lock_manager LockManager
|
||||
vfs VFSImplementation
|
||||
server_port int
|
||||
middlewares map[string][]vweb.Middleware
|
||||
}
|
||||
@@ -21,6 +23,7 @@ pub mut:
|
||||
server_port int = 8080
|
||||
root_dir string @[required]
|
||||
user_db map[string]string @[required]
|
||||
vfs VFSImplementation
|
||||
}
|
||||
|
||||
pub fn new_app(args AppArgs) !&App {
|
||||
@@ -29,6 +32,7 @@ pub fn new_app(args AppArgs) !&App {
|
||||
user_db: args.user_db.clone()
|
||||
root_dir: root_dir
|
||||
server_port: args.server_port
|
||||
vfs: args.vfs
|
||||
}
|
||||
|
||||
app.middlewares['/'] << logging_middleware
|
||||
|
||||
33
lib/vfs/webdav/methods_vfs.v
Normal file
33
lib/vfs/webdav/methods_vfs.v
Normal file
@@ -0,0 +1,33 @@
|
||||
module webdav
|
||||
|
||||
import vweb
|
||||
import os
|
||||
import freeflowuniverse.herolib.core.pathlib
|
||||
import encoding.xml
|
||||
import freeflowuniverse.herolib.ui.console
|
||||
import net.urllib
|
||||
|
||||
@['/:path...'; get]
|
||||
fn (mut app App) get_file(path string) vweb.Result {
|
||||
if !app.vfs.exists(path) {
|
||||
return app.not_found()
|
||||
}
|
||||
|
||||
fs_entry := app.vfs.get(path) or {
|
||||
console.print_stderr('failed to get FS Entry ${path}: ${err}')
|
||||
return app.server_error()
|
||||
}
|
||||
|
||||
file_data := app.vfs.file_read(fs_entry.path)
|
||||
|
||||
ext := fs_entry.get_metadata().name.all_after_last('.')
|
||||
content_type := if v := vweb.mime_types[ext] {
|
||||
v
|
||||
} else {
|
||||
'text/plain'
|
||||
}
|
||||
|
||||
app.set_status(200, 'Ok')
|
||||
app.send_response_to_client(content_type, file_data)
|
||||
return vweb.not_found() // this is for returning a dummy result
|
||||
}
|
||||
Reference in New Issue
Block a user