summaryrefslogtreecommitdiff
path: root/lib/Dancer2/Cookbook.pod
blob: 26a018e3130c91a177470115d1eee5b5d5284a8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
package Dancer2::Cookbook;
# ABSTRACT: Example-driven quick-start to the Dancer2 web framework

__END__

=pod

=encoding UTF-8

=head1 NAME

Dancer2::Cookbook - Example-driven quick-start to the Dancer2 web framework

=head1 VERSION

version 1.1.0

=head1 DESCRIPTION

A quick-start guide with examples to get you up and running with the Dancer2
web framework. This document will be twice as useful if you finish reading
the manual (L<Dancer2::Manual>) first, but that is not required... :-)

=head1 BEGINNER'S DANCE

=head2 A simple Dancer2 web app

Dancer2 has been designed to be easy to work with - it's trivial to write a
simple web app, but still has the power to work with larger projects. To
start with, let's make an incredibly simple "Hello World" example:

    #!/usr/bin/env perl

    use Dancer2;

    get '/hello/:name' => sub {
        return "Why, hello there " . route_parameters->get('name');
    };

    dance;

Yes - the above is a fully-functioning web app; running that script will
launch a webserver listening on the default port (3000). Now you can make a
request:

    $ curl http://localhost:3000/hello/Bob
    Why, hello there Bob

and it will say hello. The C<:name> part is a named parameter within the
route specification, whose value is made available through C<route_parameters>.

Note that you don't need to use the C<strict> and C<warnings> pragmas; they
are already loaded by Dancer2.

=head2 Default Route

In case you want to avoid a I<404 error>, or handle multiple routes in the
same way and you don't feel like configuring all of them, you can set up a
default route handler.

The default route handler will handle any request that doesn't get served by
any other route.

All you need to do is set up the following route as the B<last> route:

    any qr{.*} => sub {
        status 'not_found';
        template 'special_404', { path => request->path };
    };

Then you can set up the template like so:

    You tried to reach [% path %], but it is unavailable at the moment.

    Please try again or contact us at <contact@example.com>.

=head2 Using the C<auto_page> feature for automatic route creation

For simple "static" pages you can simply enable the C<auto_page> config
setting; this means you don't need to declare a route handler for those
pages; if a request is for C</foo/bar>, Dancer2 will check for a matching
view (e.g. C</foo/bar.tt>) and render it with the default layout, if
found. For full details, see the documentation for the L<auto_page
setting|Dancer2::Config/"auto_page">.

=head2 Enabling and disabling routes with config and Module::Runtime

For various reasons you may want to be able to turn routes on and off in
your app without having to comment out sections of code. This is easily
accomplished if you encapsulate each route handler (or group of related
handlers) in a separate module, and load the wanted routes at runtime.

In C<MyApp::Route::Foo>:

    package MyApp::Route::Foo;

    use Dancer2 appname => 'MyApp';

    get '/foo' => sub { return "bar" };

In C<MyApp::Route::Baz>:

    package MyApp::Route::Baz;

    use Dancer2 appname => 'MyApp';

    get '/baz' => sub { return "qux" };

In your C<config.yaml>:

    route_modules :
        Foo : 1
        Baz : 0

In your main route controller:

    use Dancer2;
    use Module::Runtime 'require_module';

    my $module_base = 'MyApp::Route::';

    my %modules = %{ config->{route_modules} };

    my @required_modules = grep { $modules{$_} } keys %modules;

    require_module( $module_base . $_ ) for @required_modules;

Now your app will expose C</foo> but requests to C</baz> will get a
404 response.

=head2 Simplifying AJAX queries with the Ajax plugin

As an AJAX query is just an HTTP query, it's similar to a GET or POST route.
You may ask yourself why you may want to use the C<ajax> keyword (from the
L<Dancer2::Plugin::Ajax> plugin) instead of a simple C<get>.

Let's say you have a path like C</user/:user> in your application. You may
want to be able to serve this page with a layout and HTML content. But you
may also want to be able to call this same url from a javascript query using
AJAX.

