by rrevi · 4y ago · 0 comments
If you don’t mind me asking, why did you choose the Crystal programming language for this project?
In working on this project, I discovered two "superpowers" of Crystal:
The first was macros https://crystal-lang.org/reference/1.5/syntax_and_semantics/... which I used sparingly, but to great effect, when an ordinary method call wouldn't do.
The second was monkeypatching: specifically, it's super easy to reopen existing standard library classes and add a field I need, for example:
# BEGIN MONKEYPATCH
class HTTP::Server::Context
property response_body_cache_key : String? = nil
end
# END MONKEYPATCH
This adds a new field the standard library HTTP::Server::Context https://github.com/crystal-lang/crystal/blob/1.5.0/src/http/... / https://crystal-lang.org/api/1.5.0/HTTP/Server/Context.html so I can specify a cache key near the beginning of a request handler, and later my response middleware picks it up to store the response body.