-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbootstrap.php
142 lines (108 loc) · 4.16 KB
/
bootstrap.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
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
<?php
use CropTool\Controllers\AuthController;
use CropTool\Controllers\FileController;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;
use CropTool\WikiPageService;
use \DI\Bridge\Slim\Bridge as App;
if (!defined('ROOT_PATH')) {
define('ROOT_PATH', __DIR__);
}
/**********************************************************************************
* PHP config
*/
error_reporting(E_ALL);
ini_set('display_errors', 'off');
ini_set('display_startup_errors', 'off');
ini_set('memory_limit', '512M');
/**********************************************************************************
* The array_get method from Laravel
*
* @param array $data
* @param string $key
* @param string $default
*
* @return mixed
*/
if (! function_exists('array_get')) {
function array_get($data, $key, $default = null) {
if (!is_array($data)) {
return $default;
}
return isset($data[$key]) ? $data[$key] : $default;
}
}
/**********************************************************************************
* Middleware
*/
$authMiddleware = function (Request $request, RequestHandler $handler) {
$user = $this->get(\CropTool\Auth\UserService::class);
$auth = $this->get(\CropTool\Auth\AuthServiceInterface::class);
if (!$user->loggedin()) {
$response = new Response();
$response->getBody()->write((string)json_encode([
'error' => 'Unauthorized',
'messages' => $auth->getMessages(),
]));
return $response->withStatus(401);
}
$response = $handler->handle($request);
return $response;
};
$pageMiddleware = function (Request $request, RequestHandler $handler) {
// Middleware that defines the current WikiPage
$requestParams = $request->getMethod() === 'GET' ? $request->getQueryParams() : $request->getParsedBody();
if ( !array_key_exists( 'title', $requestParams ) ) {
$response = new Response();
$response->getBody()->write((string)json_encode([
'error' => 'Invalid request',
'messages' => 'No title parameter found',
]));
return $response->withStatus(500);
}
$response = $handler->handle($request);
$title = $requestParams['title'];
$pageService = $this->get(WikiPageService::class);
$request->withAttribute('page', $pageService->getForTitle( $title ));
return $response;
};
/**********************************************************************************
* Init app
*/
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(ROOT_PATH . '/container.php');
$container = $builder->build();
$app = App::create($container);
$app->addErrorMiddleware(true, true, true);
$app->add(\CropTool\SessionInterface::class);
$app->addBodyParsingMiddleware();
$app->get('/api/ping', function ($request, $response) {
$response->getBody()->write('pong');
return $response->withStatus(200);
});
$app->get('/api/server-cleanup', function ($request, $response) {
exec( ROOT_PATH . '/scripts/cleanup.sh &');
$response->getBody()->write('STARTED');
return $response->withStatus(200);
});
/**********************************************************************************
* Auth routes
*/
$app->group('/api/auth', function (Slim\Routing\RouteCollectorProxy $app) use ($authMiddleware) {
$app->get('/login', [AuthController::class, 'login']);
$app->get('/callback', [AuthController::class, 'authCallback']);
$app->get('/logout', [AuthController::class, 'logout']);
$app->get('/user', [AuthController::class, 'getUser'])->add($authMiddleware);
});
/**********************************************************************************
* Page routes
*/
$app->group('/api/file', function ( Slim\Routing\RouteCollectorProxy $app) {
$app->get('/exists', [FileController::class, 'exists']);
$app->get('/info', [FileController::class, 'info']);
$app->get('/autodetect', [FileController::class, 'autodetect']);
$app->get('/crop', [FileController::class, 'crop']);
$app->post('/publish', [FileController::class, 'publish']);
})->add($authMiddleware)->add($pageMiddleware);
return $app;