Showing 9 changed files with 181 additions and 87 deletions
+63 -87
lib/Gitprep.pm
... ...
@@ -49,6 +49,11 @@ sub startup {
49 49
     croak $error;
50 50
   }
51 51
   $git->bin($git_bin);
52
+
53
+  # Repository Manager
54
+  my $manager = Gitprep::RepManager->new(app => $self);
55
+  weaken $manager->{app};
56
+  $self->manager($manager);
52 57
   
53 58
   # Repository home
54 59
   my $rep_home = $self->home->rel_file('rep');
... ...
@@ -59,18 +64,6 @@ sub startup {
59 64
   }
60 65
   $self->git($git);
61 66
 
62
-  # Reverse proxy support
63
-  $ENV{MOJO_REVERSE_PROXY} = 1;
64
-  $self->hook('before_dispatch' => sub {
65
-    my $self = shift;
66
-    
67
-    if ($self->req->headers->header('X-Forwarded-Host')) {
68
-      my $prefix = shift @{$self->req->url->path->parts};
69
-      push @{$self->req->url->base->path->parts}, $prefix
70
-        if defined $prefix;
71
-    }
72
-  });
73
-  
74 67
   # DBI
75 68
   my $db_file = $self->home->rel_file('db/gitprep.db');
76 69
   my $dbi = DBIx::Custom->connect(
... ...
@@ -143,100 +136,83 @@ EOS
143 136
   
144 137
   # Helper
145 138
   $self->helper(gitprep_api => sub { Gitprep::API->new(shift) });
146
-  
147
-  # Route
148
-  my $r = $self->routes->route->to('main#');
149
-  
139
+
150 140
   # DBViewer(only development)
151 141
   if ($self->mode eq 'development') {
152 142
     eval {
153 143
       $self->plugin(
154 144
         'DBViewer',
155 145
         dsn => "dbi:SQLite:database=$db_file",
156
-        route => $r
157 146
       );
158 147
     };
159 148
   }
149
+    
150
+  # Auto route
151
+  $self->plugin('AutoRoute');
160 152
   
161
-  # Repository Manager
162
-  my $manager = Gitprep::RepManager->new(app => $self);
163
-  weaken $manager->{app};
164
-  $self->manager($manager);
165
-
166
-  # Home
167
-  $r->get('/')->name('home');
168
-  
169
-  # Start
170
-  $r->any('/_start')->name('_start');
171
-  
172
-  # Sign in
173
-  $r->any('/_login')->name('login');
174
-  
175
-  # Admin
176
-  $r->get('/_admin')->name('_admin');
177
-  
178
-  # Admin
153
+  # User defined Routes
179 154
   {
180
-    my $r = $r->route('/_admin')->to('_admin#');
155
+    my $r = $self->routes->route->to('main#');
181 156
 
182
-    # Users
183
-    $r->any('/users')->to('#users');
184
-    
185 157
     # User
158
+    $r->get('/:user')->name('user');
159
+    
160
+    # Project
186 161
     {
187
-      my $r = $r->route('/user')->to('_admin-user#');
162
+      my $r = $r->route('/:user/:project');
163
+      $r->get('/')->name('project');
188 164
       
189
-      # Create user
190
-      $r->any('/create')->to('#create');
191
-    }
192
-  }
165
+      # Commit
166
+      $r->get('/commit/#diff')->name('commit');
167
+      
168
+      # Commits
169
+      $r->get('/commits/#rev', {id => 'HEAD'})->name('commits');
170
+      $r->get('/commits/#rev/(*blob)')->name('commits');
171
+      
172
+      # Branches
173
+      $r->get('/branches');
193 174
 
194
-  # User
195
-  $r->get('/:user')->name('user');
196
-  
197
-  # Project
198
-  {
199
-    my $r = $r->route('/:user/:project');
200
-    $r->get('/')->name('project');
201
-    
202
-    # Commit
203
-    $r->get('/commit/#diff')->name('commit');
204
-    
205
-    # Commits
206
-    $r->get('/commits/#rev', {id => 'HEAD'})->name('commits');
207
-    $r->get('/commits/#rev/(*blob)')->name('commits');
208
-    
209
-    # Branches
210
-    $r->get('/branches')->name('branches');
175
+      # Tags
176
+      $r->get('/tags');
211 177
 
212
-    # Tags
213
-    $r->get('/tags')->name('tags');
178
+      # Tree
179
+      $r->get('/tree/(*object)')->name('tree');
180
+      
181
+      # Blob
182
+      $r->get('/blob/(*object)')->name('blob');
183
+      
184
+      # Blob diff
185
+      $r->get('/blobdiff/(#diff)/(*file)')->name('blobdiff');
186
+      
187
+      # Raw
188
+      $r->get('/raw/(*object)')->name('raw');
189
+      
190
+      # Archive
191
+      $r->get('/archive/(#rev).tar.gz')->name('archive')->to(archive_type => 'tar');
192
+      $r->get('/archive/(#rev).zip')->name('archive')->to(archive_type => 'zip');
193
+      
194
+      # Compare
195
+      $r->get('/compare/(#rev1)...(#rev2)')->name('compare');
196
+      
197
+      # Settings
198
+      $r->any('/settings');
199
+      
200
+      # Fork
201
+      $r->any('/fork');
202
+    }
203
+  }
214 204
 
