File: MojoliciousTest.pm

package info (click to toggle)
libmojolicious-perl 8.71%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,996 kB
  • sloc: perl: 9,899; makefile: 31; javascript: 1
file content (157 lines) | stat: -rw-r--r-- 5,011 bytes parent folder | download
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
package MojoliciousTest;
use Mojo::Base 'Mojolicious';

use MojoliciousTest::Foo;

sub startup {
  my $self = shift;

  if ($self->mode eq 'development') {

    # Template and static file class with higher precedence for development
    unshift @{$self->static->classes},   'MojoliciousTest::Foo';
    unshift @{$self->renderer->classes}, 'MojoliciousTest::Foo';

    # Static root for development
    unshift @{$self->static->paths}, $self->home->child('public_dev');

    # Development namespace
    unshift @{$self->routes->namespaces}, 'MojoliciousTest3';
  }

  # Template and static file class with lower precedence for production
  push @{$self->static->classes},   'MojoliciousTest';
  push @{$self->renderer->classes}, 'MojoliciousTest';

  # Application specific commands
  push @{$self->commands->namespaces}, 'MojoliciousTest::Command';

  # Plugins in custom namespace
  unshift @{$self->plugins->namespaces}, $self->routes->namespaces->[-1] . '::Plugin';
  $self->plugin('test-some_plugin2');
  $self->plugin('UPPERCASETestPlugin');

  # Plugin for rendering return values
  $self->plugin('AroundPlugin');

  # Templateless renderer
  $self->renderer->add_handler(
    test => sub {
      my ($renderer, $c, $output) = @_;
      $$output = 'Hello Mojo from a templateless renderer!';
    }
  );

  # Renderer for a different file extension
  $self->renderer->add_handler(xpl => $self->renderer->handlers->{epl});

  # Shortcut for "/fun*" routes
  $self->routes->add_shortcut(
    fun => sub {
      my ($r, $append) = @_;
      $r->any("/fun$append");
    }
  );

  # Session
  $self->sessions->cookie_domain('.example.com');
  $self->sessions->cookie_path('/bar');

  # /plugin/upper_case
  # /plugin/camel_case (plugins loaded correctly)
  my $r = $self->routes;
  $r->any('/plugin/upper_case')->to('foo#plugin_upper_case');
  $r->any('/plugin/camel_case')->to('foo#plugin_camel_case');

  # /exceptional/*
  $r->any('/exceptional/:action')->to('exceptional#');

  # /exceptional_too/*
  $r->any('/exceptional_too')->inline(1)->to('exceptional#this_one_might_die')->any('/:action');

  # /fun/time
  $r->fun('/time')->to('foo#fun');

  # /happy/fun/time
  $r->any('/happy')->fun('/time')->to('foo#fun');

  # /fun/joy
  $r->fun('/joy')->to('foo#joy');

  # /stash_config
  $r->any('/stash_config')->to(controller => 'foo', action => 'config', config => {test => 123});

  # /test4 (named route for url_for)
  $r->any('/test4/:something')->to('foo#something', something => 23)->name('something');

  # /somethingtest (refer to another route with url_for)
  $r->put('/somethingtest')->to('foo#something');

  # /something_missing (refer to a non-existing route with url_for)
  $r->any('/something_missing')->to('foo#url_for_missing');

  # /test3 (no class, just a namespace)
  $r->any('/test3')->to(namespace => 'MojoliciousTestController', action => 'index');

  # /test2 (different namespace test)
  $r->any('/test2')->to(namespace => 'MojoliciousTest2', controller => 'Foo', action => 'test');

  # /test5 (only namespace test)
  $r->any('/test5')->to(namespace => 'MojoliciousTest2::Foo', action => 'test');

  # /test6 (no namespace test)
  $r->any('/test6')->to(namespace => '', controller => 'mojolicious_test2-foo', action => 'test');

  # /test7 (controller class shortcut)
  $r->any('/test7')->to('Foo::Bar#test');

  # /test8 (controller class)
  $r->any('/test8')->to(controller => 'Foo::Bar', action => 'test');

  # /test9 (controller in development namespace)
  $r->any('/test9')->to('bar#index');

  # /test10 (controller in both namespaces)
  $r->any('/test10')->to('baz#index');

  # /withblock (template with blocks)
  $r->any('/withblock')->to('foo#withBlock');

  # /staged (authentication with intermediate destination)
  my $b = $r->any('/staged')->inline(1)->to('foo#stage1', return => 1);
  $b->any->to(action => 'stage2');

  # /suspended (suspended intermediate destination)
  $r->any('/suspended')->inline(1)->to('foo#suspended')->any->inline(1)->to('foo#suspended')->any->to('foo#fun');

  # /longpoll (long polling)
  $r->any('/longpoll')->to('foo#longpoll');

  # /shortcut/act
  # /shortcut/ctrl
  # /shortcut/ctrl-act (shortcuts to controller#action)
  $r->any('/shortcut/ctrl-act')->to('foo#config', config => {test => 'ctrl-act'});
  $r->any('/shortcut/ctrl')->to('foo#', action => 'config', config => {test => 'ctrl'});
  $r->any('/shortcut/act')->to('#config', controller => 'foo', config => {test => 'act'});

  # /foo/session (session cookie with domain)
  $r->any('/foo/session')->to('foo#session_domain');

  # /rss.xml (mixed formats)
  $r->any('/rss.xml')->to('foo#bar', format => 'rss');

  # /*/* (the default route)
  $r->any('/<controller>/<action>')->to(action => 'index');

  # /just/some/template (embedded template)
  $r->any('/just/some/template')->to(template => 'just/some/template');
}

1;
__DATA__

@@ some/static/file.txt
Production static file with low precedence.

@@ just/some/template.html.ep
Production template with low precedence.