So, instead of having the following code:

    get '/user/:user' => sub {
         if ( request->is_ajax ) {
             # create xml, set headers to text/xml, blablabla
              header( 'Content-Type'  => 'text/xml' );
              header( 'Cache-Control' =>  'no-store, no-cache, must-revalidate' );
              to_xml({...})
         } else {
             template users => {...}
         }
    };

you can have

    ajax '/user/:user' => sub {
         to_xml( {...}, RootName => undef );
    }

and

    get '/user/:user' => sub {
        template users => {...}
    }

Because it's an AJAX query, you know you need to return XML content, so
the content type of the response is set for you.

=head3 Example: Feeding graph data through AJAX

Let us assume we are building an application that uses a plotting library
to generate a graph and expects to get its data, which is in the form
of word count from an AJAX call.

For the graph, we need the url I</data> to return a JSON representation
of the word count data. Dancer in fact has a C<encode_json()> function that takes
care of the JSON encapsulation.

     get '/data' => sub {
         open my $fh, '<', $count_file;

         my %contestant;
         while (<$fh>) {
             chomp;
             my ( $date, $who, $count ) = split '\s*,\s*';

             my $epoch = DateTime::Format::Flexible->parse_datetime($date)->epoch;
             my $time = 1000 * $epoch;
             $contestant{$who}{$time} = $count;
         }

         my @json;  # data structure that is going to be JSONified

         while ( my ( $peep, $data ) = each %contestant ) {
             push @json, {
                 label     => $peep,
                 hoverable => \1,    # so that it becomes JavaScript's 'true'
                 data => [ map  { [ $_, $data->{$_} ] }
                         sort { $a <=> $b }
                         keys %$data ],
             };
         }

         my $beginning = DateTime::Format::Flexible->parse_datetime( "2010-11-01")->epoch;
         my $end       = DateTime::Format::Flexible->parse_datetime( "2010-12-01")->epoch;

         push @json, {
             label => 'de par',
             data => [
                 [$beginning * 1000, 0],
                 [   DateTime->now->epoch * 1_000,
                     50_000
                       * (DateTime->now->epoch - $beginning)
                       / ($end - $beginning)
                 ]
               ],

         };

         encode_json( \@json );
     };

For more serious AJAX interaction, there's also L<Dancer2::Plugin::Ajax>
that adds an I<ajax> route handler to the mix.

Because it's an AJAX query, you know you need to return XML content, so
the content type of the response is set for you.

=head2 Using the prefix feature to split your application

For better maintainability, you may want to separate some of your application
components into different packages. Let's say we have a simple web app with an
admin section and want to maintain this in a different package:

    package myapp;
    use Dancer2;
    use myapp::admin;

    prefix undef;

    get '/' => sub {...};

    1;

    package myapp::admin;
    use Dancer2 appname => 'myapp';

    prefix '/admin';

    get '/' => sub {...};

    1;

The following routes will be generated for us:

    - get /
    - get /admin/
    - head /
    - head /admin/

By default, a separate application is created for every package that uses
Dancer2. The C<appname> tag is used to collect routes and hooks into a
single Dancer2 application. In the above example, C<appname =E<gt> 'myapp'>
adds the routes from C<myapp::admin> to the routes of the app C<myapp>.

When using multiple applications please ensure that your path definitions do
not overlap. For example, if using a default route as described above, once
a request is matched to the default route then no further routes (or
applications) would be reached.

=head2 Delivering custom error pages

=head3 At the Core

In Dancer2, creating new errors is done by creating a new L<Dancer2::Core::Error>

     my $oopsie = Dancer2::Core::Error->new(
         status  => 418,
         message => "This is the Holidays. Tea not acceptable. We want eggnog.",
         app     => $app,
     )

If not given, the status code defaults to a 500, there is no need for a message if
we feel taciturn, and while the C<$app> (which is a I<Dancer2::Core::App>
object holding all the pieces of information related to the current request) is
needed if we want to take advantage of the templates, we can also do without.

