Fork me on GitHub
#reitit
<
2024-04-08
>
vemv11:04:41

Is there a cool+up-to-date template around using reitit, sieppari, malli coercion, and then some more?

Ben Sless12:04:02

I usually just copy the example from the examples directory

vemv12:04:31

Yeah I have a few candidates to copy from, but probably I could borrow some extra stuff for extra production-gradeness (?)

Ben Sless13:04:57

I always had a bad time writing templates because of text manipulation. Got anything that can take a project and derive a template from it?

vemv13:04:29

That would be cool! But nope

Samuel Ludwig17:04:31

continuing my journey of practicing reitit/building rest-ish APIs; is there a prescribed way for returning reference-URIs for resources? I might be using the wrong terminology, so let me use a specific example: suppose I have a resource as part of a personnel management API, in which each employee may have a manager, which is another employee, so: given the request "GET https://<root-domain>/api/employees/277" (defined in a reitit.ring/router), I would like to return information like so

{
  "email": "[email protected]",
  "name": "john smith",
  "manager-id": 16,
  "manager": "https:///api/employees/16"
}
suppose my handler for that resource is written as
(fn [request] 
  {:status 200 
   :body (employee/get-by-id (-> request :parameters :path :id)))
which returns a map of the name, email, and manager-id what should I assoc to that map to get the manager reference I'm hoping for? I could of course 'manually' add the string, but that would break should the ever change, I figure it might have something to do with passing through the :reitit.core/router, but I've only been futzing around in the dark with it so far.

Samuel Ludwig20:04:21

okay, so I see I can get the path relative to the API root via (-> request :ring.core/router (reitit.core/match-by-name ::employee {:id 16}) :path), I guess I can try and derive the active host-name by some other way and prepend it~

DrLjótsson13:04:40

That is the way I would do it!

❤️ 1
Samuel Ludwig14:04:08

could i ask what your go-to method is for reliably getting the domain-name/scheme+authority of the request? (the http(s)://)

DrLjótsson10:04:08

In production, my app is behind an apache reverse-proxy. So I first check if there is a x-forwarded-for header, and then check the host header

(defn get-host
  [request]
  (let [headers (:headers request)]
    (get headers "x-forwarded-host" (get headers "host"))))

DrLjótsson10:04:15

I think that the scheme is found in (:scheme request), note that it contains a keyword, :http or :https

DrLjótsson10:04:42

I suppose this would be more idiomatic

(defn get-host
  [request]
  (let [headers (:headers request)]
    (or (get headers "x-forwarded-host")
        (get headers "host"))))

Samuel Ludwig13:04:14

ahh perfect! i appreciate it ❤️

❤️ 1