-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.php
More file actions
144 lines (119 loc) · 4.26 KB
/
Copy pathbootstrap.php
File metadata and controls
144 lines (119 loc) · 4.26 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
date_default_timezone_set('America/Phoenix');
$startTime = microtime(true);
$startMemory = memory_get_usage();
require_once __DIR__ . '/vendor/autoload.php';
use Aura\Di\ContainerBuilder;
use Aura\Sql\ExtendedPdo;
use AvalancheDevelopment\Talus\Talus;
// load the config for the application
$config_path = __DIR__ . '/config.json';
$handle = @fopen($config_path, 'r');
if ($handle === false) {
throw new RuntimeException("Could not load config");
}
$config = fread($handle, filesize($config_path));
fclose($handle);
$config = json_decode($config);
$last_json_error = json_last_error();
if ($last_json_error !== JSON_ERROR_NONE) {
throw new RuntimeException("Could not parse config - JSON error detected");
}
$builder = new ContainerBuilder();
$di = $builder->newInstance($builder::AUTO_RESOLVE);
// set up db and models
$di->set('dbal', $di->lazyNew(
'Aura\Sql\ExtendedPdo',
(array) $config->database
));
$di->types['Aura\Sql\ExtendedPdo'] = $di->lazyGet('dbal');
$di->set('commentModel', $di->lazyNew('Jacobemerick\CommentService\Model\Comment'));
$di->set('commentBodyModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentBody'));
$di->set('commentDomainModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentDomain'));
$di->set('commentLocationModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentLocation'));
$di->set('commentPathModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentPath'));
$di->set('commentRequestModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentRequest'));
$di->set('commentThreadModel', $di->lazyNew('Jacobemerick\CommentService\Model\CommentThread'));
$di->set('commenterModel', $di->lazyNew('Jacobemerick\CommentService\Model\Commenter'));
// set up serializers
$di->set('commentSerializer', $di->lazyNew('Jacobemerick\CommentService\Serializer\Comment'));
$di->set('commenterSerializer', $di->lazyNew('Jacobemerick\CommentService\Serializer\Commenter'));
// set up notification handler
$di->set('notificationHandler', $di->lazyNew(
'Jacobemerick\CommentService\Helper\NotificationHandler',
[
'mail' => $di->lazyGet('mail'),
'commenterModel' => $di->lazyGet('commenterModel'),
]
));
// set up logger
$di->set('logger', $di->lazyNew(
'Monolog\Logger',
[
'name' => 'default',
],
[
'pushHandler' => $di->lazyNew(
'Monolog\Handler\StreamHandler',
[
'stream' => __DIR__ . '/logs/default.log',
'level' => Monolog\Logger::DEBUG,
]
),
]
));
// set up mailer
$di->set('mail', $di->lazyNew(
'Jacobemerick\Archangel\Archangel',
[],
[
'setLogger' => $di->lazyGet('logger'),
]
));
// global time object
$di->set('datetime', new DateTime(
'now',
new DateTimeZone('America/Phoenix')
));
// set up swagger
$handle = fopen(__DIR__ . '/swagger.json', 'r');
$swagger = '';
while (!feof($handle)) {
$swagger .= fread($handle, 8192);
}
fclose($handle);
$swagger = json_decode($swagger, true);
if (json_last_error() !== JSON_ERROR_NONE) {
var_dump('oh noes the swagz is badz');
exit;
}
$talus = new Talus($swagger);
// controllers
use Jacobemerick\CommentService\Controller;
$talus->addController('createComment', function ($req, $res) use ($di) {
return (new Controller\Comment($di))->createComment($req, $res);
});
$talus->addController('getComment', function ($req, $res) use ($di) {
return (new Controller\Comment($di))->getComment($req, $res);
});
$talus->addController('getComments', function ($req, $res) use ($di) {
return (new Controller\Comment($di))->getComments($req, $res);
});
$talus->addController('getCommenter', function ($req, $res) use ($di) {
return (new Controller\Commenter($di))->getCommenter($req, $res);
});
$talus->addController('getCommenters', function ($req, $res) use ($di) {
return (new Controller\Commenter($di))->getCommenters($req, $res);
});
// middleware
use Jacobemerick\CommentService\Middleware;
$talus->addMiddleware(new Middleware\Authentication(
$config->auth->username,
$config->auth->password
));
$talus->run();
$di->get('logger')->addInfo('Runtime stats', [
'request' => $_SERVER['REQUEST_URI'],
'time' => (microtime(true) - $startTime),
'memory' => (memory_get_usage() - $startMemory),
]);