TrailingFormatPlug

HomePage | RecentChanges | Preferences

Trailing Format Plug

See discussion on https://github.com/phoenixframework/phoenix/issues/1132

And commit for more changes to how the format is specified https://github.com/phoenixframework/phoenix/commit/290ec590b59334b6676ea426870a0b6ad68f17ea

And my branch with the patch for Phoenix 0.15: https://github.com/mschae/trailing_format_plug/compare/master...wsmoak:phoenix_0_15_update

#elixir-lang 2015-08-02

8:16 PM <wsmoak> do the usage instructions here look correct?  https://github.com/mschae/trailing_format_plug
8:17 PM <wsmoak> I've added the :before pipeline to a generated app, but it complains about the plug :super line
8:18 PM <wsmoak> and it's not going through the :before pipeline at all that I can tell.  (but if I add it explicitly, I get an error saying it's always piped through. :/ )

8:22 PM <wsmoak> router is https://github.com/wsmoak/my_app_802337/blob/master/web/router.ex
8:22 PM <wsmoak> and the log of the request + error https://gist.github.com/wsmoak/de41ef5a710450d07170

8:33 PM <lance_halvorsen> wsmoak: that's no longer current usage. i believe it's from a time before we had endpoints
8:34 PM <lance_halvorsen> also note that we no longer do this in routers: use Phoenix.Router

8:34 PM <wsmoak> I put `plug TrailingFormatPlug` directly in api scope and it's not doing anything.
8:35 PM <wsmoak> (I did not do `use Phoenix.Router`, that didn't look familiar.)
8:35 PM <wsmoak> oops, s/api scope/the api pipeline/

8:36 PM <wsmoak> so, two mysteries, :before is never listed in the debug log so I'm not sure it _is_ being piped through
8:37 PM <lance_halvorsen> ah, ic, i wasn't very clear. the :before pipeline is the archaic bit.

8:38 PM <wsmoak> and then, even with this plug _in_ the api pipeline, it's not modifying the path & format 


8:39 PM <lance_halvorsen> wsmoak: i don't know enough about the project to offer advice on that. i would suggest opening an issue.

8:40 PM <wsmoak> lance_halvorsen: interesting, because when I explicitly say `pipe_through :before`, it won't compile and insists "(ArgumentError) the :before pipeline is always piped through"

8:51 PM <chrismccord> wsmoak: adding it to your pipeline should work
8:51 PM <chrismccord> we don't have a :before pipeline any longer, but adding it to the top of your applicable pipeline will work

8:52 PM <wsmoak> chrismccord: I've got it directly in the :api pipeline, and it's not doing anything.  https://github.com/wsmoak/my_app_802337/blob/master/web/router.ex
8:52 PM <wsmoak> the app is still getting "1.json" as the `id`, it's not setting the format or stripping the extension.

8:53 PM <wsmoak> I have not yet found how to point to a locally built version of a dependency, any hints ?

8:54 PM <chrismccord> wsmoak: can you show me what your logs look like?

8:54 PM <chrismccord> when you visit say, /tasks.json

8:55 PM <chrismccord> wsmoak: ahhh I see

8:55 PM <chrismccord> wsmoak: yeah it will never match. You'll have to plug it in your Endpoint instead
8:56 PM <chrismccord> pipelines are only invoked when a route matches, and "/tasks.json" won't match properly to have the trailing format plug ever invoked

8:57 PM <wsmoak> hm?  I'm getting into the api scope aren't I?  `GET     /api/tasks/:id`  -- and that has pipe_through :api, which has that plug.
8:58 PM <chrismccord> wsmoak: ah you are right. Sorry for the runaround

9:03 PM <wsmoak> If I want to add some debug logging to the plug, how do I point to a local version I've built? So far I only see how to point to things by version number (from hex) or on github.

9:04 PM <chrismccord> wsmoak: {:trailing_format_plug, path: "../path/to"}
9:05 PM <chrismccord> wsmoak: so it looks like a bug. I tried it and it sets the format, but the id params is wrong, which I think you are seeing
9:05 PM <chrismccord> a bug in the plug that is

9:09 PM <wsmoak> thanks!  odd, though, isn't there a test for exactly that?
9:09 PM <chrismccord> wsmoak: yes. So it's confusing. Placing it in your endpoint will make thing work properly

9:13 PM <chrismccord> wsmoak: what's happening is, the :before pipeline used to be like our Endpoint (before endpoints), it was a stack of plugs that are called before the route match
9:13 PM <chrismccord> wsmoak: what's happening now is the trailing format plug is called _after_ route match. And this is where params, like :id, are plucked from the path

9:15 PM <wsmoak> so it does! (work in the endpoint that is.) thank you chrismccord, blog posting schedule rescued.
9:15 PM <chrismccord> wsmoak: does the reasons make sense?

9:16 PM <chrismccord> wsmoak: it could be made to work in your router pipelines by having it rewrite the id params

9:18 PM <wsmoak> it... sounds reasonable.  I need to work with it and explore some things before it will sink in, like maybe try to change the plug as you described.  thanks again for your help chrismccord .

9:21 PM <chrismccord> wsmoak: throw this in the top of your router to hack with it https://gist.github.com/chrismccord/675ac3877d4df0d1e2ba

9:21 PM <chrismccord> wsmoak: should make it work for use within pipelines. The only caveat is you'd need to let it know the id param if it's not "id"
9:22 PM <chrismccord> so like `plug TrailingFormat, id: "slug"` or whatever
9:22 PM <chrismccord> err, `plug TrailingFormat, param: "slug"`

9:23 PM <chrismccord> wsmoak: it's much better this way because if we can keep it out of hte endpoint, we won't execute it on every request. So feel free to package that up as PR

9:29 PM <wsmoak> I didn't think of just pasting it into my project, much easier. :)

9:31 PM <chrismccord> wsmoak: you can also hack the `deps/tailing_format_plug` directly when you are in a pinch, but you'll need to mix deps.compile trailing_format_plug && mix phoenix.server` between changes


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited August 17, 2015 4:59 pm by 198.98.94.133 (diff)
Search: