Skip to content

Excel Editor API - Study of a complete application, with a SPA (Angular) and an API (Symfony), which allows users to connect, and to be able, according to their rights, to import Excel data and modify them online.

License

Notifications You must be signed in to change notification settings

jprivet-dev/excel-editor-api

Repository files navigation

Excel Editor API

1. Presentation

1.1. SPA, API & Insomnia

Study of a complete application, with a SPA (Angular) and an API (Symfony), which allows users to connect, and to be able, according to their rights, to import Excel data and modify them online.

Excel Editor SPA

https://github.com/jprivet-dev/excel-editor-spa

Excel Editor API

https://github.com/jprivet-dev/excel-editor-api

Excel Editor Insomnia

https://github.com/jprivet-dev/excel-editor-insomnia

1.2. Diagrams

1.2.1. Global architecture

excel editor architecture diagram

2. Prerequisites

2.1. Docker Compose CLI

Be sure to install the latest version of Docker Compose CLI plugin.

3. Installation

3.1. To the very first cloning of the project

  1. $ git clone git@github.com:jprivet-dev/excel-editor-api.

  2. $ cd excel-editor-api.

  3. $ make install: This command will execute the following commands:

    • $ make build: Build or rebuild fresh images if necessary.

    • $ make detach: Create and start containers in detached mode (no logs).

    • $ make generate_keypair: Generate the SSL keys for the JWT authentication.

    • $ make fixtures: Create an admin & an user profils.

    • $ make logs: Show live logs.

  4. Open your browser on https://localhost/api/doc and Accept the auto-generated TLS certificate.

📎

With Excel Editor API, you can use…​

3.1.1. Accept the auto-generated TLS certificate

