Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 39eebc0

Browse files
authored
Merge pull request #10 from technosophos/feat/path_prefix
Add support for PATH_PREFIX
2 parents ec6aafd + 5941cef commit 39eebc0

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ SOFTWARE
138138

139139
The fileserver took `/static/filserver.gr`, removed the `/static/` part from the front, and then loaded `fileserver.gr` from the directory mounted in the `modules.toml`. Note that any subdirectories are also served. So `/static/foo/bar` would translate to the path `foo/bar` inside of the WebAssembly module (which in the example above would fully resolve to "/path/to/fileserver/foo/bar").
140140

141+
## Prefixing a Path
142+
143+
`PATH_PREFIX` is an environment variable you can set.
144+
This allows you to add `-e PATH_PREFIX=/some/prefix` as an env var to `fileserver.gr.wasm`.
145+
146+
This will allow the fileserver to set a specific path prefix for files before it looks them up. So instead of doing `http://example.com/static/static/foo.png`, you can set `wagi -e PATH_PREFIX=static/` and then `http://example.com/static/foo.png` will resolve on the filesystem, to `static/foo.png` instead of `foo.png`.
147+
141148
## Security Note
142149

143150
The Wagi fileserver is designed to serve any file mounted in the volume. Do not mount a

fileserver.gr

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ let rec pipe = (in, out) => {
4545
}
4646
}
4747

48-
let serve = abs_path => {
49-
// Trim the leading /
50-
let path = String.slice(1, String.length(abs_path), abs_path)
48+
let serve = (abs_path, env) => {
49+
// If PATH_PREFIX is set, then the path prefix is prepended onto the incoming path.
50+
// This allows you to map to a directory that does not match the directory name in the URL.
51+
let path = match (Map.get("PATH_PREFIX", env)) {
52+
Some(prefix) => {
53+
let tmp = String.slice(1, String.length(abs_path), abs_path)
54+
String.concat(prefix, tmp)
55+
},
56+
// If no env var, trim off just the leading /
57+
None => String.slice(1, String.length(abs_path), abs_path)
58+
}
5159
// Explicitly ignoring any Ok or Err that happens on this log
5260
// The `ignore` can be removed if you don't want to be explicit about this behavior
5361
ignore(File.fdWrite(File.stderr, "Fileserver: Loading file " ++ path ++ "\n"))
@@ -89,4 +97,4 @@ let guestpath = env => {
8997

9098
let kv = Env.envMap()
9199
let pathInfo = guestpath(kv)
92-
serve(pathInfo)
100+
serve(pathInfo, kv)

0 commit comments

Comments
 (0)