215
-    # Tree
216
-    $r->get('/tree/(*object)')->name('tree');
217
-    
218
-    # Blob
219
-    $r->get('/blob/(*object)')->name('blob');
220
-    
221
-    # Blob diff
222
-    $r->get('/blobdiff/(#diff)/(*file)')->name('blobdiff');
223
-    
224
-    # Raw
225
-    $r->get('/raw/(*object)')->name('raw');
226
-    
227
-    # Archive
228
-    $r->get('/archive/(#rev).tar.gz')->name('archive')->to(archive_type => 'tar');
229
-    $r->get('/archive/(#rev).zip')->name('archive')->to(archive_type => 'zip');
230
-    
231
-    # Compare
232
-    $r->get('/compare/(#rev1)...(#rev2)')->name('compare');
233
-    
234
-    # Settings
235
-    $r->any('/settings')->name('settings');
205
+  # Reverse proxy support
206
+  $ENV{MOJO_REVERSE_PROXY} = 1;
207
+  $self->hook('before_dispatch' => sub {
208
+    my $self = shift;
236 209
     
237
-    # Fork
238
-    $r->any('/fork')->name('fork');
239
-  }
210
+    if ($self->req->headers->header('X-Forwarded-Host')) {
211
+      my $prefix = shift @{$self->req->url->path->parts};
212
+      push @{$self->req->url->base->path->parts}, $prefix
213
+        if defined $prefix;
214
+    }
215
+  });
240 216
 }
241 217
 
242 218
 1;
+118
mojo/lib/Mojolicious/Plugin/AutoRoute.pm
... ...
@@ -0,0 +1,118 @@
1
+package Mojolicious::Plugin::AutoRoute;
2
+use Mojo::Base 'Mojolicious::Plugin';
3
+
4
+our $VERSION = '0.07';
5
+
6
+sub register {
7
+  my ($self, $app, $conf) = @_;
8
+  
9
+  # Parent route
10
+  my $r = $conf->{route} || $app->routes;
11
+  
12
+  # Top directory
13
+  my $top_dir = $conf->{top_dir} || 'auto';
14
+  $top_dir =~ s#^/##;
15
+  $top_dir =~ s#/$##;
16
+  
17
+  # Condition
18
+  $app->routes->add_condition(__auto_route_plugin_file_exists => sub {
19
+    my ($r, $c, $captures, $pattern) = @_;
20
+    
21
+    my $path = $captures->{__auto_route_plugin_path};
22
+    $path = 'index' unless defined $path;
23
+    
24
+    return if $path =~ /\.\./;
25
+    
26
+    my $found;
27
+    for my $dir (@{$c->app->renderer->paths}) {
28
+      if (-f "$dir/$top_dir/$path.html.ep") {
29
+        return 1;
30
+      }
31
+    }
32
+    
33
+    return;
34
+  });
35
+  
36
+  # Index
37
+  $r->route('/')
38
+    ->over('__auto_route_plugin_file_exists')
39
+    ->to(cb => sub { shift->render("/$top_dir/index") });
40
+  
41
+  # Route
42
+  $r->route('/(*__auto_route_plugin_path)')
43
+    ->over('__auto_route_plugin_file_exists')
44
+    ->to(cb => sub {
45
+      my $c = shift;
46
+      
47
+      my $path = $c->stash('__auto_route_plugin_path');
48
+      
49
+      $c->render("/$top_dir/$path");
50
+    });
51
+}
52
+
53
+1;
54
+
55
+=head1 NAME
56
+
57
+Mojolicious::Plugin::AutoRoute - Mojolicious Plugin to create routes automatically
58
+
59
+=head1 CAUTION
60
+
61
+B<This is beta release. Implementation will be changed without warnings>. 
62
+
63
+=head1 SYNOPSIS
64
+
65
+  # Mojolicious
66
+  $self->plugin('AutoRoute');
67
+
68
+  # Mojolicious::Lite
69
+  plugin 'AutoRoute';
70
+
71
+=head1 DESCRIPTION
72
+
73
+L<Mojolicious::Plugin::AutoRoute> is a L<Mojolicious> plugin
74
+to create routes automatically.
75
+
76
+Routes corresponding to URL is created .
77
+
78
+  TEMPLATES                           ROUTES
79
+  templates/auto/index.html.ep        # /
80
+                /foo.html.ep          # /foo
81
+                /foo/bar.html.ep      # /foo/bar
82
+                /foo/bar/baz.html.ep  # /foo/bar/baz
83
+
84
+If you like C<PHP>, this plugin is very good.
85
+You only put file into C<auto> directory.
86
+
87
+=head1 OPTIONS
88
+
89
+=head2 C<route>
90
+
91
+  route => $route;
92
+
93
+You can set parent route if you need.
94
+This is L<Mojolicious::Routes> object.
95
+Default is C<$app->routes>.
96
+
97
+=head2 C<top_dir>
98
+
99
+  top_dir => 'myauto'
100
+
101
+Top directory. default is C<auto>.
102
+
103
+=head1 METHODS
104
+
105
+L<Mojolicious::Plugin::AutoRoute> inherits all methods from
106
+L<Mojolicious::Plugin> and implements the following new ones.
107
+
108
+=head2 C<register>
109
+
110
+  $plugin->register($app);
111
+
112
+Register plugin in L<Mojolicious> application.
113
+
114
+=head1 SEE ALSO
115
+
116
+L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
117
+
118
+=cut
templates/_admin.html.ep → templates/auto/_admin.html.ep
File renamed without changes.
templates/_admin/create.html.ep → templates/auto/_admin/create.html.ep
File renamed without changes.
templates/_admin/user/create.html.ep → templates/auto/_admin/user/create.html.ep
File renamed without changes.
templates/_admin/users.html.ep → templates/auto/_admin/users.html.ep
File renamed without changes.
templates/login.html.ep → templates/auto/_login.html.ep
File renamed without changes.
templates/_start.html.ep → templates/auto/_start.html.ep
File renamed without changes.
templates/home.html.ep → templates/auto/index.html.ep
File renamed without changes.