Fork me on GitHub
#pedestal
<
2022-02-03
>
lucian30319:02:54

how can I make a wildcard route to only serve routes that are not explicitly defined in my routes? i thought this was working before, but there is an issue as sometimes the wildcard route serves the defined routes also: my routes:

(def routes #{["/" :get (conj html-interceptors `home-page)]

              ["/*all" :get (conj html-interceptors `all-page)]
              ["/api/config" :get (conj open-api-interceptors `config)]})
and in my service config, i have added:
:io.pedestal.http/router :linear-search
I've also tried to switch the order of the routes, putting the wildcard last, without luck. But the routes are defined as a set, which means they are unordered. Using a sorted set leads to errors. I think the lack of order of the set is likely what is leading to the issues but i'm not sure how to fix it. Seems to me the linear router exists, but it can't be used since the order of routes simply cannot be defined. If that's not the case, how can the order of the routes be defined? So reading up on route syntax, it seems this affects the table syntax because it uses a set. Is there a way to use table syntax without a set? Maybe the expanded syntax is the only way to do this?

lucian30323:02:02

For now, I'm using this to override the protocol and process vectors using table syntax but a config option would probably be best:

;; Extend protocol and process routes in vectors using table syntax
(extend-protocol route/ExpandableRoutes
  clojure.lang.APersistentVector
  (-expand-routes [route-spec]
    (table/table-routes route-spec)))

hanDerPeder06:02:21

With linear-search you should use a vector of routes, not a set.

hanDerPeder06:02:35

Also, if you want a handler for any non matched route you could use an interceptor after your router.

lucian30322:02:57

is it possible to use a set w/ the tabular format? it seems that a vector is interpreted w/ the terse format, that's why i overrode that protocol to interpret vectors using the table format. is there a better way to do that?