-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
89 lines (72 loc) · 2.22 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
<?php
/**
* index.php
* prime numbers api
* route handler for api
* purdy
* august 2016
*/
use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\Http\Response;
use Phalcon\DI\FactoryDefault;
// Use Loader() to autoloadmodel
$loader = new Loader();
$loader->registerDirs(
array(
__DIR__ . '/models/'
)
)->register();
$di = new FactoryDefault();
$app = new Micro($di);
/**
* for quick and dirty api, enabling COORS
* for a secure api swap this out with a proper secure call such as oauth token exhange.
*/
$response = $app->response;
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setHeader('Access-Control-Allow-Headers', 'X-Requested-With');
$response->sendHeaders();
$app->get('/preflight', function() use ($app) {
$content_type = 'application/json';
$status = 200;
$description = 'OK';
$response = $app->response;
$status_header = 'HTTP/1.1 ' . $status . ' ' . $description;
$response->setRawHeader($status_header);
$response->setStatusCode($status, $description);
$response->setContentType($content_type, 'UTF-8');
$response->setHeader('Access-Control-Allow-Origin', '*');
$response->setHeader('Access-Control-Allow-Headers', 'X-Requested-With');
$response->setHeader("Access-Control-Allow-Headers: Authorization");
$response->setHeader('Content-type: ' . $content_type);
$response->sendHeaders();
});
//routing here
$app->get('/api/prime/{start:[0-9]+}/{end:[0-9]+}', function($start, $end) use ($app) {
$clPrime = new Prime();
$response = new Response();
try {
$primes = $clPrime->atkins($start,$end);
} catch (exception $e) {
$response->setJsonContent(
array(
'status' => $e->getCode(),
'data' => $e->getMessage()
)
);
return $response;
}
$response->setJsonContent(
array(
'status' => 200,
'data' => $primes
)
);
return $response;
});
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
echo 'This is crazy, but this page was not found!';
});
$app->handle();