reitit

vemv 2024-04-08T11:43:41.052099Z

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

Ben Sless 2024-04-08T12:46:02.780359Z

I usually just copy the example from the examples directory

vemv 2024-04-08T12:56:31.249849Z

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

Ben Sless 2024-04-08T13:06:57.330409Z

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?

vemv 2024-04-08T13:08:29.070559Z

That would be cool! But nope

Samuel Ludwig 2024-04-08T17:39:31.713219Z

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": "employee277@gmail.com",
  "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.

DrLjótsson 2024-04-09T13:53:40.533039Z

That is the way I would do it!

❤️ 1
Samuel Ludwig 2024-04-09T14:50:08.471919Z

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

DrLjótsson 2024-04-10T10:33:08.948289Z

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ótsson 2024-04-10T10:35:15.840199Z

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

DrLjótsson 2024-04-10T10:36:42.825449Z

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 Ludwig 2024-04-10T13:37:14.933509Z

ahh perfect! i appreciate it ❤️

❤️ 1
Samuel Ludwig 2024-04-08T20:02:21.241329Z

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~