clerk icon indicating copy to clipboard operation
clerk copied to clipboard

BYOServer – clerk as middleware

Open den1k opened this issue 3 years ago • 5 comments

I'm looking into adding clerk to a prod instance and would like to place it under a specified route behind auth in my existing server setup (ring-compatible). Have you considered providing middleware/hooks instead of the standalone server to make this possible w/ clerk?

den1k avatar May 14 '22 18:05 den1k

actually, this might already be possible by putting

https://github.com/nextjournal/clerk/blob/main/src/nextjournal/clerk/webserver.clj#L60-L77

under a route. will try this

den1k avatar May 14 '22 18:05 den1k

okay, got everything to work besides the reload over the websocket. I believe the culprit lies in the hardcoded route here

https://github.com/nextjournal/clerk/blob/de7ffa0906a308552cba029bb094a3422161e926/src/nextjournal/clerk/view.clj#L45-L48

den1k avatar May 14 '22 19:05 den1k

I hardcoded my route into the above and got websockets to work as well. Looking at the impl of webserver it looks like the _ws prefix might be superfluous since there is a :websocket? value on the request. _blob, however, does have a place. To make an arbitrary starting route possible I wonder if _blob should be at the end of the path param, e.g. domain.com/<my/clerk/route>/noteboook_xyz/_blob so that one handler provided by clerk can manage all the routes. E.g. in reitit route syntax:

(def routes
  ["/admin/clerk/*"
   {:middleware [wrap-auth-redirect]
    :get        clerk.webserver/app}])

(reitit.core/match-by-path
  (reitit.core/router
    routes)
  "/admin/clerk/some_notebook/blob")
; => 
#reitit.core.Match{:template "/admin/clerk/*",
                   :data {:middleware [#object[yam.serve$wrap_auth_redirect
                                               0x6a909ba4
                                               "yam.serve$wrap_auth_redirect@6a909ba4"]],
                          :get #object[nextjournal.clerk.webserver$app
                                       0x10636c10
                                       "nextjournal.clerk.webserver$app@10636c10"]},
                   :result nil,
                   :path-params {: "some_notebook/blob"},
                   :path "/admin/clerk/some_notebook/blob"}

This way app receives all requests under "/admin/clerk/*" and retains control over any deeper path & query params.

den1k avatar May 16 '22 12:05 den1k

I'm looking into adding clerk to a prod instance and would like to place it under a specified route behind auth in my existing server setup (ring-compatible).

Nice, this is something we want as well so I'm certainly up for figuring out what knobs we need to provide to make this easier.

To make an arbitrary starting route possible I wonder if _blob should be at the end of the path param, e.g. domain.com/<my/clerk/route>/noteboook_xyz/_blob so that one handler provided by clerk can manage all the routes.

Wondering if it would be enough if we allowed to support an optional :path-prefix for Clerk urls?

mk avatar May 16 '22 13:05 mk

Great! It seems very straightforward unless I'm missing something.

Wondering if it would be enough if we allowed to support an optional :path-prefix for Clerk urls?

that should work as long as blobs are also prefixed with it, e.g. example.com/admin/clerk/_blob...

den1k avatar May 16 '22 14:05 den1k