However, to be seen by the end user, we have to populate the L<Dancer2::Core::Response>
object with the error's data. This is done via:

     $oopsie->throw($response);

Or, if we want to use the response object already present in the C<$app>
(which is usually the case):

     $oopsie->throw;

This populates the status code of the response, sets its content, and throws a
I<halt()> in the dispatch process.

=head3 What it will look like

The error object has quite a few ways to generate its content.

First, it can be explicitly given

     my $oopsie = Dancer2::Core::Error->new(
         content => '<html><body><h1>OMG</h1></body></html>',
     );

If the C<$context> was given, the error will check if there is a
template by the name of the status code (so, say you're using Template
Toolkit, I<418.tt>) and will use it to generate the content, passing it
the error's C<$message>, C<$status> code and C<$title> (which, if not
specified, will be the standard http error definition for the status code).

If there is no template, the error will then look for a static page (to
continue with our example, I<418.html>) in the I<public/> directory.

And finally, if all of that failed, the error object will fall back on
an internal template.

=head3 Errors in Routes

The simplest way to use errors in routes is:

     get '/xmas/gift/:gift' => sub {
         die "sorry, we're all out of ponies\n"
             if route_parameters->get('gift') eq 'pony';
     };

The die will be intercepted by Dancer, converted into an error (status
code 500, message set to the dying words) and passed to the response.

In the cases where more control is required, C<send_error()> is the way to go:

     get '/glass/eggnog' => sub {
         send_error "Sorry, no eggnog here", 418;
     };

And if total control is needed:

     get '/xmas/wishlist' => sub {
         Dancer2::Core::Error->new(
             response => response(),
             status   => 406,
             message  => "nothing but coal for you, I'm afraid",
             template => 'naughty/index',
         )->throw unless user_was_nice();

         ...;
     };

=head2 Template Toolkit's WRAPPER directive in Dancer2

Dancer2 already provides a WRAPPER-like ability, which we call a "layout".
The reason we don't use Template Toolkit's WRAPPER (which also makes us
incompatible with it) is because not all template systems support it.
In fact, most don't.

However, you might want to use it, and be able to define META variables and
regular L<Template::Toolkit> variables.

These few steps will get you there:

=over 4

=item * Disable the layout in Dancer2

You can do this by simply commenting (or removing) the C<layout>
configuration in the config file.

=item * Use the Template Toolkit template engine

Change the configuration of the template to Template Toolkit:

    # in config.yml
    template: "template_toolkit"

=item * Tell the Template Toolkit engine which wrapper to use

    # in config.yml
    # ...
    engines:
        template:
            template_toolkit:
                WRAPPER: layouts/main.tt

=back

Done! Everything will work fine out of the box, including variables and META
variables.

However, disabling the internal layout it will also disable the hooks C<before_layout_render> and C<after_layout_render>.

=head2 Customizing Template Toolkit in Dancer2

Please see L<Dancer2::Template::TemplateToolkit|Dancer2::Template::TemplateToolkit/"ADVANCED CUSTOMIZATION">
for more details.

=head2 Accessing configuration information from a separate script

You may want to access your webapp's configuration from outside your
webapp. You could, of course, use the YAML module of your choice and load
your webapps's C<config.yml>, but chances are that this is not convenient.

Use Dancer2 instead. You can simply use
the values from C<config.yml> and some additional default values:

    # bin/show_app_config.pl
    use Dancer2;
    printf "template: %s\n", config->{'template'}; # simple
    printf "log: %s\n",      config->{'log'};      # undef

Note that C<< config->{log} >> should result in an uninitialized warning
on a default scaffold since the environment isn't loaded and
log is defined in the environment and not in C<config.yml>. Hence C<undef>.

Dancer2 will load your C<config.yml> configuration file along with the
correct environment file located in your C<environments> directory.

The environment is determined by two environment variables in the following
order:

=over 4

=item * DANCER_ENVIRONMENT

=item * PLACK_ENV

=back

If neither of those is set, it will default to loading the development
environment (typically C<$webapp/environment/development.yml>).

