Ch-ch-changes!
First, I want to give a shout out to
@goto-engineering for fixing the response duration logging code!
@hammywhammy / @hweeks for all of the hard work on the docker/ci stuff!
- keyword routes
- default middleware function
- before/after functions
- a new, simpler starting template
keyword routes
Tired of having to define all of your routes in two different files, me too!
(route :get "/" :home)
(defn home [r])
This is how the new template works too:
(route :get "/todos" :todos/index)
(route :get "/todos/new" :todos/new)
(route :get "/todos/:id" :todos/show)
(route :post "/todos" :todos/create)
(route :get "/todos/:id/edit" :todos/edit)
(route :patch "/todos/:id" :todos/patch)
(route :delete "/todos/:id" :todos/delete)
You can imagine the corresponding functions.
default middleware function
Before:
(def routes (routes [:get "/" :home]))
(def app (as-> (handler routes) ?
(layout ? layout/app)
(csrf-token ?)
(session ?)
(extra-methods ?)
(query-string ?)
(body-parser ?)
(server-error ?)
(x-headers ?)
(static-files ?)
(not-found ?)
(logger ? )))
After:
(def routes (routes [:get "/" :home]))
(def app (app {:routes routes :layout layout-fn}))
You can also turn middleware on/off by changing the dictionary passed to app:
(app {:layout false :extra-methods false :session false :x-headers false :static-files false})
There are a few more options too like changing the cookie options for sessions, things like that.
before/after functions
Don't want to bother writing a whole middleware function just to append things to the request on certain routes? Me neither!
(before "/*" :run-before)
(defn run-before [req]
(put req :a 1))
You can use a combination of wildcard routes and the before function to modify the request dictionary before any matching routes. Make sure you return the request from your before functions.
Similarly, the after
function works the same way, except on the response, you also need to return the response as well.
new starter template
.
├── Procfile
├── main.janet
├── project.janet
└── public
├── app.css
└── app.js
5 files with a Procfile! One thing I didn't add to the default template is a Dockerfile which I'm kind of fine tuning still, it works great with dokku!
That's it for now, there are a few more new things, but you can find me online anywhere or consult the docs if you want to know more