| ... | ... |
@@ -38,3 +38,4 @@ xt/import_rep.db |
| 38 | 38 |
xt/import_rep_user |
| 39 | 39 |
xt/smart_http.db |
| 40 | 40 |
xt/smart_http |
| 41 |
+public/api/* |
| ... | ... |
@@ -4,6 +4,9 @@ our $VERSION = '0.01'; |
| 4 | 4 |
|
| 5 | 5 |
use Mojo::Base 'Mojolicious'; |
| 6 | 6 |
use DBIx::Custom; |
| 7 |
+use File::Path 'mkpath'; |
|
| 8 |
+use Mojo::JSON; |
|
| 9 |
+use Mojo::Util 'spurt'; |
|
| 7 | 10 |
|
| 8 | 11 |
has 'dbi'; |
| 9 | 12 |
|
| ... | ... |
@@ -35,18 +38,30 @@ sub startup {
|
| 35 | 38 |
# Route |
| 36 | 39 |
my $r = $self->routes; |
| 37 | 40 |
$r->get('/')->to(template => 'index');
|
| 38 |
- $r->get('/api/book/:id/content')->to(cb => sub {
|
|
| 41 |
+ $r->get('/api/book/:id/content.json')->to(cb => sub {
|
|
| 39 | 42 |
my $self = shift; |
| 40 | 43 |
my $id = $self->param('id');
|
| 41 | 44 |
|
| 42 | 45 |
my $dbi = $self->app->dbi; |
| 43 | 46 |
my $content = $dbi->model('book')->select('content', id => $id)->value;
|
| 44 | 47 |
|
| 45 |
- my $data = {
|
|
| 46 |
- content => $content |
|
| 47 |
- }; |
|
| 48 |
- |
|
| 49 |
- $self->render(json => $data); |
|
| 48 |
+ if ($content) {
|
|
| 49 |
+ my $dir = $self->app->home->rel_file("public/api/book/$id");
|
|
| 50 |
+ mkpath $dir; |
|
| 51 |
+ |
|
| 52 |
+ my $data = {
|
|
| 53 |
+ content => $content |
|
| 54 |
+ }; |
|
| 55 |
+ |
|
| 56 |
+ my $json = Mojo::JSON->new; |
|
| 57 |
+ my $data_json = $json->encode($data); |
|
| 58 |
+ spurt $data_json, $self->app->home->rel_file("public/api/book/$id/content.json");
|
|
| 59 |
+ |
|
| 60 |
+ $self->render_static("/api/book/$id/content.json");
|
|
| 61 |
+ } |
|
| 62 |
+ else {
|
|
| 63 |
+ $self->render_not_found; |
|
| 64 |
+ } |
|
| 50 | 65 |
}); |
| 51 | 66 |
|
| 52 | 67 |
$r->get('/api/word-count/:word')->to(cb => sub {
|
| ... | ... |
@@ -73,7 +73,7 @@ |
| 73 | 73 |
var ret = book_id_str.match(/book-(\d+)/); |
| 74 | 74 |
var book_id = ret[1]; |
| 75 | 75 |
|
| 76 |
- $.get('/api/book/' + book_id + '/content', function (result) {
|
|
| 76 |
+ $.get('/api/book/' + book_id + '/content.json', function (result) {
|
|
| 77 | 77 |
|
| 78 | 78 |
if (current_word) {
|
| 79 | 79 |
var i = 1; |