Commit 70cf6b26 authored by Luc Didry's avatar Luc Didry

Update API documentation. Fix #20

+ add JSON response to some point of API
parent 57c2175d
Pipeline #11216 passed with stages
in 5 minutes and 37 seconds
......@@ -19,6 +19,10 @@ podcheck:
test: podcheck
$(CARTON) $(REAL_LSTU) test
test-sqlite:
MOJO_CONFIG=t/sqlite1.conf $(CARTON) $(REAL_LSTU) test
MOJO_CONFIG=t/sqlite2.conf $(CARTON) $(REAL_LSTU) test
dev:
$(CARTON) morbo $(LSTU) --listen http://0.0.0.0:3000 --watch lib/ --watch script/ --watch themes/ --watch lstu.conf
......
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lstu;
use Mojo::Base 'Mojolicious';
use Mojo::JSON;
use Net::LDAP;
use Apache::Htpasswd;
use Lstu::DB::URL;
......@@ -227,10 +228,37 @@ sub startup {
my $pwd = $c->param('password');
if($c->authenticate($login, $pwd)) {
$c->redirect_to('index');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged in.')
}
);
},
any => sub {
$c->redirect_to('index');
}
);
} else {
$c->stash(msg => $c->l('Please, check your credentials: unable to authenticate.'));
$c->render(template => 'login');
my $msg = $c->l('Please, check your credentials: unable to authenticate.');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->false,
msg => $msg
}
);
},
any => sub {
$c->stash(msg => $msg);
$c->render(template => 'login')
}
);
}
});
# Logout page
......@@ -239,7 +267,20 @@ sub startup {
if ($c->is_user_authenticated) {
$c->logout;
}
$c->render(template => 'logout');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged out.')
}
);
},
any => sub {
$c->render(template => 'logout');
}
);
})->name('logout');
}
......
......@@ -43,22 +43,64 @@ sub login {
)->write;
$c->session('token' => $token);
$c->redirect_to('stats');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged in.')
}
);
},
any => sub {
my $c = shift;
$c->redirect_to('stats');
}
);
} elsif (defined($act) && $act eq 'logout') {
Lstu::DB::Session->new(
app => $c,
token => $c->session('token')
)->delete;
delete $c->session->{token};
$c->redirect_to('stats');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->true,
msg => $c->l('You have been successfully logged out.')
}
);
},
any => sub {
shift->redirect_to('stats');
}
);
} else {
Lstu::DB::Ban->new(
app => $c,
ip => $ip
)->increment_ban_delay(3600);
$c->flash('msg' => $c->l('Bad password'));
$c->redirect_to('stats');
my $msg = $c->l('Bad password');
$c->respond_to(
json => sub {
my $c = shift;
$c->render(
json => {
success => Mojo::JSON->false,
msg => $msg
}
);
},
any => sub {
my $c = shift;
$c->flash('msg' => $msg);
$c->redirect_to('stats');
}
);
}
}
}
......@@ -97,7 +139,7 @@ sub delete {
);
}
} else {
$c->flash('msg' => $c->l('Bad password'));
$c->flash('msg' => $c->l('You\'re not authenticated as the admin'));
$c->redirect_to('stats');
}
}
......
......@@ -79,7 +79,7 @@ sub _provisioning {
for (my $i = 0; $i < $c->config('provis_step'); $i++) {
my $short;
do {
$short= $c->shortener($c->config('length'));
$short = $c->shortener($c->config('length'));
} while ($db_url->exist($short));
$db_url->short($short)->write;
......
......@@ -86,7 +86,7 @@ $b =~ s#http://127\.0\.0\.1:\d+/##;
$t->ua->max_redirects(1);
$t->get_ok('/d/'.$b)
->status_is(200)
->content_like(qr/Bad password/);
->content_like(qr/You&#39;re not authenticated as the admin/);
$t->post_ok('/stats' => form => { adminpwd => 'toto', page => 0 })
->status_is(200)
......
......@@ -24,6 +24,10 @@ msgstr ""
msgid "%1 is not a valid URL."
msgstr ""
#: themes/default/templates/api.html.ep:103
msgid "A page with a table containing the same informations that the JSON response"
msgstr ""
#: themes/default/templates/layouts/default.html.ep:57
msgid "About"
msgstr ""
......@@ -32,7 +36,7 @@ msgstr ""
msgid "Admin password"
msgstr ""
#: lib/Lstu/Controller/Admin.pm:100 lib/Lstu/Controller/Admin.pm:60
#: lib/Lstu/Controller/Admin.pm:87 themes/default/templates/api.html.ep:122
msgid "Bad password"
msgstr ""
......@@ -52,7 +56,7 @@ msgstr ""
msgid "Created"
msgstr ""
#: themes/default/templates/api.html.ep:15 themes/default/templates/index.html.ep:10 themes/default/templates/index.html.ep:9
#: themes/default/templates/api.html.ep:49 themes/default/templates/index.html.ep:10 themes/default/templates/index.html.ep:9
msgid "Custom shortened text"
msgstr ""
......@@ -60,7 +64,7 @@ msgstr ""
msgid "Delete"
msgstr ""
#: themes/default/templates/api.html.ep:21 themes/default/templates/api.html.ep:39
#: themes/default/templates/api.html.ep:145 themes/default/templates/api.html.ep:58 themes/default/templates/api.html.ep:76
msgid "Failure reason"
msgstr ""
......@@ -80,15 +84,36 @@ msgstr ""
msgid "Hit Enter, then Ctrl+C to copy the short link"
msgstr ""
#: themes/default/templates/api.html.ep:16
#: themes/default/templates/api.html.ep:126
msgid "If \"action\" is defined to \"logout\":"
msgstr ""
#: themes/default/templates/api.html.ep:119
msgid "If \"adminpwd\" is defined:"
msgstr ""
#: themes/default/templates/api.html.ep:115 themes/default/templates/api.html.ep:140 themes/default/templates/api.html.ep:18 themes/default/templates/api.html.ep:32 themes/default/templates/api.html.ep:50 themes/default/templates/api.html.ep:93
msgid "If equal to \"json\", response will be in JSON format, HTML format otherwise"
msgstr ""
#: themes/default/templates/api.html.ep:21 themes/default/templates/api.html.ep:39
#. (config('page_offset')
#: themes/default/templates/api.html.ep:86
msgid "If you are logged in as admin (\"adminpwd\" or \"hashed_adminpwd\" setting), it will give you statistics for all URLs, sorted by the most visited first, paginated with pages containing %1 records."
msgstr ""
#: themes/default/templates/api.html.ep:91
msgid "If you are logged in as admin, you can provide a \"page\" parameter"
msgstr ""
#: themes/default/templates/api.html.ep:84
msgid "It will use Lstu's cookies to know which shortened URL it will return statistics for."
msgstr ""
#: themes/default/templates/api.html.ep:122 themes/default/templates/api.html.ep:145 themes/default/templates/api.html.ep:23 themes/default/templates/api.html.ep:58 themes/default/templates/api.html.ep:76
msgid "JSON: failure"
msgstr ""
#: themes/default/templates/api.html.ep:20 themes/default/templates/api.html.ep:38
#: themes/default/templates/api.html.ep:121 themes/default/templates/api.html.ep:129 themes/default/templates/api.html.ep:144 themes/default/templates/api.html.ep:22 themes/default/templates/api.html.ep:36 themes/default/templates/api.html.ep:57 themes/default/templates/api.html.ep:75
msgid "JSON: success"
msgstr ""
......@@ -117,7 +142,7 @@ msgstr ""
msgid "No shortened URL available. Please retry or contact the administrator at %1. Your URL to shorten: %2"
msgstr ""
#: themes/default/templates/api.html.ep:12
#: themes/default/templates/api.html.ep:111 themes/default/templates/api.html.ep:138 themes/default/templates/api.html.ep:14 themes/default/templates/api.html.ep:30 themes/default/templates/api.html.ep:46 themes/default/templates/api.html.ep:88
msgid "Parameters:"
msgstr ""
......@@ -125,7 +150,7 @@ msgstr ""
msgid "Password"
msgstr ""
#: lib/Lstu.pm:232
#: lib/Lstu.pm:246 themes/default/templates/api.html.ep:23
msgid "Please, check your credentials: unable to authenticate."
msgstr ""
......@@ -133,11 +158,15 @@ msgstr ""
msgid "Previous"
msgstr ""
#: themes/default/templates/api.html.ep:22
#: themes/default/templates/api.html.ep:103 themes/default/templates/api.html.ep:123 themes/default/templates/api.html.ep:130 themes/default/templates/api.html.ep:146 themes/default/templates/api.html.ep:24 themes/default/templates/api.html.ep:37 themes/default/templates/api.html.ep:59
msgid "Response for HTML format"
msgstr ""
#: themes/default/templates/api.html.ep:18 themes/default/templates/api.html.ep:27 themes/default/templates/api.html.ep:36
#: themes/default/templates/api.html.ep:102
msgid "Response for JSON format"
msgstr ""
#: themes/default/templates/api.html.ep:117 themes/default/templates/api.html.ep:142 themes/default/templates/api.html.ep:20 themes/default/templates/api.html.ep:34 themes/default/templates/api.html.ep:52 themes/default/templates/api.html.ep:64 themes/default/templates/api.html.ep:73 themes/default/templates/api.html.ep:95
msgid "Response:"
msgstr ""
......@@ -145,6 +174,10 @@ msgstr ""
msgid "Shortened URL"
msgstr ""
#: themes/default/templates/api.html.ep:127 themes/default/templates/api.html.ep:29
msgid "Should always be successful"
msgstr ""
#: themes/default/templates/layouts/default.html.ep:64 themes/default/templates/login.html.ep:16 themes/default/templates/logout.html.ep:5
msgid "Signin"
msgstr ""
......@@ -164,7 +197,7 @@ msgid "The URL redirects 3 times or most. It's most likely a dangerous URL (spam
msgstr ""
#. ($c->url_for('/')
#: lib/Lstu/Controller/Admin.pm:89 lib/Lstu/Controller/Lstu.pm:279
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Lstu.pm:279
msgid "The shortened URL %1 doesn't exist."
msgstr ""
......@@ -178,15 +211,23 @@ msgstr ""
msgid "The shortened text can contain only numbers, letters and the - and _ character, can't be \"a\", \"api\", \"d\" or \"stats\" or end with \".json\". Your URL to shorten: %1"
msgstr ""
#: themes/default/templates/api.html.ep:113
msgid "To do an admin login, set it to the password defined in the settings (\"adminpwd\" or \"hashed_adminpwd\")"
msgstr ""
#: themes/default/templates/api.html.ep:114
msgid "To do an admin logout, set it to \"logout\"."
msgstr ""
#: lib/Lstu/Controller/Admin.pm:29
msgid "Too many bad passwords. You're banned."
msgstr ""
#: themes/default/templates/stats.html.ep:29
#: themes/default/templates/api.html.ep:102 themes/default/templates/stats.html.ep:29
msgid "URL"
msgstr ""
#: themes/default/templates/api.html.ep:14 themes/default/templates/index.html.ep:5 themes/default/templates/index.html.ep:6
#: themes/default/templates/api.html.ep:48 themes/default/templates/index.html.ep:5 themes/default/templates/index.html.ep:6
msgid "URL to shorten"
msgstr ""
......@@ -195,26 +236,74 @@ msgstr ""
msgid "You asked to shorten too many URLs too quickly. You're banned for %1 hour(s)."
msgstr ""
#: themes/default/templates/logout.html.ep:3
#: lib/Lstu.pm:237 lib/Lstu/Controller/Admin.pm:52 themes/default/templates/api.html.ep:121 themes/default/templates/api.html.ep:22
msgid "You have been successfully logged in."
msgstr ""
#: lib/Lstu.pm:276 lib/Lstu/Controller/Admin.pm:73 themes/default/templates/api.html.ep:129 themes/default/templates/api.html.ep:36 themes/default/templates/logout.html.ep:3
msgid "You have been successfully logged out."
msgstr ""
#: themes/default/templates/api.html.ep:22
msgid "You will be redirected to the Lstu interface with a message giving the shortened URL"
#: themes/default/templates/api.html.ep:137
msgid "You must be logged in as admin (\"adminpwd\" or \"hashed_adminpwd\" setting) to use it."
msgstr ""
#: themes/default/templates/api.html.ep:30
#: themes/default/templates/api.html.ep:44 themes/default/templates/api.html.ep:82
msgid "You must be logged in to use it."
msgstr ""
#: themes/default/templates/api.html.ep:130
msgid "You will be redirected to Lstu statistics page"
msgstr ""
#: themes/default/templates/api.html.ep:146
msgid "You will be redirected to Lstu statistics page with a message in case of failure"
msgstr ""
#: themes/default/templates/api.html.ep:37
msgid "You will be redirected to Lstu successfully logged out interface"
msgstr ""
#: themes/default/templates/api.html.ep:24
msgid "You will be redirected to the Lstu classic interface where you will be able to shorten URLs"
msgstr ""
#: themes/default/templates/api.html.ep:59
msgid "You will be redirected to the Lstu classic interface with a message giving the shortened URL"
msgstr ""
#: themes/default/templates/api.html.ep:67
msgid "You will be redirected to the targeted URL or to the Lstu interface with a message giving the failure reason"
msgstr ""
#: themes/default/templates/api.html.ep:14
#: themes/default/templates/api.html.ep:123
msgid "You will have the statistics page with the admin stats if you're succesfully logged in, or your stats and a failure message otherwise"
msgstr ""
#: lib/Lstu/Controller/Admin.pm:142
msgid "You're not authenticated as the admin"
msgstr ""
#: themes/default/templates/api.html.ep:16
msgid "Your login"
msgstr ""
#: themes/default/templates/api.html.ep:17
msgid "Your password"
msgstr ""
#: themes/default/templates/api.html.ep:102
msgid "integer, how many unique visits of the shortened URL"
msgstr ""
#: themes/default/templates/api.html.ep:16 themes/default/templates/api.html.ep:17 themes/default/templates/api.html.ep:48
msgid "mandatory"
msgstr ""
#: themes/default/templates/api.html.ep:15 themes/default/templates/api.html.ep:16
#: themes/default/templates/api.html.ep:113 themes/default/templates/api.html.ep:114 themes/default/templates/api.html.ep:115 themes/default/templates/api.html.ep:140 themes/default/templates/api.html.ep:18 themes/default/templates/api.html.ep:32 themes/default/templates/api.html.ep:49 themes/default/templates/api.html.ep:50 themes/default/templates/api.html.ep:91 themes/default/templates/api.html.ep:93
msgid "optional"
msgstr ""
#: themes/default/templates/api.html.ep:25 themes/default/templates/api.html.ep:34
#: themes/default/templates/api.html.ep:135 themes/default/templates/api.html.ep:62 themes/default/templates/api.html.ep:71 themes/default/templates/api.html.ep:99
msgid "shortened_url"
msgstr ""
This diff is collapsed.
This diff is collapsed.
% # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab:
% title 'Lstu API';
% use Mojo::JSON qw(to_json);
%= stylesheet begin
dd > strong {
padding-left: 15px;
......@@ -7,8 +8,41 @@
% end
<h2>Lstu API</h2>
<dl>
<dt>POST /a</dt>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<dt>POST <%= url_for('login') %></dt>
<dd>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>login</strong> <em><%= l('mandatory') %></em><br><%= l('Your login') %></li>
<li><strong>password</strong> <em><%= l('mandatory') %></em><br><%= l('Your password') %></li>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged in.')%>","success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Please, check your credentials: unable to authenticate.') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu classic interface where you will be able to shorten URLs') %></li>
</ul>
</dd>
<dt>GET <%= url_for('logout') %></dt>
<dd>
<p><%= l('Should always be successful') %></p>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged out.')%>","success":true}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu successfully logged out interface') %></li>
</ul>
</dd>
% }
<dt>POST <%= url_for('add') %></dt>
<dd>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<p><%= l('You must be logged in to use it.') %></p>
% }
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>lsturl</strong> <em><%= l('mandatory') %></em><br><%= l('URL to shorten') %></li>
......@@ -17,12 +51,15 @@
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":"https:\/\/lstu.fr\/XzQBd6eF","success":true}</code></li>
% my $u = prefix().url_for('short', short => 'XzQBd6eFa');
% $u =~ s#//#/#g;
% $u = to_json $u;
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":<%= $u %>,"success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu interface with a message giving the shortened URL') %></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to the Lstu classic interface with a message giving the shortened URL') %></li>
</ul>
</dd>
<dt>GET /<%= l('shortened_url') %></dt>
<dt>GET <%= url_for('short', short => l('shortened_url')) %></dt>
<dd>
<strong><%= l('Response:') %></strong>
<ul>
......@@ -31,7 +68,7 @@
</li>
</ul>
</dd>
<dt>GET /<%= l('shortened_url') %>.json</dt>
<dt>GET <%= url_for('short', short => l('shortened_url')) %>.json</dt>
<dd>
<strong><%= l('Response:') %></strong>
<ul>
......@@ -39,4 +76,75 @@
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
</li>
</dd>
<dt>GET <%= url_for('stats') %></dt>
<dd>
% if (defined $self->config('ldap') || defined $self->config('htpasswd')) {
<p><%= l('You must be logged in to use it.') %></p>
% }
<p><%= l('It will use Lstu\'s cookies to know which shortened URL it will return statistics for.') %></p>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<p><%= l('If you are logged in as admin ("adminpwd" or "hashed_adminpwd" setting), it will give you statistics for all URLs, sorted by the most visited first, paginated with pages containing %1 records.', config('page_offset')) %></p>
% }
<strong><%= l('Parameters:') %></strong>
<ul>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<li><strong>page</strong> <em><%= l('optional') %></em><br><%= l('If you are logged in as admin, you can provide a "page" parameter') %>
% }
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong><br>
<ul>
<li>
<ul>
% $u = prefix().url_for('short', short => l('shortened_url'));
% $u =~ s#//#/#g;
% $u = to_json $u;
<li><strong><%= l('Response for JSON format') %></strong><br><code>[{"short":<%= $u %>,"url":"<%= l('URL') %>","counter":<%= l('integer, how many unique visits of the shortened URL') %>}]</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('A page with a table containing the same informations that the JSON response') %></li>
</ul>
</li>
</ul>
</dd>
% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) {
<dt>POST <%= url_for('stats') %></dt>
<dd>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>adminpwd</strong></li> <em><%= l('optional') %></em><br><%= l('To do an admin login, set it to the password defined in the settings ("adminpwd" or "hashed_adminpwd")') %></li>
<li><strong>action</strong></li> <em><%= l('optional') %></em><br><%= l('To do an admin logout, set it to "logout".') %></li>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><%= l('If "adminpwd" is defined:') %>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged in.')%>","success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Bad password') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will have the statistics page with the admin stats if you\'re succesfully logged in, or your stats and a failure message otherwise') %></li>
</ul>
</li>
<li><%= l('If "action" is defined to "logout":') %><br>
<%= l('Should always be successful') %></p>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"msg":"<%= l('You have been successfully logged out.')%>","success":true}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu statistics page') %></li>
</ul>
</li>
</ul>
</dd>
<dt>GET <%= url_for('delete', short => l('shortened_url')) %></dt>
<dd>
<p><%= l('You must be logged in as admin ("adminpwd" or "hashed_adminpwd" setting) to use it.') %></p>
<strong><%= l('Parameters:') %></strong>
<ul>
<li><strong>format</strong> <em><%= l('optional') %></em><br><%= l('If equal to "json", response will be in JSON format, HTML format otherwise') %></li>
</ul>
<strong><%= l('Response:') %></strong>
<ul>
<li><strong><%= l('JSON: success') %></strong><br><code>{"url":"https:\/\/fiat-tux.fr","short":<%= $u %>,"success":true}</code></li>
<li><strong><%= l('JSON: failure') %></strong><br><code>{"msg":"<%= l('Failure reason') %>","success":false}</code></li>
<li><strong><%= l('Response for HTML format') %></strong><br><%= l('You will be redirected to Lstu statistics page with a message in case of failure') %></li>
</ul>
</dd>
% }
</dl>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment