This commit is contained in:
parent
70d23486b6
commit
b36235801b
1 changed files with 15 additions and 10 deletions
|
@ -218,6 +218,8 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
|
||||||
no warnings 'experimental';
|
no warnings 'experimental';
|
||||||
use builtin qw/true false/;
|
use builtin qw/true false/;
|
||||||
use Hash::Merge;
|
use Hash::Merge;
|
||||||
|
use HTTP::Response;
|
||||||
|
use HTTP::Request;
|
||||||
|
|
||||||
field $_json = JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1);
|
field $_json = JSON::MaybeXS->new(utf8 => 1, convert_blessed => 1);
|
||||||
field $http_server;
|
field $http_server;
|
||||||
|
@ -246,13 +248,8 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
|
||||||
$http_server = Net::Async::HTTP::Server->new(
|
$http_server = Net::Async::HTTP::Server->new(
|
||||||
$httpserver_args->%*,
|
$httpserver_args->%*,
|
||||||
on_request => sub($httpself, $req) {
|
on_request => sub($httpself, $req) {
|
||||||
my $f = $self->loop->new_future();
|
my $async_f = $self->_route_request($req, $ctx);
|
||||||
|
|
||||||
my $async_f = $self->_route_request($httpself, $req, $ctx);
|
|
||||||
$f->on_done($async_f);
|
|
||||||
$self->adopt_future($async_f);
|
$self->adopt_future($async_f);
|
||||||
|
|
||||||
$f->done();
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -282,21 +279,25 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
|
||||||
|
|
||||||
method register_url(%opts) {
|
method register_url(%opts) {
|
||||||
# TODO check params
|
# TODO check params
|
||||||
|
use Data::Dumper;
|
||||||
|
say Dumper("Got url registered", \%opts);
|
||||||
push $routes->@*, \%opts;
|
push $routes->@*, \%opts;
|
||||||
}
|
}
|
||||||
|
|
||||||
async method _route_request($req, $ctx) {
|
async method _route_request($req, $ctx) {
|
||||||
my $method = $req->method();
|
my $method = $req->method();
|
||||||
my $uri = URI->new($req->uri);
|
my $path = $req->path;
|
||||||
my $path = $uri->path;
|
|
||||||
|
say "Got request ", $method, " => ", $path;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
my $found_route = false;
|
my $found_route = false;
|
||||||
my $f;
|
my $f;
|
||||||
for my $route ($self->routes->@*) {
|
for my $route ($routes->@*) {
|
||||||
if ($uri =~ $route->{url} && $route->{method} eq $method) {
|
if ($path =~ $route->{url} && $route->{method} eq $method) {
|
||||||
my $params = +{%+, _ => [@+]}; # make a copy of named parameters, and digited ones to pass into the handler
|
my $params = +{%+, _ => [@+]}; # make a copy of named parameters, and digited ones to pass into the handler
|
||||||
$found_route = true;
|
$found_route = true;
|
||||||
|
say "Found path $route->{url}";
|
||||||
|
|
||||||
my $obj;
|
my $obj;
|
||||||
if ($route->{decoder} eq "www-form-urlencoded") {
|
if ($route->{decoder} eq "www-form-urlencoded") {
|
||||||
|
@ -350,6 +351,10 @@ class OpenAIAsync::Server :repr(HASH) :strict(params) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unless ($found_route) {
|
||||||
|
$self->_resp_custom($req, 404, "Not found");
|
||||||
|
}
|
||||||
} catch($err) {
|
} catch($err) {
|
||||||
$self->_resp_custom($req, 400, "Error: ".$err);
|
$self->_resp_custom($req, 400, "Error: ".$err);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue