New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gradual serving #120

Open
szabgab opened this Issue May 29, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@szabgab
Member

szabgab commented May 29, 2017

I wonder if this is possible:

A user creates a file app.pl with

use v6;
use Bailador;
baile;

That, without any additional configuration, will map any 'nice' URL to the corresponding .html file in the views/ directory So the user can created views/index.html and it will be served by the route / and the user can create about.html and it will be served by the route /about. The html files will be served by calling template()
If the requested route does not have a corresponding .html file Bailador will automaticly try to serve it from the public/ directory as a static file. (css, js, ico, tx, images etc.)
If no such file either then 404

Then if the user wants to be more dynamic she can change the html files to include other template files (eg. header, footer) and have parameters in them as well.

If the user wants more control she can start to add routes to the app.pl that will work as expected.

I am not sure if either of these are needed, but the static file serving can be turned off with some configuration parameter. Also the automatic route to html mapping can be turned off by a configuration parameter.

My, not yet successful attempt to implement something like this in a bailador file looks like this:

use v6;
use Bailador;
use Bailador::Route::StaticFile;

#my $files = Bailador::Route::StaticFile.new: directory => $dir, path => '/html/:file';
#add_route: $files;

# this is planned to be a bit more generic than needed here
my $root = $*PROGRAM.absolute.IO.dirname;
if $root.IO.basename eq 'bin' {
    $root = $root.IO.dirname;
}
my $files = Bailador::Route::StaticFile.new: directory => $root, path => /.*/;

get '/(.*)' => sub ($url) {
    my $file  = ($url eq '' ?? 'index' !! $url) ~ '.html';
    my $path = $root.IO.child('views').child($file).Str;
    if $path.IO.e {
        return template($file)
    }

    return False if $url eq '';
    my $static = $root.IO.child('public').child($url);
    content_type('text/css') if $url ~~ /\.css$/;
    content_type('image/x-icon') if $url ~~ /\.ico$/;
    return $static if $static.f;

    return False;
}

baile();

@ufobat ufobat self-assigned this May 30, 2017

@szabgab

This comment has been minimized.

Show comment
Hide comment
@szabgab

szabgab Jun 3, 2017

Member

There is now a module https://github.com/Bailador/bailador.net/blob/main/lib/BailadorGradual.pm in bailador.net that serves this purpose, but must be loaded at the end of the file, just before we call baile() or it will preempt all the other routes. The nice way to use this should be:

use v6;
use Bailador;
use Bailador::Gradual;
baile()

or even

use v6;
use Bailador::Gradual;
baile()
Member

szabgab commented Jun 3, 2017

There is now a module https://github.com/Bailador/bailador.net/blob/main/lib/BailadorGradual.pm in bailador.net that serves this purpose, but must be loaded at the end of the file, just before we call baile() or it will preempt all the other routes. The nice way to use this should be:

use v6;
use Bailador;
use Bailador::Gradual;
baile()

or even

use v6;
use Bailador::Gradual;
baile()
@szabgab

This comment has been minimized.

Show comment
Hide comment
@szabgab
Member

szabgab commented Jun 25, 2017

See e532a64

@ufobat

This comment has been minimized.

Show comment
Hide comment
@ufobat

ufobat Jul 11, 2017

Member

I think we should move it into Bailador::Feature::Gradual
In order to use it we can do:

use v6.c;
use Bailador;

use-feature('Gradual');
....
baile()
Member

ufobat commented Jul 11, 2017

I think we should move it into Bailador::Feature::Gradual
In order to use it we can do:

use v6.c;
use Bailador;

use-feature('Gradual');
....
baile()
@szabgab

This comment has been minimized.

Show comment
Hide comment
@szabgab

szabgab Jul 11, 2017

Member

Go for it. Though as I mentioned elsewhere, currently I think it might be nice to have this out of the box without writing anything. In any case moving to Bailador::Feature::Gradual would be a good step.

Member

szabgab commented Jul 11, 2017

Go for it. Though as I mentioned elsewhere, currently I think it might be nice to have this out of the box without writing anything. In any case moving to Bailador::Feature::Gradual would be a good step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment