forked from sitepoint-editors/REST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
83 lines (63 loc) · 1.96 KB
/
bootstrap.php
File metadata and controls
83 lines (63 loc) · 1.96 KB
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
<?php
require_once dirname(__FILE__) . '/vendor/autoload.php';
use Slim\Slim;
use API\Application;
use API\Middleware\TokenOverBasicAuth;
use Flynsarmy\SlimMonolog\Log\MonologWriter;
// Init application mode
if (empty($_ENV['SLIM_MODE'])) {
$_ENV['SLIM_MODE'] = (getenv('SLIM_MODE'))
? getenv('SLIM_MODE') : 'development';
}
// Init and load configuration
$config = array();
$configFile = dirname(__FILE__) . '/share/config/'
. $_ENV['SLIM_MODE'] . '.php';
if (is_readable($configFile)) {
require_once $configFile;
} else {
require_once dirname(__FILE__) . '/share/config/default.php';
}
// Create Application
$app = new Application($config['app']);
// Only invoked if mode is "production"
$app->configureMode('production', function () use ($app) {
$app->config(array(
'log.enable' => true,
'log.level' => \Slim\Log::WARN,
'debug' => false
));
});
// Only invoked if mode is "development"
$app->configureMode('development', function () use ($app) {
$app->config(array(
'log.enable' => true,
'log.level' => \Slim\Log::DEBUG,
'debug' => false
));
});
// Get log writer
$log = $app->getLog();
// Init database
try {
if (!empty($config['db'])) {
\ORM::configure($config['db']['dsn']);
if (!empty($config['db']['username'])
&& !empty($config['db']['password'])) {
\ORM::configure('username', $config['db']['username']);
\ORM::configure('password', $config['db']['password']);
}
}
} catch (\PDOException $e) {
$log->error($e->getMessage());
}
// Cache Middleware (inner)
$app->add(new API\Middleware\Cache('/api/v1'));
// Parses JSON body
$app->add(new \Slim\Middleware\ContentTypes());
// Manage Rate Limit
$app->add(new API\Middleware\RateLimit('/api/v1'));
// JSON Middleware
$app->add(new API\Middleware\JSON('/api/v1'));
// Auth Middleware (outer)
$app->add(new API\Middleware\TokenOverBasicAuth(array('root' => '/api/v1')));