diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-15 14:51:48 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-15 14:53:43 +0700 |
| commit | 713d51e4792fb873d67d442553c0967e7fa0dc56 (patch) | |
| tree | 8d06441dd302c253bda3c2cac9a9e3f6694bead9 /lib/http/fs_handler.go | |
| parent | 625722e9087173d7b01acfb786de935fe09f4310 (diff) | |
| download | pakakeh.go-713d51e4792fb873d67d442553c0967e7fa0dc56.tar.xz | |
lib/http: refactoring FSHandler type to return [*memfs.Node]
Changing FSHandler type to return [*memfs.Node], allow the handler to
redirect or return custom node.
One of the use case is when service Single Page Application (SPA), where
route is handled by JavaScript.
For example, when user requested "/dashboard" but dashboard directory
does not exist, one can write the following handler to return
"/index.html",
{
node, _ = memfs.Get(`/index.html`)
return node
}
Diffstat (limited to 'lib/http/fs_handler.go')
| -rw-r--r-- | lib/http/fs_handler.go | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/lib/http/fs_handler.go b/lib/http/fs_handler.go index 06d470f1..b88c13b4 100644 --- a/lib/http/fs_handler.go +++ b/lib/http/fs_handler.go @@ -12,12 +12,14 @@ import ( // FSHandler define the function to inspect each GET request to Server // [memfs.MemFS] instance. -// The node parameter contains the requested file inside the memfs. +// The node parameter contains the requested file inside the memfs or nil +// if the file does not exist. // -// If the handler return true, server will continue processing the node -// (writing the [memfs.Node] content type, body, and so on). +// If the handler return non-nil [*memfs.Node], server will continue +// processing the node, writing the [memfs.Node] content type, body, and so +// on. // -// If the handler return false, server stop processing the node and return +// If the handler return nil, server stop processing the node and return // immediately, which means the function should have already handle writing // the header, status code, and/or body. -type FSHandler func(node *memfs.Node, res http.ResponseWriter, req *http.Request) bool +type FSHandler func(node *memfs.Node, res http.ResponseWriter, req *http.Request) (out *memfs.Node) |
