-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pavel Janda
committed
Jul 3, 2017
1 parent
58952ca
commit 461a504
Showing
17 changed files
with
1,549 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Order Allow,Deny | ||
Deny from all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Http\ApiResponseFormatter; | ||
use Nette\Application\IPresenter; | ||
|
||
abstract class AbstractController implements IPresenter | ||
{ | ||
|
||
/** | ||
* @var ApiResponseFormatter | ||
*/ | ||
protected $apiResponseFormatter; | ||
|
||
|
||
public function __construct(ApiResponseFormatter $apiResponseFormatter) | ||
{ | ||
$this->apiResponseFormatter = $apiResponseFormatter; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Http\ApiResponse; | ||
use Nette\Application\BadRequestException; | ||
use Nette\Application\IResponse; | ||
use Nette\Application\Request; | ||
|
||
final class ErrorController extends AbstractController | ||
{ | ||
|
||
public function run(Request $request): IResponse | ||
{ | ||
$exception = $request->getParameter('exception'); | ||
|
||
/** | ||
* @todo Log exception | ||
*/ | ||
|
||
return new ApiResponse($this->apiResponseFormatter->formatException($exception)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Http\ApiResponse; | ||
use Nette\Application\IResponse; | ||
use Nette\Application\Request; | ||
use Ublaboo\ApiRouter\ApiRoute; | ||
|
||
/** | ||
* API for logging users in | ||
* | ||
* @ApiRoute( | ||
* "/api/login", | ||
* methods={ | ||
* "POST"="run" | ||
* }, | ||
* presenter="Login", | ||
* format="json" | ||
* ) | ||
*/ | ||
final class LoginController extends AbstractController | ||
{ | ||
|
||
public function run(Request $request): IResponse | ||
{ | ||
return new ApiResponse($this->apiResponseFormatter->formatMessage('Hello')); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http; | ||
|
||
use Nette\Application\Responses\JsonResponse; | ||
|
||
final class ApiResponse extends JsonResponse | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http; | ||
|
||
use Nette\Application\Responses\JsonResponse; | ||
|
||
final class ApiResponseFormatter | ||
{ | ||
|
||
public function formatMessage(string $message): array | ||
{ | ||
return [ | ||
'status' => 'ok', | ||
'payload' => [ | ||
'message' => $message | ||
] | ||
]; | ||
} | ||
|
||
|
||
public function formatPayload(array $payload): array | ||
{ | ||
return [ | ||
'status' => 'ok', | ||
'payload' => $payload | ||
]; | ||
} | ||
|
||
|
||
public function formatException(\Exception $e): array | ||
{ | ||
return [ | ||
'status' => 'error', | ||
'code' => $e->getCode(), | ||
'message' => $e->getMessage() | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Routing; | ||
|
||
use Nette\Application\Routers\RouteList; | ||
|
||
final class RouterFactory | ||
{ | ||
|
||
public function create(): RouteList | ||
{ | ||
$router = new RouteList; | ||
|
||
return $router; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$configurator = new Nette\Configurator; | ||
|
||
//$configurator->setDebugMode(FALSE); | ||
$configurator->enableTracy(__DIR__ . '/../log'); | ||
|
||
$configurator->setTimeZone('Europe/Prague'); | ||
$configurator->setTempDirectory(__DIR__ . '/../temp'); | ||
|
||
$configurator->addConfig(__DIR__ . '/config/config.neon'); | ||
|
||
if (file_exists(__DIR__ . '/config/config.local.neon')) { | ||
$configurator->addConfig(__DIR__ . '/config/config.local.neon'); | ||
} | ||
|
||
$container = $configurator->createContainer(); | ||
|
||
return $container; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
extensions: | ||
apiRouter: Ublaboo\ApiRouter\DI\ApiRouterExtension | ||
|
||
|
||
parameters: | ||
|
||
|
||
application: | ||
errorPresenter: Error | ||
scanDirs: false | ||
mapping: | ||
*: App\Controllers\*Controller | ||
|
||
|
||
session: | ||
expiration: 14 days | ||
|
||
|
||
services: | ||
- App\Http\ApiResponseFormatter | ||
- App\Routing\RouterFactory | ||
router: @App\Routing\RouterFactory::create | ||
|
||
# Api endpoints | ||
# | ||
- App\Controllers\LoginController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "ublaboo/api-router-project", | ||
"type": "project", | ||
"description": "Example starter project for ublaboo/api-router", | ||
"keywords": ["rest", "api", "routes", "nette", "router", "restapi", "restrouter", "routing", "route", "starter", "project", "example"], | ||
"homepage": "https://github.com/ublaboo/api-router-project", | ||
"license": ["MIT"], | ||
"support": { | ||
"issues": "https://github.com/ublaboo/api-router-project/issues" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Pavel Janda", | ||
"homepage": "http://paveljanda.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">= 7.1", | ||
"nette/application": "^2.4", | ||
"nette/bootstrap": "^2.4.2", | ||
"nette/caching": "^2.5", | ||
"nette/di": "^2.4", | ||
"nette/http": "^2.4", | ||
"nette/security": "^2.4", | ||
"nette/utils": "^2.4", | ||
"tracy/tracy": "^2.4", | ||
"ublaboo/api-router": "^1.2" | ||
}, | ||
"require-dev": { | ||
"nette/tester": "^1.6" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "app" | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
Oops, something went wrong.