📎
The following steps are based on the Stackoverflow answer Getting Chrome to accept self-signed localhost certificate.
  1. On the page https://localhost/api/doc, click on lock > Certificate Information, then click on Untrusted certificate:

    chrome 1 view certificate
  2. In the new dialog, click on the Details tab, and Export button:

    chrome 2 certificate details
  3. You will export the localhost.p7c file. Click on Save button:

    chrome 3 export p7c file
  4. Go on chrome://settings/certificates then on the Authorities tab, click on Import button and choose the localhost.p7c file.

    chrome 4 import p7c file
  5. Check all boxes and click on OK button:

    chrome 5 check all
  6. You can see org-Caddy Local Authority in the list:

    chrome 6 caddy local authority
  7. Restart Chrome (chrome://restart)

    chrome 7 ok

3.2. The following times

  1. Just launch the project with $ make start command.

  2. Open your browser on https://localhost/api/doc.

💡
  • $ make stop: Stop and remove containers, networks (alias: $ make down).

  • $ make: See all available make commands.

4. Screenshot

4.1. Nelmio documentation

nelmio api doc

5. Upload Excel files

The Excel files are uploaded and renamed (with a unique indentifier) in the uploads folder.

💡
You can test and upload the Excel files in the data folder.

To purge the uploads folder: $ rm uploads/*.xlsx.

6. Main technical constraints for the study

7. API documentation with Nelmio

Open https://localhost/api/doc to see the API documentation.

8. Style Guide

8.1. JSON naming convention

That project (API & SPA) use the camelCase format for the property names of JSON responses:

{
  "thisPropertyIsAnIdentifier": "identifier value"
}

8.2. Exceptions: return errors into JSON format

In this project, I will not use a listener or subscriber to force all errors into JSON format. As for example with the following subscriber:

namespace App\EventSubscriber;

use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\SerializerInterface;

class ExceptionSubscriber implements EventSubscriberInterface
{
    public function __construct(private SerializerInterface $serializer)
    {
    }

    public function onKernelException(ExceptionEvent $event): void
    {
        $response = new JsonResponse();

        $exception = $event->getThrowable();
        $flattenException = FlattenException::createFromThrowable($exception);
        $data = $this->serializer->normalize($flattenException);
        $response->setData($data);

        // HttpExceptionInterface is a special type of exception that
        // holds status code and header details
        if ($exception instanceof HttpExceptionInterface) {
            $response->setStatusCode($exception->getStatusCode());
            $response->headers->replace($exception->getHeaders());
        } else {
            $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        $event->setResponse($response);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::EXCEPTION => 'onKernelException',
        ];
    }
}

Instead, I’ll let the user choose the format of the response (HTML, JSON, XML or other) by properly using the Accept header request:

$ curl https://localhost/api/data --header 'Accept: application/json'

In the SerializerErrorRenderer::render() of Symfony, a FlattenException is created from the exception and is passed to the serializer, with the format from the request. Accept: application/json change the "preferred format" on the request to JSON.

💡
In addition, the JSON error will be automatically filled in depending on the environment (dev or prod).

9. PHPStorm configuration

The following configuration are provided for PHPStorm 2022.3.2

9.1. Docker Compose-based remote PHP interpreter

  1. Go on Settings (Ctrl+Alt+S) > PHP.

  2. In the PHP Settings dialog, click on …​ Browse button next to the CLI Interpreter list.

  3. In the CLI Interpreters dialog that opens, click on + button and select From Docker, Vagrant, VM, WSL, Remote…​.

    phpstorm 2022.3.2 settings php select cli interpreter
  4. In the Configure Remote PHP Interpreter dialog that opens, select "Docker" :

    • Server: Docker

    • Image name: excel-editor-api-php:latest

    • PHP interpreter path: php

    • And click on OK button.

      phpstorm 2022.3.2 settings php configure remote php interpreterpng
  5. In the CLI Interpreters dialog, click on OK button.

    phpstorm 2022.3.2 settings php cli interpreters
  6. In the Settings dialog, click on OK or Apply button to validate all.

phpstorm 2022.3.2 settings php

I have a new problem, with PHPStorm 2023.3.1 and 2023.3.2, I did not have on my previous computer: after the configuration, PHP_CodeSniffer & PHP Mess Detector do not work with the Remote PHP Interpreter.

phpstorm 2023.3.2 settings php interpreter error

Search for a solution in progress…​

9.2. PHP_CodeSniffer script associated with a PHP interpreter

9.2.1. Configuration

  1. Go on Settings (Ctrl+Alt+S) > PHP > Quality Tools.

  2. Expand the PHP_CodeSniffer area and switch ON the tool.

  3. In Configuration, choose By default project interpreter.

  4. In Coding standard, select Custom and choose the phpcs.xml file of this repository.

  5. After the configuration of PHP_CodeSniffer, PHPStorm will highlight the problematic lines in the files and can run PHP CS fixer.

  6. In the Settings dialog, click on OK or Apply button to validate all.

phpstorm 2022.3.2 settings php codesniffer

9.2.2. How is it installed?

Include a dependency for squizlabs/php_codesniffer in the composer.json file:

{
    "require-dev": {
        "squizlabs/php_codesniffer": "3.*"
    }
}

And update all:

$ make composer c=update    # with Makefile
# OR
$ composer update           # with .bash_aliases

9.3. PHP Mess Detector script associated with a PHP interpreter

9.3.1. Configuration

  1. Go on Settings (Ctrl+Alt+S) > PHP > Quality Tools.

  2. Expand the PHP Mess Detector area and switch ON the tool.

  3. In Configuration, choose By default project interpreter.

  4. In Custom rulesets, click on + button and choose the phpmd.xml file of this repository.

  5. In the Settings dialog, click on OK or Apply button to validate all.

phpstorm 2022.3.2 settings php mess detector

9.3.2. How is it installed?

$ composer require --dev phpmd/phpmd

9.4. PHP Unit associated with a PHP interpreter

9.4.1. Configuration

  1. Go on Settings (Ctrl+Alt+S) > PHP > Test Frameworks.

  2. Click on + button and select PHPUnit by Remote Interpreter.

    phpstorm 2022.3.2 settings phpunit by remote interpreter
  3. In PHPUnit by Remote Interpreter, select excel-editor-api-php:latest and click on OK button.

    phpstorm 2022.3.2 settings phpunit by remote interpreter select
  4. In the Settings dialog, click on OK or Apply button to validate all.

phpstorm 2022.3.2 settings phpunit

9.4.2. Run PHPUnit tests

For example, in the Project tool window, select the file or folder to run your tests from and choose Run '<file or folder>' from the context menu of the selection:

phpstorm 2022.3.2 phpunit launch

PhpStorm generates a default run configuration and starts a run test session with it:

phpstorm 2022.3.2 phpunit results

10. Codacy configuration

10.1. Code coverage

💡
Set XDEBUG_MODE=coverage before starting the container (see https://github.com/dunglas/symfony-docker/blob/main/docs/xdebug.md).

Duplicate CODACY_PROJECT_TOKEN.sh:

$ cp scripts/CODACY_PROJECT_TOKEN.sh.dist scripts/CODACY_PROJECT_TOKEN.sh

And define the API token CODACY_PROJECT_TOKEN (see https://app.codacy.com/gh/jprivet-dev/excel-editor-api/settings/coverage).

The file scripts/CODACY_PROJECT_TOKEN.sh is ignored by Git and imported by scripts/reporter.sh.

The file scripts/reporter.sh generates code coverage (a clover.xml file with PHPUnit) and uploads the coverage reports on Codacy.

11. Troubleshooting

11.1. Docker Compose CLI

Be sure to install the latest version of Docker Compose CLI plugin. With the older generation of docker compose, I had encountered the following error:

$ docker-compose build --pull --no-cache
...
Status: Downloaded newer image for composer:2
 ---> daa583eddaba
Step 27/31 : COPY composer.* symfony.* ./
COPY failed: no source files were specified
ERROR: Service 'php' failed to build : Build failed

Problem solved with the latest generation:

$ docker compose build --pull --no-cache

12. Resources & Inspiration

12.1. Dockerization

12.2. Caddy & TLS

{"level":"info","ts":1677857037.9273698,"msg":"warning: \"certutil\" is not available, install \"certutil\" with \"apt install libnss3-tools\" or \"yum install nss-tools\" and try again"}

13. Comments, suggestions?

Feel free to make comments/suggestions to me in the Git issues section.

14. License

"Excel Editor API" is released under the MIT License


About

Excel Editor API - Study of a complete application, with a SPA (Angular) and an API (Symfony), which allows users to connect, and to be able, according to their rights, to import Excel data and modify them online.

Topics

Resources

License

Stars

Watchers

Forks