If you wish to load a different environment, you need to override these
variables.

You can call your script with the environment changed:

    $ PLACK_ENV=production perl bin/show_app_config.pl

Or you can override them directly in the script (less recommended):

    BEGIN { $ENV{'DANCER_ENVIRONMENT'} = 'production' }
    use Dancer2;

    ...

=head2 Using DBIx::Class

L<DBIx::Class>, also known as DBIC, is one of the many Perl ORM
(I<Object Relational Mapper>). It is easy to use DBIC in Dancer2 using the
L<Dancer2::Plugin::DBIC>.

=head3 An example

This example demonstrates a simple Dancer2 application that allows one to search
for authors or books. The application is connected to a database, that contains
authors, and their books. The website will have one single page with a form,
that allows one to query books or authors, and display the results.

=head4 Creating the application

    $ dancer2 -a bookstore

To use the Template Toolkit as the template engine, we specify it in the
configuration file:

    # add in bookstore/config.yml
    template: template_toolkit

=head4 Creating the view

We need a view to display the search form, and below, the results, if any. The
results will be fed by the route to the view as an arrayref of results. Each
result is a I<hashref>, with a author key containing the name of the author, and
a books key containing an I<arrayref> of strings : the books names.

    # example of a list of results
    [ { author => 'author 1',
        books => [ 'book 1', 'book 2' ],
      },
      { author => 'author 2',
        books => [ 'book 3', 'book 4' ],
      }
    ]


    # bookstore/views/search.tt
    <p>
    <form action="/search">
    Search query: <input type="text" name="query" />
    </form>
    </p>
    <br>

An example of the view, displaying the search form, and the results, if any:

    <% IF query.length %>
      <p>Search query was : <% query %>.</p>
      <% IF results.size %>
        Results:
        <ul>
        <% FOREACH result IN results %>
          <li>Author: <% result.author.replace("((?i)$query)", '<b>$1</b>') %>
          <ul>
          <% FOREACH book IN result.books %>
            <li><% book.replace("((?i)$query)", '<b>$1</b>') %>
          <% END %>
          </ul>
        <% END %>
      <% ELSE %>
        No result
      <% END %>
    <% END %>

=head4 Creating a Route

A simple route, to be added in the I<bookstore.pm> module:

    # add in bookstore/lib/bookstore.pm
    get '/search' => sub {
        my $query   = query_parameters->get('query');
        my @results = ();

        if ( length $query ) {
            @results = _perform_search($query);
        }

        template search => {
            query   => $query,
            results => \@results,
        };
    };

=head4 Creating a database

We create a SQLite file database:

    $ sqlite3 bookstore.db
    CREATE TABLE author(
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      firstname text default '' not null,
      lastname text not null);

    CREATE TABLE book(
      id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
      author INTEGER REFERENCES author (id),
      title text default '' not null );

Now, to populate the database with some data, we use L<DBIx::Class>:

    # populate_database.pl
    package My::Bookstore::Schema;
    use base qw(DBIx::Class::Schema::Loader);
    package main;
    my $schema = My::Bookstore::Schema->connect('dbi:SQLite:dbname=bookstore.db');
    $schema->populate('Author', [
      [ 'firstname', 'lastname'],
      [ 'Ian M.',    'Banks'   ],
      [ 'Richard',   'Matheson'],
      [ 'Frank',     'Herbert' ],
    ]);
    my @books_list = (
      [ 'Consider Phlebas',    'Banks'    ],
      [ 'The Player of Games', 'Banks'    ],
      [ 'Use of Weapons',      'Banks'    ],
      [ 'Dune',                'Herbert'  ],
      [ 'Dune Messiah',        'Herbert'  ],
      [ 'Children of Dune',    'Herbert'  ],
      [ 'The Night Stalker',   'Matheson' ],
      [ 'The Night Strangler', 'Matheson' ],
    );
    # transform author names into ids
    $_->[1] = $schema->resultset('Author')->find({ lastname => $_->[1] })->id
      foreach (@books_list);
    $schema->populate('Book', [
      [ 'title', 'author' ],
      @books_list,
    ]);

