-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.php
More file actions
95 lines (81 loc) · 2.33 KB
/
app.php
File metadata and controls
95 lines (81 loc) · 2.33 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
<?php
/**
* Benchmark Server
* 7/8/15
*/
require( VENDOR . '/autoload.php' );
require( ROOT . '/config.php' );
define( 'VERSION', '1.2.1' );
$app = new \Slim\Slim();
/**
* Configuration array.
*/
$config = array(
'mysql' => array(
'host' => DB_HOST,
'user' => DB_USER,
'pass' => DB_PASS,
'dbname' => DB_NAME,
)
);
/**
* Base route for testing server.
*/
$app->get( '/', function() use( $app ) {
// Check if simple ping request.
if ( !is_null( $app->request->params( 'ping' ) ) ) {
$app->halt( 200, 'ok' );
} else {
$app->halt( 200, 'bandwidth-benchmark v. ' . VERSION );
}
});
/**
* API routes.
*/
$app->group( '/api', function() use( $app ) {
$app->response()->header('Access-Control-Allow-Origin', '*');
$app->response()->header('Access-Control-Allow-Headers', 'origin, content-type, accept');
$app->response()->header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
// OPTIONS requests.
$app->options( '/:type+', function() {});
/**
* Route for inserting bandwidth data to the database.
*/
$app->post( '/bandwidth', function() use( $app ) {
try {
// Payload.
$body = $app->request()->getBody();
$data = json_decode( $body );
// User agent.
$user_agent = $app->request()->getUserAgent();
// IP.
$ip = $_SERVER['REMOTE_ADDR'];
//
$referrer = $app->request()->getReferrer();
// Insert to the database.
$sqlquery = "INSERT INTO bandwidth_records (js, swf, user_agent, ip, referrer) "
. "VALUES(:js_bandwidth, :swf_bandwidth, :user_agent, :ip, :referrer)";
$sql = $app->db->prepare( $sqlquery );
$sql->bindParam( ':js_bandwidth', $data->js, PDO::PARAM_STR );
$sql->bindParam( ':swf_bandwidth', $data->swf, PDO::PARAM_STR );
$sql->bindParam( ':user_agent', $user_agent, PDO::PARAM_STR );
$sql->bindParam( ':ip', $ip, PDO::PARAM_STR );
$sql->bindParam( ':referrer', $referrer, PDO::PARAM_STR );
$sql->execute();
} catch( \Exception $e ) {
error_log( 'Error inserting bw record to DB: ' . $e->getMessage() );
$app->halt( 500 );
}
$app->halt( 200 , 'true');
})->name('bandwidth-api');
});
/**
* The Database object.
*/
$app->container->singleton( 'db', function() use ($app, $config) {
$mysql = $config['mysql'];
$db = new PDO("mysql:host={$mysql['host']};dbname={$mysql['dbname']}", $mysql['user'], $mysql['pass'] );
return $db;
});
// Run the app.
$app->run();