Skip to content

Commit

Permalink
Arquivos base (#1)
Browse files Browse the repository at this point in the history
base files;
  • Loading branch information
leandrohago authored Apr 12, 2023
1 parent e00e459 commit 0ec3486
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./.idea
.idea/
8 changes: 8 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function buscaCep()
{
echo json_encode([
'data' => 'do something',
]);
}
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

header('Content-Type: application/json');

include 'routes.php';
46 changes: 46 additions & 0 deletions routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

include 'functions.php';

$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];

$routes = [
'/cep' => [
'method' => 'GET',
'action' => 'buscaCep',
],
];

function checkRouteExist($routes, $uri)
{
if (!array_key_exists($uri, $routes)) {
http_response_code(404);

echo json_encode([
'data' => 'Not Found',
]);

die();
}
}

function checkMethod($routes, $uri, $method)
{
$routeMethod = $routes[$uri];

if ($routeMethod['method'] !== $method) {
http_response_code(405);

echo json_encode([
'data' => 'Method Not Allowed',
]);

die();
}
}

checkRouteExist($routes, $uri);
checkMethod($routes, $uri, $method);

call_user_func($routes[$uri]['action']);

0 comments on commit 0ec3486

Please sign in to comment.