Then run it in the directory where I<bookstore.db> sits:

    perl populate_database.db

=head4 Using Dancer2::Plugin::DBIC

There are 2 ways of configuring DBIC to understand how the data is organized
in your database:

=over 4

=item * Use auto-detection

The configuration file needs to be updated to indicate the use of the
Dancer2::Plugin::DBIC plugin, define a new DBIC schema called I<bookstore> and
to indicate that this schema is connected to the SQLite database we created.

    # add in bookstore/config.yml
    plugins:
      DBIC:
        bookstore:
          dsn:  "dbi:SQLite:dbname=bookstore.db"

Now, C<_perform_search> can be implemented using L<Dancer2::Plugin::DBIC>. The
plugin gives you access to an additional keyword called B<schema>, which you
give the name of schema you want to retrieve. It returns a C<DBIx::Class::Schema::Loader>
which can be used to get a resultset and perform searches, as per standard
usage of DBIX::Class.

    # add in bookstore/lib/bookstore.pm
    sub _perform_search {
        my ($query) = @_;
        my $bookstore_schema = schema 'bookstore';
        my @results;
        # search in authors
        my @authors = $bookstore_schema->resultset('Author')->search({
          -or => [
            firstname => { like => "%$query%" },
            lastname  => { like => "%$query%" },
          ]
        });
        push @results, map {
            { author => join(' ', $_->firstname, $_->lastname),
              books => [],
            }
        } @authors;
        my %book_results;
        # search in books
        my @books = $bookstore_schema->resultset('Book')->search({
            title => { like => "%$query%" },
        });
        foreach my $book (@books) {
            my $author_name = join(' ', $book->author->firstname, $book->author->lastname);
            push @{$book_results{$author_name}}, $book->title;
        }
        push @results, map {
            { author => $_,
              books => $book_results{$_},
            }
        } keys %book_results;
        return @results;
    }

=item * Use home made schema classes

The L<DBIx::Class::MooseColumns> lets you write the DBIC schema classes
using L<Moose>. The schema classes should be put in a place that Dancer2
will find. A good place is in I<bookstore/lib/>.

Once your schema classes are in place, all you need to do is modify I<config.yml>
to specify that you want to use them, instead of the default auto-detection method:

    # change in bookstore/config.yml
    plugins:
      DBIC:
        bookstore:
          schema_class: My::Bookstore::Schema
          dsn: "dbi:SQLite:dbname=bookstore.db"

B<Starting the application>:
Our bookstore lookup application can now be started using the built-in server:

    # start the web application
    plackup bin/app.psgi

=back

=head2 Authentication

Writing a form for authentication is simple: we check the user credentials
on a request and decide whether to continue or redirect them to a form.
The form allows them to submit their username and password and we save that
and create a session for them so when they now try the original request,
we recognize them and allow them in.

=head3 Basic Application

The application is fairly simple. We have a route that needs authentication,
we have a route for showing the login page, and we have a route for posting
login information and creating a session.

     package MyApp;
     use Dancer2;

     get '/' => sub {
         session('user')
             or redirect('/login');

         template index => {};
     };

     get '/login' => sub {
         template login => {};
     };

     post '/login' => sub {
         my $username  = query_parameters->get('username');
         my $password  = query_parameters->get('password');
         my $redir_url = query_parameters->get('redirect_url') || '/login';

         $username eq 'john' && $password eq 'correcthorsebatterystaple'
             or redirect $redir_url;

         session user => $username;
         redirect $redir_url;
     };

=head3 Tiny Authentication Helper

L<Dancer2::Plugin::Auth::Tiny> allows you to abstract away not only the
part that checks whether the session exists, but to also generate a
redirect with the right path and return URL.

We simply have to define what routes needs a login using Auth::Tiny's
C<needs> keyword.

     get '/' => needs login => sub {
         template index => {};
     };

It creates a proper return URL using C<uri_for> and the address from which
the user arrived.

