| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | use utf8; | ||
| 4 | use 5.0100000; | ||
| 5 | |||
| 6 | package Ukigumo::Server::API::Dispatcher; | ||
| 7 | use Router::Simple; | ||
| 8 | use Data::Validator; | ||
| 9 | |||
| 10 | my $router = Router::Simple->new(); | ||
| 11 | |||
| 12 | sub rule { Ukigumo::Server::Validator->new(@_) } | ||
| 13 | sub post { | ||
| 14 | my ($path, $rule_src, $code) = @_; | ||
| 15 | my $rule = Data::Validator->new(@$rule_src)->with('NoThrow'); | ||
| 16 | $router->connect($path, +{ code => $code, rule => $rule }, {method => 'POST'}) | ||
| 17 | } | ||
| 18 | sub get { | ||
| 19 | my ($path, $rule_src, $code) = @_; | ||
| 20 | my $rule = Data::Validator->new(@$rule_src)->with('NoThrow'); | ||
| 21 | $router->connect($path, +{ code => $code, rule => $rule }, {method => 'GET'}) | ||
| 22 | } | ||
| 23 | |||
| 24 | get '/api/v1/branch/list' => [ | ||
| 25 | project => { isa => 'Str' }, | ||
| 26 | ] => sub { | ||
| 27 | my ($c, $args) = @_; | ||
| 28 | |||
| 29 | return +{ | ||
| 30 | branches => scalar(Ukigumo::Server::Command::Branch->list( | ||
| 31 | %$args | ||
| 32 | )) | ||
| 33 | }; | ||
| 34 | }; | ||
| 35 | |||
| 36 | post '/api/v1/branch/delete' => [ | ||
| 37 | project => { isa => 'Str' }, | ||
| 38 | branch => { isa => 'Str' }, | ||
| 39 | ] => sub { | ||
| 40 | my ($c, $args) = @_; | ||
| 41 | |||
| 42 | my $branch_id = Ukigumo::Server::Command::Branch->find( | ||
| 43 | %$args | ||
| 44 | ); | ||
| 45 | Ukigumo::Server::Command::Branch->delete( | ||
| 46 | branch_id => $branch_id | ||
| 47 | ); | ||
| 48 | |||
| 49 | return { }; | ||
| 50 | }; | ||
| 51 | |||
| 52 | get '/api/v1/report/search' => [ | ||
| 53 | project => { isa => 'Str' }, | ||
| 54 | branch => { isa => 'Str' }, | ||
| 55 | revision => { isa => 'Str' }, | ||
| 56 | limit => { isa => 'Int', default => 20 }, | ||
| 57 | ] => sub { | ||
| 58 | my ($c, $args) = @_; | ||
| 59 | |||
| 60 | # and insert it | ||
| 61 | my $reports = Ukigumo::Server::Command::Report->search( | ||
| 62 | %$args | ||
| 63 | ); | ||
| 64 | |||
| 65 | return { | ||
| 66 | reports => +[ | ||
| 67 | map { +{ | ||
| 68 | report_id => $_->{report_id}, | ||
| 69 | status => $_->{status}, | ||
| 70 | ctime => $_->{ctime}, | ||
| 71 | } } @$reports | ||
| 72 | ] | ||
| 73 | }; | ||
| 74 | }; | ||
| 75 | |||
| 76 | post '/api/v1/report/add' => [ | ||
| 77 | status => { isa => 'Int' }, | ||
| 78 | project => { isa => 'Str' }, | ||
| 79 | branch => { isa => 'Str' }, | ||
| 80 | vc_log => { isa => 'Str', optional => 1 }, | ||
| 81 | body => { isa => 'Str', optional => 1 }, | ||
| 82 | revision => { isa => 'Str' }, | ||
| 83 | repo => { isa => 'Str' }, | ||
| 84 | compare_url => { isa => 'Str', optional => 1 }, | ||
| 85 | elapsed_time_sec => { isa => 'Int', optional => 1 }, | ||
| 86 | ] => sub { | ||
| 87 | my ($c, $args) = @_; | ||
| 88 | |||
| 89 | my $body; | ||
| 90 | if (my $upload = $c->req->upload('body')) { | ||
| 91 | my $fname = $upload->path; | ||
| 92 | open my $fh, '<:encoding(utf-8)', $fname or die "Cannot open file: $fname: $!"; | ||
| 93 | $body = do { local $/; <$fh> }; | ||
| 94 | } | ||
| 95 | |||
| 96 | # get last report status | ||
| 97 | my $last_status = Ukigumo::Server::Command::Report->get_last_status( | ||
| 98 | project => $args->{project}, | ||
| 99 | branch => $args->{branch} | ||
| 100 | ); | ||
| 101 | |||
| 102 | # and insert it | ||
| 103 | my $report_id = Ukigumo::Server::Command::Report->insert( | ||
| 104 | (defined $body ? (body => $body) : () ), | ||
| 105 | %$args | ||
| 106 | ); | ||
| 107 | my $url = Ukigumo::Server::Command::Report->get_url($report_id); | ||
| 108 | |||
| 109 | return { | ||
| 110 | report => +{ | ||
| 111 | report_id => $report_id, | ||
| 112 | url => $url, | ||
| 113 | last_status => $last_status, | ||
| 114 | } | ||
| 115 | }; | ||
| 116 | }; | ||
| 117 | |||
| 118 | sub dispatch { | ||
| 119 | my ($class, $c) = @_; | ||
| 120 | |||
| 121 | if (my $controller = $router->match($c->req->env)) { | ||
| 122 | my $rule = $controller->{rule} or die; | ||
| 123 | my $args = $rule->validate($c->req->parameters->flatten); | ||
| 124 | if ($rule->has_errors) { | ||
| 125 | my $errors = $rule->clear_errors; | ||
| 126 | my $message = join("\n", map { $_->{message} } @$errors); | ||
| 127 | my $res = $c->render_json( +{ error => +{ message => $message } } ); | ||
| 128 | $res->code(400); | ||
| 129 | return $res; | ||
| 130 | } | ||
| 131 | my $res = $controller->{code}->($c, $args); | ||
| 132 | if (ref $res eq 'HASH') { | ||
| 133 | return $c->render_json($res); # succeeded | ||
| 134 | } else { | ||
| 135 | return $res; # succeeded | ||
| 136 | } | ||
| 137 | } else { | ||
| 138 | return $c->res_404(); # not found... | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; | ||
| 143 |