forked from MacEncrypted/phalcon-rest
/
index.php
114 lines (91 loc) · 2.73 KB
/
index.php
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
<?php
use Phalcon\Mvc\Micro as Micro;
use \Phalcon\Loader as Loader;
use Phalcon\Db\Adapter\Pdo\Mysql as MysqlAdapter;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\Mvc\Micro\Collection as MicroCollection;
// Setup loader
$loader = new Loader();
$loader->registerDirs(array(
__DIR__ . '/app/models/',
__DIR__ . '/app/controllers/'
))->register();
// Read the configuration
$config = new ConfigIni(__DIR__ . '/config/config.ini');
// Start Micro
$app = new Micro();
// Setup the database service
$app['db'] = function() use ($config) {
return new MysqlAdapter(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname
));
};
// Include controllers
$app['controllers'] = function() {
return [
'core' => true,
'users' => true,
'messages' => true
];
};
// Authentication
$app['auth'] = function() use ($app, $config) {
$auth = array();
$authorization = $app->request->getHeader("AUTHORIZATION");
if ($authorization) {
$cut = str_replace('Basic ', '', $authorization);
$creds = explode(':', base64_decode($cut));
$auth['login'] = $creds[0];
$auth['password'] = $creds[1];
} else {
$auth['login'] = null;
$auth['password'] = null;
}
$usr = new Users();
$auth['id'] = $usr->getUserId($auth['login'], $auth['password']);
return $auth;
};
// CoreController
if ($app['controllers']['core']) {
$core = new MicroCollection();
// Set the handler & prefix
$core->setHandler(new CoreController($app));
$core->setPrefix('/');
// Set routers
$core->get('/', 'index');
$app->mount($core);
}
// MessagesController
if ($app['controllers']['messages']) {
$messages = new MicroCollection();
// Set the handler & prefix
$messages->setHandler(new MessagesController($app));
$messages->setPrefix('/messages');
// Set routers
$messages->post('/', 'create');
$messages->get('/{id_sender}/{id_receiver}', 'stream');
$messages->get('/', 'inbox');
$app->mount($messages);
}
// UsersController
if ($app['controllers']['users']) {
$users = new MicroCollection();
// Set the handler & prefix
$users->setHandler(new UsersController($app));
$users->setPrefix('/users');
// Set routers
$users->post('/', 'create');
$users->put('/{id}', 'update');
$users->delete('/{id}', 'delete');
$users->get('/', 'preview');
$users->get('/{id}', 'info');
$app->mount($users);
}
// Not Found
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
});
$app->handle();