File: | lib/Yukki/Web/Router.pm |
Coverage: | 91.4% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Yukki::Web::Router; | ||||||
2 | |||||||
3 | 1 1 | 5 2 | use v5.24; | ||||
4 | 1 1 1 | 2 1 4 | use utf8; | ||||
5 | 1 1 1 | 10 1 2 | use Moo; | ||||
6 | |||||||
7 | extends 'Path::Router'; | ||||||
8 | |||||||
9 | 1 1 1 | 341 2 19 | use Yukki::Web::Router::Route; | ||||
10 | |||||||
11 | 1 1 1 | 4 2 4 | use Types::Standard qw( ArrayRef Str ); | ||||
12 | 1 1 1 | 390 1 4 | use Type::Utils qw( class_type declare as where ); | ||||
13 | 1 1 1 | 438 1 38 | use List::Util qw( all ); | ||||
14 | 1 1 1 | 3 1 3 | use Yukki::Types qw( LoginName ); | ||||
15 | |||||||
16 | 1 1 1 | 310 1 5 | use namespace::clean; | ||||
17 | |||||||
18 | # ABSTRACT: send requests to the correct controllers, yo | ||||||
19 | |||||||
20 - 39 | =head1 DESCRIPTION This maps incoming paths to the controllers that should be used to handle them. This is based on L<Path::Router>, but adds "slurpy" variables. =head1 EXTENDS L<Path::Router> =head1 ATTRIBUTES =head2 route_class Defaults to L<Yukki::Web::Router::Route>. =head2 inline This is turned off because inline slurpy routing is not implemented. =cut | ||||||
40 | |||||||
41 | # Add support for slurpy variables, inline off because I haven't written the match | ||||||
42 | # generator function yet. | ||||||
43 | has '+route_class' => ( default => 'Yukki::Web::Router::Route' ); | ||||||
44 | has '+inline' => ( default => 0 ); | ||||||
45 | |||||||
46 - 50 | =head2 app This is the L<Yukki> handler. =cut | ||||||
51 | |||||||
52 | has app => ( | ||||||
53 | is => 'ro', | ||||||
54 | isa => class_type('Yukki'), | ||||||
55 | required => 1, | ||||||
56 | weak_ref => 1, | ||||||
57 | handles => 'Yukki::Role::App', | ||||||
58 | ); | ||||||
59 | |||||||
60 - 66 | =head1 METHODS =head2 BUILD Builds the routing table used by L<Yukki::Web>. =cut | ||||||
67 | |||||||
68 | sub BUILD { | ||||||
69 | 1 | 1 | 2564 | my $self = shift; | |||
70 | |||||||
71 | $self->add_route('' => ( | ||||||
72 | defaults => { | ||||||
73 | redirect => 'page/view/main', | ||||||
74 | }, | ||||||
75 | acl => [ | ||||||
76 | 1 1 | 19 4 | [ none => { action => sub { 1 } } ], | ||||
77 | ], | ||||||
78 | target => $self->controller('Redirect'), | ||||||
79 | )); | ||||||
80 | |||||||
81 | $self->add_route('login/?:action' => ( | ||||||
82 | defaults => { | ||||||
83 | action => 'page', | ||||||
84 | }, | ||||||
85 | validations => { | ||||||
86 | action => qr/^(?:page|submit|exit)$/, | ||||||
87 | }, | ||||||
88 | acl => [ | ||||||
89 | 1 2 | 4278 7 | [ none => { action => sub { 1 } } ], | ||||
90 | ], | ||||||
91 | target => $self->controller('Login'), | ||||||
92 | )); | ||||||
93 | |||||||
94 | $self->add_route('profile/?:action' => ( | ||||||
95 | defaults => { | ||||||
96 | action => 'profile', | ||||||
97 | }, | ||||||
98 | validations => { | ||||||
99 | action => qr/^(?:profile|update)$/, | ||||||
100 | }, | ||||||
101 | acl => [ | ||||||
102 | 1 0 | 42 0 | [ none => { action => sub { 1 } } ], | ||||
103 | ], | ||||||
104 | target => $self->controller('Login'), | ||||||
105 | )); | ||||||
106 | |||||||
107 | 1 | 35 | $self->add_route('logout' => ( | ||||
108 | defaults => { | ||||||
109 | action => 'exit', | ||||||
110 | }, | ||||||
111 | acl => [ | ||||||
112 | [ none => { action => 'exit' } ] | ||||||
113 | ], | ||||||
114 | target => $self->controller('Login'), | ||||||
115 | )); | ||||||
116 | |||||||
117 | $self->add_route('page/:action/:repository/*:page' => ( | ||||||
118 | defaults => { | ||||||
119 | action => 'view', | ||||||
120 | repository => 'main', | ||||||
121 | }, | ||||||
122 | validations => { | ||||||
123 | action => qr/^(?:view|edit|history|diff|preview|attach|rename|remove)$/, | ||||||
124 | repository => qr/^[_a-z0-9]+$/i, | ||||||
125 | page => declare as ArrayRef[Str], where { | ||||||
126 | 12 12 | 271 39 | all { /^[_a-z0-9-.]+(?:\.[_a-z0-9-]+)*$/i } @$_ | ||||
127 | }, | ||||||
128 | }, | ||||||
129 | 1 | 23 | acl => [ | ||||
130 | [ read => { action => [ qw( view preview history diff ) ] } ], | ||||||
131 | [ write => { action => [ qw( edit attach rename remove ) ] } ], | ||||||
132 | ], | ||||||
133 | target => $self->controller('Page'), | ||||||
134 | )); | ||||||
135 | |||||||
136 | $self->add_route('attachment/:action/:repository/+:file' => ( | ||||||
137 | defaults => { | ||||||
138 | action => 'download', | ||||||
139 | repository => 'main', | ||||||
140 | file => [ 'untitled.txt' ], | ||||||
141 | }, | ||||||
142 | validations => { | ||||||
143 | action => qr/^(?:view|upload|download|rename|remove)$/, | ||||||
144 | repository => qr/^[_a-z0-9]+$/i, | ||||||
145 | file => declare as ArrayRef[Str], where { | ||||||
146 | 0 0 | 0 0 | all { /^[_a-z0-9-]+(?:\.[_a-z0-9-]+)*$/i } @$_ | ||||
147 | }, | ||||||
148 | }, | ||||||
149 | 1 | 69 | acl => [ | ||||
150 | [ read => { action => [ qw( view download ) ] } ], | ||||||
151 | [ write => { action => [ qw( upload rename remove ) ] } ], | ||||||
152 | ], | ||||||
153 | target => $self->controller('Attachment'), | ||||||
154 | )); | ||||||
155 | |||||||
156 | 1 | 44 | $self->add_route('admin/user/:action' => ( | ||||
157 | defaults => { | ||||||
158 | action => 'list', | ||||||
159 | special => 'admin_user', | ||||||
160 | }, | ||||||
161 | validations => { | ||||||
162 | action => qr/^(?:add|list)$/, | ||||||
163 | }, | ||||||
164 | acl => [ | ||||||
165 | [ read => { action => [ qw( list ) ] } ], | ||||||
166 | [ write => { action => [ qw( add ) ] } ], | ||||||
167 | ], | ||||||
168 | target => $self->controller('Admin::User'), | ||||||
169 | )); | ||||||
170 | |||||||
171 | 1 | 33 | $self->add_route('admin/user/:action/:login_name' => ( | ||||
172 | defaults => { | ||||||
173 | action => 'edit', | ||||||
174 | special => 'admin_user', | ||||||
175 | }, | ||||||
176 | validations => { | ||||||
177 | action => qr/^(?:edit|remove)$/, | ||||||
178 | login_name => LoginName, | ||||||
179 | }, | ||||||
180 | acl => [ | ||||||
181 | [ write => { action => [ qw( edit remove ) ] } ], | ||||||
182 | ], | ||||||
183 | target => $self->controller('Admin::User'), | ||||||
184 | )); | ||||||
185 | } | ||||||
186 | |||||||
187 | 1; |