bddcab77f714c8d7f80a75069ce58faab23e7644
[odin-cgi] / mason / shorturl / dhandler
1 <&| SELF:wrapper, title => "odin.gg URL Shortener" &>
2 <h1><tt>odin.gg</tt> URL Shortener</h1>
3 <form method="POST" accept-charset="UTF-8" enctype="multipart/form-data">
4 <label for="url" accesskey="u"><u>U</u>RL to shorten:</label>
5 <input id="url" name="u" value="<% $u // "" |h %>">
6 <button type="submit">Go</button>
7 </form>
8 % if (defined $tag) {
9 <p>Shortened to: <a href="<% "$Odin::SHORTURL/$tag" %>"><%
10 "$Odin::SHORTURL/$tag" %></a>
11 % }
12 </&>
13 %#
14 <%init>
15 my $tag = $m->dhandler_arg;
16 if (length $tag) {
17 my $url = Odin::get_shorturl $tag;
18 if ($q) { $m->comp(".query", url => $url); }
19 else { $m->redirect($url, 301); }
20 return;
21 } elsif (defined $u) {
22 $tag = Odin::new_shorturl $u;
23 } else {
24 Odin::path_info($r) =~ m:/$:
25 or $m->redirect("$Odin::SHORTURL/", 301);
26 $tag = undef;
27 }
28 </%init>
29 %#
30 <%args>
31 $q => undef
32 $u => undef
33 </%args>
34 %#
35 <%def .query>\
36 % $r->content_type("text/plain; charset=utf8");
37 <% $url %>
38 <%args>
39 $url
40 </%args>
41 </%def>
42 %#
43 <%def .notfound>\
44 <&| SELF:error, title => "not found", status => 404 &>\
45 tag &lsquo;<% $tag |h %>&rsquo; not found
46 </&>
47 <%args>
48 $tag
49 </%args>
50 </%def>
51 %#
52 <%def .badurl>\
53 <&| SELF:error, title => "invalid url", status => 404 &>\
54 &lsquo;<tt><% $u |h %></tt>&rsquo; is not a valid URL
55 </&>
56 <%args>
57 $u
58 </%args>
59 </%def>
60 %#
61 <%once>
62 use utf8;
63 use Odin;
64 </%once>