-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
83 lines (66 loc) · 1.78 KB
/
index.php
File metadata and controls
83 lines (66 loc) · 1.78 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
use \Slim\App as App;
require_once 'vendor/autoload.php';
session_start();
// Environment Variables in .env file
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
// Configuration
$config = [
'db' => [
'host' => getenv('DB_HOST'),
'user' => getenv('DB_USER'),
'pass' => getenv('DB_PASS'),
'dbname' => getenv('DB_NAME'),
],
'displayErrorDetails' => true,
];
$app = new App(['settings' => $config]);
$container = $app->getContainer();
$container['db'] = function ($c) {
$db = $c['settings']['db'];
try {
$pdo = new PDO(
'mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'],
$db['user'],
$db['pass'],
[]
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Caught PDOException: ';
die($e->getMessage());
}
return $pdo;
};
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
// Request Headers
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
});
// Example route
$app->get('/', function ($request, $response, $args) {
$data = [
'foo' => 'bar',
'fizz' => 'buzz',
'args' => $args,
];
return $response->withJson($data);
});
// import routes
require './routes.php';
// 404
$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
return $res->withJson([
'code' => 404,
'message' => 'Not Found',
], 404);
});
// run application
$app->run();