We can thus decorate all of our private routes to require authentication in
this manner. If a user does not have a session, it will automatically forward
it to I</login>, in which we would render a form for the user to send a login request.

Auth::Tiny even provides a new parameter, C<return_url>, which can be used to send
the user back to their original requested path.

=head3 Password Hashing

L<Dancer2::Plugin::Passphrase> provides a simple passwords-as-objects interface with
sane defaults for hashed passwords which you can use in your web application. It uses
B<bcrypt> as the default but supports anything the L<Digest> interface does.

Assuming we have the original user-creation form submitting a username and password:

     package MyApp;
     use Dancer2;
     use Dancer2::Plugin::Passphrase;
     post '/register' => sub {
         my $username = query_parameters->get('username');
         my $password = passphrase( 
             query_parameters->get('password') 
         )->generate;

         # $password is now a hashed password object
         save_user_in_db( $username, $password->rfc2307 );

         template registered => { success => 1 };
     };

We can now add the B<POST> method for verifying that username and password:

     post '/login' => sub {
         my $username   = query_parameters->get('username');
         my $password   = query_parameters->get('password');
         my $saved_pass = fetch_password_from_db($username);

         if ( passphrase($password)->matches($saved_pass) ) {
             session user => $username;
             redirect query_parameters->get('return_url') || '/';
         }

         # let's render instead of redirect...
         template login => { error => 'Invalid username or password' };
     };

=head2 Writing a REST application

With Dancer2, it's easy to write REST applications. Dancer2 provides helpers
to serialize and deserialize for the following data formats:

=over 4

=item JSON

=item YAML

=item XML

=item Data::Dumper

=back

To activate this feature, you only have to set the C<serializer> setting to
the format you require, for instance in your config file:

   serializer: JSON

Or directly in your code:

   set serializer => 'JSON';

From now, all hashrefs or arrayrefs returned by a route will be serialized
to the format you chose, and all data received from B<POST> or B<PUT>
requests will be automatically deserialized.

    get '/hello/:name' => sub {
        # this structure will be returned to the client as
        # {"name":"$name"}
        return { name => query_parameters->get('name') };
    };

It's possible to let the client choose which serializer to use. For
this, use the C<mutable> serializer, and an appropriate serializer will be
chosen from the C<Content-Type> header.

It's also possible to return a custom error using the
L<send_error|Dancer2::Manual/send_error> keyword. When you don't use a serializer,
the C<send_error> function will take a string as first parameter (the
message), and an optional HTTP code. When using a serializer, the message
can be a string, an arrayref or a hashref:

    get '/hello/:name' => sub {
        if (...) {
           send_error("you can't do that");
           # or
           send_error({reason => 'access denied', message => "no"});
        }
    };

The content of the error will be serialized using the appropriate
serializer.

=head2 Using the serializer

Serializers essentially do two things:

=over 4

=item * Deserialize incoming requests

When a user makes a request with serialized input, the serializer
automatically deserializes it into actual input parameters.

=item * Serialize outgoing responses

When you return a data structure from a route, it will automatically
serialize it for you before returning it to the user.

=back

=head3 Configuring

In order to configure a serializer, you just need to pick which format
you want for encoding/decoding (e.g. L<JSON|Dancer2::Serializer::JSON>)
and set it up using the C<serializer> configuration keyword.

