#elixir-lang

/

      • reset joined the channel
      • eka joined the channel
      • davidw joined the channel
      • xtagon
        In my Rails app I use a plugin called Versionist to scope my API routes by API version in an Accept HTTP header. What's a good way to do this in Phoenix?
      • I don't want the API version to be in the URL. If the Accept header has the V1 format I want it to route to web/api/v1/foo_controller, etc.
      • Any ideas on how to set up that in a Plug or something?
      • Sriram3 has quit
      • asonge
        xtagon: with plug, you could copy the header into a param (if detected), and then pattern-match the header in the controller
      • xtagon
        asonge: Each API version will have a different controller
      • asonge
        that sounds kinda out there
      • i mean, it's doable, i think
      • meatherly joined the channel
      • tristanp has quit
      • xtagon: you sure that copies of controllers are the right abstraction layer for phoenix?
      • xtagon
        asonge: Actually, you made me think! With pattern matching it might make sense to abstract it differently
      • asonge
        that's what i was going to suggest
      • davidw has quit
      • you have different abstractions available...i assume in the rails case, you can extend a previous controller and use inheritence
      • but using defoverridable/super with a module here seems not quite as nice
      • xtagon
        I don't inherit previous major API versions though, at all, because then if I update the parent functionality it affects two major versions that should be completely separate for compatibility
      • meatherly has quit
      • If I make a change to some part of the code API v2 uses, I don't want it to affect API v1
      • dylan joined the channel
      • Another consideration is that the routes can be different from one API version to another, so I can't always just route them to the same controller I don't think
      • asonge
        ah, i see. anyway, using pattern-matching and short-circuiting on newer API versions should help
      • well, controllers aren't directly related to routes...but it's easy enough to put in multiple routes
      • xtagon
        I'll toy with that. But another reason for having a controller per API version is my controllers can have a lot of methods, and the file can get pretty big that way
      • Sorry, actions/functions.
      • dylan has quit
      • asonge: Is there no way to do arbitrary scope contstraints? e.g. something like: scope "/api", as: :v1, if: my_accept_header_test_func, do: ...
      • if the constraint function fails, it passes on the match and goes to the next matching route
      • asonge
        xtagon: you can stick guards into routes, i think. not sure though.
      • damjan
        how do I tell Plug.Static (or phoenix) to return priv/static/index.html for the "/" url
      • xtagon
        asonge: or maybe a pipe_through to a plug that skips the scope if the Accept header doesn't match?
      • asonge
        xtagon: you could do that
      • kronicdeth joined the channel
      • sotojuan has quit
      • xtagon
        asonge: I'm looking through the docs, what plug function would I use to say "Skip this, use the next matching scope/route instead"
      • I don't want to just halt the connection
      • kurtisnelson joined the channel
      • kronicdeth has quit
      • asonge
        hrm...you might be right...blah.
      • xtagon
        Basically I just want to define different routes for different HTTP headers (or any check on a Plug.Conn, really)
      • codestorm444 joined the channel
      • terakilobyte joined the channel
      • damjan
        xtagon: afaik you just return conn
      • kronicdeth joined the channel
      • xtagon
        damjan: Wouldn't that just tell it to keep using the routes in the first scope matched?
      • sotojuan joined the channel
      • damjan: If the Accept header isn't what I want, I want it to skip that scope (all routes in it) and continue through the router
      • damjan
        ah the whole scope. hmm
      • xtagon
        This is similar to scoping on subdomains too, which I also don't know how to do yet
      • damjan
        you can either nest scopes (and do something with that) OR ... does the Plug concept support internal redirects
      • asonge
        damjan: you can mutate the datastructure
      • erm, return a new datastructure
      • but the "redirect" won't make it restart from the tope
      • *from the top
      • tuelz joined the channel
      • damjan
        asonge: maybe xtagon can see what Phoenix does to call a scope, and do that?
      • codestorm444 has quit
      • so jump_on_certain_accept(other_scope)
      • programming is hard
      • :)
      • madnificent has quit
      • ventsi joined the channel
      • xtagon
        It's easy in Rails ;)
      • I think I just need to learn more about how the router works
      • asonge
        xtagon: if you want to retain routes between versions, don't you want multiple routers anyway?
      • you can put plugs in the endpoint to hit different routers
      • xtagon
        asonge: I want each API version to have separate route definitions, separate controllers
      • tuelz has quit
      • fcruxen joined the channel
      • asonge
        xtagon: ahhhh, you want to put a plug into the endpoint that will send it to a different router, then
      • xtagon
        Oh, that sounds good. Any specific docs I should read that would help with that?
      • asonge
        nothing you really need to know
      • just look at App.Endpoint
      • lmoura has quit
      • jeramyRR joined the channel
      • damjan
        Plug.Router has something 'forward'
      • forward(path, options)
      • Forwards requests to another Plug. The path_info of the forwarded connection will exclude the portion of the path specified in the call to forward
      • ventsi has quit
      • codestorm has quit
      • xtagon
        So I could do it so that each API version even gets its own router? That sounds good
      • dylan joined the channel
      • asonge
        xtagon: yes
      • btriley_ has quit
      • fcruxen has quit
      • jeramyRR
        good evening all
      • sp4rrow joined the channel
      • xtagon
        damjan: Okay I think I get how Plug.Router.forward works, kinda...but I am having trouble seeing how my router can forward to a different router based on the data in the conn.
      • jeramyRR: Good evening :-)
      • mr_bmt has quit
      • lmoura joined the channel
      • damjan
        argh the "only" filter in Plug.static is soooo crude
      • defp allowed?(_only, []), do: false
      • meh
      • dylan has quit
      • codestorm joined the channel
      • dignifiedquire has quit
      • jeramyRR has quit
      • davidw joined the channel
      • davidw has quit
      • davidw joined the channel
      • FIFOd joined the channel
      • benwilson512
        Context?
      • damjan
        benwilson512: I want to tell Plug.Static (or phoenix) to return priv/static/index.html for the "/" url
      • kronicdeth has quit
      • davidw has quit
      • benwilson512
        Are you trying to run a single page app or something?9
      • FIFOd has quit
      • damjan
        yes
      • btriley_ joined the channel
      • benwilson512
        I think most people doing that are hosting the spa code separate from Phoenix
      • damjan
        Plug.static.serve_static is a defp, so I can't call it, right?
      • asonge
        correct
      • sotojuan joined the channel
      • FIFOd joined the channel
      • benwilson512
      • jkreeftmeijer joined the channel
      • On mobile so hard to hell
      • btriley_ has quit
      • damjan
        ohh it's running 2 http servers
      • kronicdeth joined the channel
      • lmoura has quit
      • Phoenix.Router is not the same thing as Plug.Router is it?
      • asonge
        it isn't
      • jkreeftmeijer has quit
      • benwilson512
        Nope
      • Not sure what you mean by "it" btw
      • kronicdeth has quit
      • damjan
        benwilson512: in which sentence? :)
      • ericmj
        tristan__: pong
      • benwilson512
        9:29 PM <damjan> ohh it's running 2 http servers
      • damjan
        benwilson512: the blog post you mentioned
      • it's also running node (it seems)
      • codestorm444 joined the channel
      • so the Phoenix Router can't do: get "/", do: ... code block ?
      • meatherly joined the channel
      • sotojuan
        anyone write spooky elixir today?
      • benwilson512
        sotojuan: I used Code.eval_string...
      • damjan: or text
      • *corrext
      • tuelz joined the channel
      • And yea the general thing with SPA is to put the static assets on a CDN and hit a JSON API
      • codestorm444 has quit
      • damjan
        benwilson512: this is app is so far from it :)
      • I'll ever run it on my laptop, max on my desktop
      • lnap joined the channel
      • benwilson512
        Gotcha