<?php  

require_once "models/connection.php";

$routesArray = explode("/",$_SERVER['REQUEST_URI']);
$routesArray = array_filter($routesArray);

if(count($routesArray) == 0){

	$json = array(
		'status'=> 404,
		'result'=> 'Not found'
	);

	echo json_encode($json,http_response_code($json['status']));

	return;
}

if(count($routesArray) == 1 && isset($_SERVER['REQUEST_METHOD'])){

	$table = explode("?",$routesArray[1])[0];
	
	$authorization = getallheaders();

	$apikey = Connection::apikey();
	
	if(!isset($authorization['Apikey']) || $authorization['Apikey'] != $apikey){

		$json = array(

			'status'=> 400,
			'result'=> 'You are not authorized to make this request'
		);

		echo json_encode($json,http_response_code($json['status']));
		return;
	}


	if ($_SERVER['REQUEST_METHOD'] == "GET"){

		include "services/get.php";

	}

	if ($_SERVER['REQUEST_METHOD'] == "POST"){

		include "services/post.php";

	}

	if ($_SERVER['REQUEST_METHOD'] == "PUT"){

		include "services/put.php";

	}

}

?>