It is recommended to explicitly add it in the actual code instead of the
configuration file so it doesn't apply automatically to every app that
reads the configuration file (unless that's what you want):

     package MyApp;
     use Dancer2;
     set serializer => 'JSON'; # Dancer2::Serializer::JSON

     ...

=head3 Using

Now that we have a serializer set up, we can just return data structures:

     get '/' => sub {
         return { resources => \%resources };
     };

When we return this data structure, it will automatically be serialized
into JSON. No other code is necessary.

We also now receive requests in JSON:

     post '/:entity/:id' => sub {
         my $entity = route_parameters->get('entity');
         my $id     = route_parameters->get('id');

         # input which was sent serialized
         my $user = body_parameters->get('user');

         ...
     };

We can now make a serialized request:

     $ curl -X POST http://ourdomain/person/16 -d '{"user":"sawyer_x"}'

=head3 App-specific feature

Serializers are engines. They affect a Dancer Application, which means
that once you've set a serializer, B<all> routes within that package
will be serialized and deserialized. This is how the feature works.

As suggested above, if you would like to have both, you need to create
another application which will not be serialized.

A common usage for this is an API providing serialized endpoints (and
receiving serialized requests) and providing rendered pages.

     # MyApp.pm
     package MyApp;
     use Dancer2;

     # another useful feature:
     set auto_page => 1;

     get '/' => sub { template 'index' => {...} };

     # MyApp/API.pm
     package MyApp::API;
     use Dancer2;
     set serializer => 'JSON'; # or any other serializer

     get '/' => sub { +{ resources => \%resources, ... } };

     # user-specific routes, for example
     prefix '/users' => sub {
         get '/view'     => sub {...};
         get '/view/:id' => sub {...};
         put '/add'      => sub {...}; # automatically deserialized params
     };

     ...

Then those will be mounted together for a single app:

     # handler: app.pl:
     use MyApp;
     use MyApp::API;
     use Plack::Builder;

     builder {
         mount '/'    => MyApp->to_app;
         mount '/api' => MyApp::API->to_app;
     };

If you want use redirect from a mounted package to the application's root
URI, L<Dancer2::Plugin::RootURIFor> makes this possible:

    package OurWiki;
    use Dancer;
    use Dancer2::Plugin::RootURIFor;

    get '/:some_path' => sub {
        redirect root_uri_for('/');
    }

=head3 An example: Writing API interfaces

This example demonstrates an app that makes a request to a weather
API and then displays it dynamically in a web page.

Other than L<Dancer2> for defining routes, we will use L<HTTP::Tiny>
to make the weather API request, L<JSON> to decode it from JSON format,
and finally L<File::Spec> to provide a fully-qualified path to our
template engine.

     use JSON;
     use Dancer2;
     use HTTP::Tiny;
     use File::Spec;

=head4 Configuration

We use the L<Template::Toolkit> template system for this app.
Dancer searches for our templates in our views directory, which defaults
to I<views> directory in our current directory. Since we want to put our
template in our current directory, we will configure that. However,
I<Template::Toolkit> does not want us to provide a relative path without
configuring it to allow it. This is a security issue. So, we're using
L<File::Spec> to create a full path to where we are.

We also unset the default layout, so Dancer won't try to wrap our template
with another one. This is a feature in Dancer to allow you to wrap your
templates with a layout when your templating system doesn't support it. Since
we're not using a layout here, we don't need it.

     set template => 'template_toolkit';       # set template engine
     set layout   => undef;                    # disable layout
     set views    => File::Spec->rel2abs('.'); # full path to views

Now, we define our URL:

     my $url = 'http://api.openweathermap.org/data/2.5/weather?id=5110629&units=imperial';

=head4 Route

We will define a main route which, upon a request, will fetch the information
from the weather API, decode it, and then display it to the user.

Route definition:

     get '/' => sub {
         ...
     };

Editing the stub of route dispatching code, we start by making the request
and decoding it:

     # fetch data
     my $res = HTTP::Tiny->new->get($url);

     # decode request
     my $data = decode_json $res->{'content'};

The data is not just a flat hash. It's a deep structure. In this example, we
will filter it for only the simple keys in the retrieved data:

     my $metrics = { map +(
         ref $data->{$_} ? () : ( $_ => $data->{$_} )
     ), keys %{$data} };

All that is left now is to render it:

     template index => { metrics => $metrics };

=head1 NON-STANDARD STEPS

=head2 Turning off warnings

The C<warnings> pragma is already used when one loads Dancer2. However, if
you I<really> do not want the C<warnings> pragma (for example, due to an
undesired warning about use of undef values), add a C<no warnings> pragma to
the appropriate block in your module or psgi file.

=head1 AUTHOR

Dancer Core Developers

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut