reblocks-file-server
2024-10-12
A Reblocks extension allowing to create routes for serving static files from disk.
reblocks-file-server - A Reblocks extension allowing to create routes for serving static files from disk.
REBLOCKS-FILE-SERVER ASDF System Details
- Description: A Reblocks extension allowing to create routes for serving static files from disk.
- Licence: Unlicense
- Author: Alexander Artemenko [email protected]
- Homepage: https://40ants.com/reblocks-file-server/
- Bug tracker: https://github.com/40ants/reblocks-file-server/issues
- Source control: GIT
- Depends on: cl-fad, cl-ppcre, log4cl, reblocks, routes, trivial-mimes
Installation
You can install this library from Quicklisp, but you want to receive updates quickly, then install it from Ultralisp.org:
(ql-dist:install-dist "http://dist.ultralisp.org/"
:prompt nil)
(ql:quickload :reblocks-file-server)
Usage
Here is a few examples on how this library can be used. These lines can be added into the code
which starts your Reblocks web application inside the initialize-instance
method of your app:
(defmethod initialize-instance ((app app) &rest args) (declare (ignorable args)) (reblocks-file-server:make-route :root (asdf:system-relative-pathname "ultralisp" "images/") :uri "/images/") (call-next-method))
This is how to serve all *.txt
files from the /var/www
folder:
(reblocks-file-server:make-route :uri "/static/" :root "/var/www/" :dir-listing nil :filter ".*.txt")
You also can provide a DIR-LISTING
argument to repond on /static/ route with a rendered directory listing:
(reblocks-file-server:make-route :uri "/static/" :root "/var/www/" :dir-listing t :filter ".*.txt")
In case if you want to serve all files except *.txt
, you can negate filter expression by giving NIL
in FILTER-TYPE
argument:
(reblocks-file-server:make-route :uri "/static/" :root "/var/www/" :dir-listing t :filter ".*.txt" :filter-type nil)
API
REBLOCKS-FILE-SERVER/CORE
package reblocks-file-server/core
Classes
STATIC-FILES-ROUTE
class reblocks-file-server/core:static-files-route
(route)
Readers
reader reblocks-file-server/core:get-dir-listing
(static-files-route) (:dir-listing = t)
When nil, directory contents is not shown.
reader reblocks-file-server/core:get-filter
(static-files-route) (:filter)
A regular expression.
reader reblocks-file-server/core:get-filter-type
(static-files-route) (:filter-type = t)
T means show files that match the filter regexp. NIL
means hide files that match the filter regexp
reader reblocks-file-server/core:get-root
(static-files-route) (:root)
reader reblocks-file-server/core:get-uri
(static-files-route) (:uri)
Generics
generic-function reblocks-file-server/core:render-404
route uri
Returns a string with HTML
for a case when `uri' wasn't found on the disk.
generic-function reblocks-file-server/core:render-directory
route uri children
Renders a list of files in a directory
generic-function reblocks-file-server/core:render-styles
route
This method should use reblocks/html:with-html and output a :style element.
generic-function reblocks-file-server/core:serve-directory
route uri full-path
Returns a Lack response with a rendered directory listing.
generic-function reblocks-file-server/core:serve-file
route full-path
Returns content of the file.
Functions
function reblocks-file-server/core:list-directory
full-path filter filter-type
Returns a list of files in the directory. All items of the list are relative.