feat: new route architecture

This commit is contained in:
2025-12-24 18:45:54 +01:00
parent a67a6214d1
commit 7c967b179e

View File

@@ -30,16 +30,38 @@ sub startup {
} }
); );
my $r = $self->routes; my $r = $self->routes;
my $api = $r->under('/api');
$api->post('/shorten')->to( $r->get('/health')->to(
cb => sub {
my $c = shift;
$c->render_later;
$c->db->ping->then(
sub {
$c->render( json => { status => 'ok' } );
}
)->catch(
sub {
my $err = shift;
$c->app->log->error("Health check DB error: $err");
$c->render(
json => { status => 'error', error => 'Database connection failed' },
status => 503
);
}
);
}
);
my $api_v1 = $r->under('/api/v1');
$api_v1->post('/urls')->to(
cb => sub { cb => sub {
my $c = shift; my $c = shift;
bless $c, 'Urupam::API'; bless $c, 'Urupam::API';
$c->shorten; $c->shorten;
} }
); );
$api->get('/url')->to( $api_v1->get('/urls/:short_code')->to(
cb => sub { cb => sub {
my $c = shift; my $c = shift;
bless $c, 'Urupam::API'; bless $c, 'Urupam::API';
@@ -47,37 +69,50 @@ sub startup {
} }
); );
$r->get('/')->to( $r->get('/:short_code')->to(
cb => sub { cb => sub {
my $c = shift; my $c = shift;
my $tx = $c->render_later->tx; $c->render_later;
my $db = $c->db; my $short_code = $c->param('short_code') // '';
my $url_service = $c->url_service;
my $validator = $c->validator;
$db->set( 'test_key' => '123soleil' )->then( unless ( $short_code
&& $validator->validate_short_code($short_code) )
{
$c->render(
json => { error => 'Invalid short code format' },
status => 400
);
return;
}
$url_service->get_original_url($short_code)->then(
sub { sub {
$c->app->log->info('Value set: test_key => 123soleil'); my $original_url = shift;
return $db->get('test_key'); if ($original_url) {
} $c->redirect_to($original_url);
)->then( }
sub { else {
my $value = shift; $c->render(
$c->app->log->info("Value retrieved: $value"); json => { error => 'Short code not found' },
$c->render( json => { status => 'ok', value => $value } ); status => 404
undef $tx; );
}
} }
)->catch( )->catch(
sub { sub {
my $err = shift; my $err = shift;
$c->app->log->error("DB error: $err"); $c->app->log->error("Redirect lookup error: $err");
$c->render( $c->render(
json => { status => 'error', message => "$err" }, json => { error => 'Internal server error' },
status => 500 status => 500
); );
undef $tx;
} }
); );
} }
); );
} }
1; 1;