Dragonfly create routes | Dragonfly web framework

Creating Routes

There are typically three steps necessary to create a route:

  1. Create a .lsp file in the plugins-active directory
  2. Make a FOOP subclass of the Route class implementing its methods
  3. Add an instance to the DF:dragonfly-routes list

A route has two methods: matches? and run.

matches? - should return non-nil if the route decides, based on environment variables or anything else, whether it wants to handle this request.

run - does anything it needs to do and sends a response back to the client.

SET_DF_SELF

Most routes should also call the function SET_DF_SELF prior to actually loading or serving the requested URL. This function takes a single argument: the actual path (on the server) to whatever file is being served.

Based on that parameter, the function will then set the global symbols DF_SELF and DF_SELF_DIR, which let whatever is being loaded know where it is located on the server.

IMPORTANT
Do not confuse DF_SELF with PHP's $_SERVER['PHP_SELF'] (which is DF_PAGE in Dragonfly).

Example: Route.CGI

In addition to the two default routes Route.Static and Route.Resource, Dragonfly includes a route called Route.CGI, although by default it's disabled because it's in the plugins-inactive folder:

;; File: plugins-inactive/route_cgi.lsp

(new Route 'Route.CGI)
(context Route.CGI)

(constant 'CGI_EXTENSION ".nl")

(define (matches?)
    (setf path (1 DF_PAGE))
    (and (ends-with path CGI_EXTENSION) (file? path))
)
(define (run)
    (SET_DF_SELF path)
    (load path)
    (DF:send-and-exit)
)

; add the route to the end of the list of routes
(push (Route.CGI) DF:dragonfly-routes -1)

(context MAIN)

This route is very useful if you simply want to run some newLISP code without having to deal with HTML templates. It simply loads a newLISP source file ending in CGI_EXTENSION, essentially behaving like a CGI script.

The script then does whatever it needs to do, and it can send output back to the browser using the print and println functions.

CONFIGURATION
If you change CGI_EXTENSION, check newlisp-redirection.lsp for compatibility when running the newlispServer script.
More information: Working Locally

CONTINUE »

Rendered in 0 milliseconds. Used 83 KB of memory, 58 KB for Lisp Cells.