diff --git a/api/.env b/api/.env index 4840d6c..ef4838e 100644 --- a/api/.env +++ b/api/.env @@ -14,7 +14,7 @@ # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration MERCURE_SUBSCRIBE_URL=http://localhost:1337/hub -VARNISH_URL=http://cache-proxy +#VARNISH_URL=http://cache-proxy ###> symfony/framework-bundle ### APP_ENV=dev @@ -31,7 +31,7 @@ DATABASE_URL=postgres://api-platform:!ChangeMe!@db/api ###< doctrine/doctrine-bundle ### ###> nelmio/cors-bundle ### -CORS_ALLOW_ORIGIN=^https?://localhost(:[0-9]+)?$ +CORS_ALLOW_ORIGIN=^https?://.*$ ###< nelmio/cors-bundle ### ###> symfony/mercure-bundle ### diff --git a/api/Readme.md b/api/Readme.md new file mode 100644 index 0000000..69219af --- /dev/null +++ b/api/Readme.md @@ -0,0 +1,7 @@ +Enable prod mode: `docker-compose exec php composer dump-env prod` + +Disable prod mode: delete `.env.local.php` + +Clear Prod cache `docker-compose exec php bin/console cache:clear --env=prod` + +Swagger: https://api-platform.com/docs/core/swagger/#using-the-openapi-and-swagger-contexts diff --git a/api/config/packages/api_platform.yaml b/api/config/packages/api_platform.yaml index 7be9e1c..d273239 100644 --- a/api/config/packages/api_platform.yaml +++ b/api/config/packages/api_platform.yaml @@ -9,16 +9,12 @@ api_platform: mapping: paths: ['%kernel.project_dir%/src/Entity'] title: Hello API Platform + description: สวัสดีเพื่อนๆ version: 1.0.0 - # Varnish integration, remove if unwanted - http_cache: - invalidation: - enabled: true - varnish_urls: ['%env(VARNISH_URL)%'] - max_age: 0 - shared_max_age: 3600 - vary: ['Content-Type', 'Authorization'] - public: true - # Mercure integration, remove if unwanted - mercure: - hub_url: '%env(MERCURE_SUBSCRIBE_URL)%' + show_webby: false + formats: + json: ['application/json'] + xml: ['application/xml', 'text/xml'] + yaml: ['application/x-yaml'] + csv: ['text/csv'] + html: ['text/html'] diff --git a/api/config/packages/nelmio_cors.yaml b/api/config/packages/nelmio_cors.yaml index 6f202c2..b5b7a44 100644 --- a/api/config/packages/nelmio_cors.yaml +++ b/api/config/packages/nelmio_cors.yaml @@ -2,7 +2,7 @@ nelmio_cors: defaults: origin_regex: true allow_origin: ['%env(CORS_ALLOW_ORIGIN)%'] - allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE'] + allow_methods: ['GET', 'OPTIONS'] allow_headers: ['Content-Type', 'Authorization'] expose_headers: ['Link'] max_age: 3600 diff --git a/api/src/DataProvider/CircleDataProvider.php b/api/src/DataProvider/CircleDataProvider.php new file mode 100644 index 0000000..bf22605 --- /dev/null +++ b/api/src/DataProvider/CircleDataProvider.php @@ -0,0 +1,71 @@ +requestStack = $requestStack; + } + + + public function supports(string $resourceClass, string $operationName = null, array $context = []): bool + { + return Circle::class === $resourceClass; + } + + public function getCollection(string $resourceClass, string $operationName = null): \Generator + { + $query = $this->requestStack->getCurrentRequest()->query; + $ids = $query->get('ids'); + if ($ids !== null) { + if (!preg_match('/^\d+(,\d+)*$/', $ids)) { + throw new InvalidValueException('ids must be comma-seperated integers', 400); + //BadQueryStringException('ids must be comma-seperated integer', 400); + } + $ids = explode(',', $ids); + foreach ($ids as $id) { + yield new Circle((int)$id, $operationName, 'bla'); + } + } + else { + for($i = 0; $i< 10; $i++) { + yield new Circle($i, $operationName, json_encode($ids)); + } + } + // Returning array works too + // Return empty array if no data + + } + + public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) + { + //return null; for 404 + $tmp = new Circle($id, is_string($id)? 'true' : 'false', 'a01'); + $tmp->creations = [new Creation(55,'55'), new Creation(66,'66')]; + return $tmp; + } + + public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null) + { + return $context['property']; + // TODO: Implement getSubresource() method. + } +} diff --git a/api/src/Entity/Circle.php b/api/src/Entity/Circle.php new file mode 100644 index 0000000..292f76e --- /dev/null +++ b/api/src/Entity/Circle.php @@ -0,0 +1,131 @@ +id = $id; + $this->name = $name; + $this->booth = $booth; + } + + /** + * @return integer + */ + public function getId(): int + { + return $this->id; + } +} diff --git a/api/src/Entity/Creation.php b/api/src/Entity/Creation.php new file mode 100644 index 0000000..aa22afd --- /dev/null +++ b/api/src/Entity/Creation.php @@ -0,0 +1,62 @@ +id = $id; + $this->name = $name; + } + + /** + * @return integer + */ + public function getId(): int + { + return $this->id; + } +} diff --git a/api/src/Entity/Greeting.php b/api/src/Entity/Greeting.php deleted file mode 100644 index cdfd3e9..0000000 --- a/api/src/Entity/Greeting.php +++ /dev/null @@ -1,38 +0,0 @@ -id; - } -} diff --git a/api/templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig b/api/templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig new file mode 100644 index 0000000..2afd0fb --- /dev/null +++ b/api/templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig @@ -0,0 +1,85 @@ + + + + + {% if title %}{{ title }} - {% endif %}API Platform + + {% block stylesheet %} + + + + {% endblock %} + + {# json_encode(65) is for JSON_UNESCAPED_SLASHES|JSON_HEX_TAG to avoid JS XSS #} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +{% if showWebby %} +
+
+{% endif %} + +
+ +
+
+
+ Other API docs: + {% set active_ui = app.request.get('ui', 'swagger_ui') %} + {% if swaggerUiEnabled and active_ui != 'swagger_ui' %}Swagger UI{% endif %} + {% if reDocEnabled and active_ui != 're_doc' %}ReDoc{% endif %} +
+
+
+ +{% block javascript %} +{% if (reDocEnabled and not swaggerUiEnabled) or (reDocEnabled and 're_doc' == active_ui) %} + + +{% elseif (swaggerUiEnabled) %} + + + +{% endif %} +{% endblock %} + + +