Showing 3 changed files with 162 additions and 36 deletions
+33 -1
lib/Gitprep.pm
... ...
@@ -5,8 +5,12 @@ our $VERSION = '0.01';
5 5
 
6 6
 use Mojo::Base 'Mojolicious';
7 7
 use Gitprep::Git;
8
+use DBIx::Custom;
9
+use Validator::Custom;
8 10
 
9 11
 has 'git';
12
+has 'dbi';
13
+has 'validator';
10 14
 
11 15
 sub startup {
12 16
   my $self = shift;
... ...
@@ -58,8 +62,11 @@ sub startup {
58 62
   # Home
59 63
   $r->get('/')->to('#home');
60 64
   
65
+  # Start
66
+  $r->any('/_start')->to('#start');
67
+  
61 68
   # Login
62
-  $r->get('/_login')->to('#login');
69
+  $r->any('/_login')->to('#login');
63 70
   
64 71
   # Admin
65 72
   {
... ...
@@ -112,6 +119,31 @@ sub startup {
112 119
   
113 120
   # File cache
114 121
   $git->search_projects;
122
+  
123
+  # DBI
124
+  my $db_file = $self->home->rel_file('db/gitprep.db');
125
+  my $dbi = DBIx::Custom->connect(
126
+    dsn => "dbi:SQLite:database=$db_file",
127
+    connector => 1,
128
+    option => {sqlite_unicode => 1}
129
+  );
130
+
131
+  eval {
132
+  # Create table
133
+    my $sql = <<"EOS";
134
+create table user (
135
+  row_id integer primary key autoincrement,
136
+  id not null unique,
137
+  config not null
138
+);
139
+EOS
140
+    $dbi->execute($sql);
141
+  };
142
+  $self->dbi($dbi);
143
+  
144
+  # Validator
145
+  my $validator = Validator::Custom->new;
146
+  $self->validator($validator);
115 147
 }
116 148
 
117 149
 1;
+5 -35
templates/main/login.html.ep
... ...
@@ -1,36 +1,6 @@
1
-%= stylesheet begin
2
-  .account_panel {
3
-    margin-top:50px;
4
-    margin-left:auto;
5
-    margin-right:auto;
6
-    width:500px;
7
-    background-color:#e6f1f6;
8
-    padding:30px;
9
-  }
10
-  .account {
11
-    margin-left:auto;
12
-    margin-right:auto;
13
-    width:300px;
14
-  }
15
-  .account_panel .atitle {
16
-    font-size:130%;
17
-    maring-top:30px;
18
-  }
19
-% end
20 1
 
21
-% layout 'common';
22
-%= include '/css/common';
23
-  <div class="account_panel">
24
-    <table class="account">
25
-      <tr class="auser">
26
-        <td>管理者: </td>
27
-        <td><input value="admin"></td>
28
-      </tr>
29
-      <tr class="apassword">
30
-        <td>パスワード: </td>
31
-        <td><input></td>
32
-      </tr>
33
-    </table>
34
-    <div><input type="submit" value="Start Git Prep!"></div>
35
-</div>
36
- 
2
+% if (flash('success')) {
3
+  Start up success!
4
+% }
5
+
6
+Login page
+124
templates/main/start.html.ep
... ...
@@ -0,0 +1,124 @@
1
+<%
2
+  use Mojo::JSON ();
3
+  use Encode ();
4
+  
5
+  my $op = param('op') || '';
6
+  my $state = 'start';
7
+  
8
+  my $errors;
9
+  if ($op eq 'create') {
10
+    $state = 'create';
11
+    
12
+    my $params = {
13
+      id => scalar param('id'),
14
+      password => scalar param('password'),
15
+      password2 => scalar param('password2')
16
+    };
17
+    my $id = param('id');
18
+    my $validator = $self->app->validator;
19
+    my $rule = [
20
+      id => [
21
+        ['not_blank' => 'Input admin user.'],
22
+        [{'regex' => qr/^[a-zA-Z0-9_]+$/} => 'Admin User contain invalid character.'],
23
+        [{'length' => {max => 20}} => 'Admin User is too long.']
24
+      ],
25
+      password => [
26
+        ['not_blank' => 'Input password.'],
27
+        ['ascii' => 'Password contain invalid character.'],
28
+        [{'length' => {max => 20}} => 'Password is too long.']
29
+      ],
30
+      {password_check => [qw/password password2/]}
31
+        => {copy => 0}
32
+        => [
33
+          ['duplication' => "Two password don't match"]
34
+        ]
35
+    ];
36
+    my $vresult = $validator->validate($params, $rule);
37
+    
38
+    if ($vresult->is_ok) {
39
+      my $valid_params = $vresult->data;
40
+      my $id = delete $valid_params->{id};
41
+      my $config = Encode::decode('UTF-8', Mojo::JSON->new->encode($valid_params));
42
+      
43
+      $self->app->dbi->insert({id => $id, config => $config}, table => 'user');
44
+      
45
+      $self->flash('success' => 1);
46
+      $self->redirect_to('/_login');
47
+    }
48
+    else {
49
+      $state = 'error';
50
+      $errors = $vresult->messages;
51
+    }
52
+  }
53
+%>
54
+
55
+%= stylesheet begin
56
+  .error {
57
+    margin-left:auto;
58
+    margin-right:auto;
59
+    width:300px;
60
+    color:red;
61
+    margin-bottom:10px;
62
+  }
63
+  .account_panel {
64
+    margin-top:50px;
65
+    margin-left:auto;
66
+    margin-right:auto;
67
+    width:500px;
68
+    background-color:#e6f1f6;
69
+    padding:30px;
70
+    border:1px solid #c5d5dd;
71
+    border-radius:7px;
72
+  }
73
+  .account {
74
+    margin-left:auto;
75
+    margin-right:auto;
76
+    width:300px;
77
+    margin-bottom:10px;
78
+  }
79
+  .account_panel .atitle {
80
+    font-size:130%;
81
+    maring-top:30px;
82
+  }
83
+  .submit {
84
+    text-align:center;
85
+  }
86
+  .submit input {
87
+    width:200px;
88
+    height:40px;
89
+  }
90
+% end
91
+
92
+% layout 'common';
93
+%= include '/css/common';
94
+
95
+% if ($state eq 'start' || $state eq 'error') {
96
+  <div class="account_panel">
97
+    % if ($state eq 'error') {
98
+      <div class="error">
99
+        % for my $error (@$errors) {
100
+          <div><%= $error %></div>
101
+        % }
102
+      </div>
103
+    % }
104
+    <form action="<%= url_for->query(op => 'create') %>" method="post">
105
+      <div class="account">
106
+        <table>
107
+          <tr class="auser">
108
+            <td>Admin User: </td>
109
+            <td><%= input_tag id => 'admin' %></td>
110
+          </tr>
111
+          <tr class="apassword">
112
+            <td>Password: </td>
113
+            <td><%= password_field 'password' %></td>
114
+          </tr>
115
+          <tr class="apassword">
116
+            <td>(Again): </td>
117
+            <td><%= password_field 'password2' %></td>
118
+          </tr>
119
+        </table>
120
+      </div>
121
+      <div class="submit"><input type="submit" value="Start Git Prep!"></div>
122
+    </form>
123
+  </div>
124
+% }