How to redirect in webmachine
Recently I was looking for an example on how to do redirect in webmachine. Unfortunately I haven’t found one. So I started figuring it out by myself. After try and error using brilliant wmtrace_resource It turned out to be trivial ;). Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | %% @doc Example of redirect webmachine_resource. -module(redirect_resource). -export([init/1, resource_exists/2, moved_temporarily/2, previously_existed/2]). -include_lib("webmachine/include/webmachine.hrl"). init([]) -> {ok, undefined}. moved_temporarily(ReqData, Context) -> Site = wrq:path_info(site, ReqData), Location = base64:decode(Site), {{true, Location}, ReqData, Context}. previously_existed(ReqData, Context) -> {true, ReqData, Context}. resource_exists(ReqData, Context) -> {false, ReqData, Context}. |
The resource is mapped in my dispatch.conf as
{["redirect", site], redirect_resource, []}.
You can do a request like http://host.com/redirect/aHR0cDovL2xhbWJkZXIuY29t
The redirect_resource will interpret the last token in the path as base64 encoded location to redirect to.
Rock on. Awesome to hear that wmtrace helped you step through the logic to reach this excellent, concise resource.
Have you announced what you’re building with Webmachine yet? I’m anxious to see.
Pretty cool post. I just found your site and wanted to say
that I’ve really liked browsing your blog posts. Any way
I’ll be subscribing to your feed and I hope you post again soon!