-
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting up routes for static content #36
Comments
You could use static middleware (setf *app*
(lack:builder
:session
(:static :path "/public/"
:root #P"/static-files/")
(lambda (app)
(lambda (env)
(prog1 (funcall app env)
(do-before-responding))))
*app*)) |
@knobo I am afraid this is not working with my setup, I am actually using ningle instance to define routes, this is my (ql:quickload '(:ningle :djula :lack))
(djula:add-template-directory #P"templates/")
(defparameter *template-registry* (make-hash-table :test 'equal))
;; render template - copied & modified from caveman
(defun render (template-path &optional data)
(let ((html (make-string-output-stream))
(template (gethash template-path *template-registry*)))
(unless template
(setf template (djula:compile-template* (princ-to-string template-path)))
(setf (gethash template-path *template-registry*) template))
(apply #'djula:render-template* template html data)
`(200 (:content-type "text/html")
(,(format nil "~a" (get-output-stream-string html))))))
(defvar *app* (make-instance 'ningle:app))
(setf (ningle:route *app* "/") (render #P"index.html"))
(setf (ningle:route *app* "/about") (render #P"about.html"))
(setf *app*
(lack:builder
:session
(:static :path "/"
:root #P"/static/")
(lambda (app)
(lambda (env)
(prog1 (funcall app env)
;; (do-before-responding)
)))
*app*))
*app*
The route definitions are overridden when I setf app with lack builder |
I guess #p"/static/" does not exist. try relative path instead. |
This is the latest code (ql:quickload '(:ningle :djula :lack))
(djula:add-template-directory #P"templates/")
(defparameter *template-registry* (make-hash-table :test 'equal))
;; render template - copied & modified from caveman
(defun render (template-path &optional data)
(let ((template (gethash template-path *template-registry*)))
(unless template
(setf template (djula:compile-template* (princ-to-string template-path)))
(setf (gethash template-path *template-registry*) template))
(apply #'djula:render-template* template nil data)))
(defvar *app* (make-instance 'ningle:app))
(setf (ningle:route *app* "/") (render #P"index.html"))
(setf *app* (lack:builder
:session
(:static :path "/"
:root #P"static/")
(lambda (app)
(lambda (env)
(funcall app env)))
*app*))
*app* Am I missing something? |
I don't know. It's several years since I used this. But here is some code from one of my projects. (defparameter *application-root* (asdf:system-source-directory :myapp)
(defparameter *static-directory* (merge-pathnames #P"static/" *application-root*)
;;;
;;;
;;; ....
(:static
:path (lambda (path)
(if (ppcre:scan "^(?:/images/|/css/|/js/|/robot\\.txt$|/favicon\\.ico$)" path)
path
nil))
:root *static-directory*) |
You don't have to set the app at the end. (ql:quickload '(:ningle :djula :lack))
(djula:add-template-directory #P"templates/")
(defparameter *template-registry* (make-hash-table :test 'equal))
;; render template - copied & modified from caveman
(defun render (template-path &optional data)
(let ((template (gethash template-path *template-registry*)))
(unless template
(setf template (djula:compile-template* (princ-to-string template-path)))
(setf (gethash template-path *template-registry*) template))
(apply #'djula:render-template* template nil data)))
(defvar *app* (make-instance 'ningle:app))
(setf (ningle:route *app* "/") (render #P"index.html"))
(lack:builder
:session
(:static :path "/pub/"
:root #P"static/")
:
(lambda (app)
(lambda (env)
(funcall app env)))
*app*)
|
Super Awesome, @knobo Thanks a lot, that worked. Really appreciate the help. |
hi @knobo , I don't mean to bring up an old thread but I've tried this example and it doesn't seem to work for me now. Did somethign change? I can't seem to get static files working at all with ningle. |
How can I serve static content with ningle?
I have a
static
folder under my project root and keep the relevant static files in sub folders likecss
,js
,images
, etc.,And I want to access them like:
The text was updated successfully, but these errors were encountered: