A chess REST API.

Related tags

API chess-api
Overview

Chess API

A chess REST API.

Documentation

Read the latest docs here.

Installation

Clone the chesslablab/chess-api repo into your projects folder as it is described in the following example:

$ git clone [email protected]:chesslablab/chess-api.git

Then cd the chess-api directory and install the Composer dependencies:

$ composer install

Create an .env file:

$ cp .env.example .env

If necessary, update the environment variables in your .env file.

The Chess API goes hand in hand with Chess Data which is a database, data science and machine learning repository. Thus, please setup the Chess Data repo, create a database and seed it with sample data. You may want to configure a web server along with a MySQL server, or use Docker if you prefer. Described below are the steps to run the API with Docker.


Start the Chess Data Containers

cd your ~/projects/chess-data and run:

$ bash/prod/start.sh
This will bootstrap the production environment. Are you sure to continue? (y|n) y

The bash script will create the chess_data_mysql and chess_data_php_fpm containers:

$ docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
092ec0757601   mysql:8.0            "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   chess_data_mysql
5bf471066735   chess-data_php_fpm   "docker-php-entrypoi…"   10 minutes ago   Up 10 minutes   9000/tcp                                               chess_data_php_fpm

As well as the chess-data_default network:

$ docker network ls
NETWORK ID     NAME                 DRIVER    SCOPE
e70c00029afb   bridge               bridge    local
936d496d81bb   chess-data_default   bridge    local
3a4db4d71d51   host                 host      local
ffe89efcb84e   none                 null      local

Please notice that if restarting your computer, the DB_HOST in your .env file may need to be updated with the new IP of the chess_data_mysql container.

For further information, read the bash/prod/start.sh script.

$ IP_ADDRESS="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' chess_data_mysql)"
$ sed -i "s/DB_HOST=.*/DB_HOST=${IP_ADDRESS}/g" .env

Create and Seed the Chess Database With Data

Let's now create the database and seed some tables with data.

$ docker exec -itu 1000:1000 chess_data_php_fpm php cli/db-create.php
$ docker exec -itu 1000:1000 chess_data_php_fpm php cli/seed/openings.php
$ docker exec -itu 1000:1000 chess_data_php_fpm php cli/seed/games.php data/players/Carlsen.pgn
$ docker exec -itu 1000:1000 chess_data_php_fpm php cli/seed/games.php data/players/PolgarJ.pgn

Start the Chess API Containers

First things first, make sure to create and install an SSL certificate in the docker/nginx/ssl folder as described next:

These posts explain how to create a certificate for a Chess Server, however, the steps are pretty much identical to create an SSL certificate for the Chess API.

The thing is, a certificate needs to be installed — otherwise nginx will exit with code 1.

...
chess_api_nginx exited with code 1
chess_api_nginx | /docker-entrypoint.sh: Configuration complete; ready for start up
chess_api_nginx | 2022/01/28 17:28:55 [emerg] 1#1: cannot load certificate "/etc/nginx/ssl/certificate.crt": PEM_read_bio_X509_AUX() failed (SSL: error:0909006C:PEM routines:get_name:no start line:Expecting: TRUSTED CERTIFICATE)
chess_api_nginx | nginx: [emerg] cannot load certificate "/etc/nginx/ssl/certificate.crt": PEM_read_bio_X509_AUX() failed (SSL: error:0909006C:PEM routines:get_name:no start line:Expecting: TRUSTED CERTIFICATE)
chess_api_nginx exited with code 1

Thus, here is how the folder structure looks like after the certificate has been created.

🗁 docker
  🗁 nginx
    🗁 ssl
      🗎 ca_bundle.crt
      🗎 certificate.crt
      🗎 private.key

Then, cd your ~/projects/chess-api and run:

$ bash/prod/start.sh
This will bootstrap the production environment. Are you sure to continue? (y|n) y

The bash script will create the chess_api_php_fpm and chess_api_nginx containers:

$ docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
6c1aa4ce9886   chess-api_php_fpm    "docker-php-entrypoi…"   32 seconds ago   Up 29 seconds   9000/tcp                                               chess_api_php_fpm
1ada37d4a794   nginx:1.20           "/docker-entrypoint.…"   32 seconds ago   Up 28 seconds   80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp          chess_api_nginx
1db2e7e758ec   chess-data_php_fpm   "docker-php-entrypoi…"   2 hours ago      Up 6 minutes    9000/tcp                                               chess_data_php_fpm
7b839bce2d58   mysql:8.0            "docker-entrypoint.s…"   2 hours ago      Up 6 minutes    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   chess_data_mysql

Test the API Endpoints

Find out the IP of the chess_api_nginx container as it is described in the following example:

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' chess_api_nginx
172.19.0.4

And finally add this entry in your /etc/hosts file:

172.19.0.4      pchess.net

Figure 1 Figure 1. Example of GET /api/docs request.

Figure 2 Figure 2. Example of POST /api/grandmaster request.

Figure 3 Figure 3. Example of POST /api/opening request.

Figure 4 Figure 4. Example of POST /api/play request.

Contributions

See the contributing guidelines.

Happy learning and coding! Thank you, and keep it up.

Comments
  • Implement the POST /api/download_image endpoint

    Implement the POST /api/download_image endpoint

    This endpoint is intended to download a PNG image given a FEN string, and is to be implemented as per the specification below.

    | Method | Endpoint | Description | |--------|---------------------|------------------------| | POST | /api/download_image | Downloads a PNG image. |

    | Parameter | Type | Description | |-----------|--------|--------------| | fen | string | A FEN string |

    | Response | Description | |-------------------|------------------------------| | 200 (OK) | The FEN string is valid. | | 400 (Bad Request) | The FEN string is not valid. |

    The POST /api/download_image endpoint should be implemented in a new file called src/Controller/DownloadImageController.php.

    See:

    • https://php-chess.readthedocs.io/en/latest/media-board-to-png/

    Keep it up, and happy learning!

    good first issue 
    opened by programarivm 13
  • Implement the POST /api/eco endpoint

    Implement the POST /api/eco endpoint

    This endpoint is intended to fetch the name of a chess opening given its ECO code.

    | Method | Endpoint | Description | |--------|----------|----------------------------------------| | POST | /api/eco | Returns the name of the chess opening. |

    | Parameter | Type | Description | |-----------|--------|-------------| | eco | string | ECO code. |

    | Response | Description | |------------------|-----------------------------------------| | 200 (OK) | The ECO code is valid. | | 204 (No Content) | The ECO code was found in the database. |

    Query examples:

    curl -d '{"eco":"B94"}' -H "Content-Type: application/json" -X POST http://localhost:8000/api/eco
    

    Response:

    {
      "name": "Sicilian, Najdorf"
    }
    

    The POST /api/eco endpoint should be implemented in a new file called src/Controller/EcoController.php.

    Keep it up, and happy learning!

    opened by programarivm 1
  • Implement the POST /api/engine/stockfish endpoint

    Implement the POST /api/engine/stockfish endpoint

    This endpoint is intended to play chess against the Stockfish chess engine and is to be implemented as described next.

    | Method | Endpoint | Description | |--------|----------|-------------| | POST | /api/engine/stockfish | Returns Stockfish's move |

    | Parameter | Type | Description | |-----------|------|-------------| | movetext | string | LAN movetext | | skillLevel | string | Skill level | | depth | string | Skill level |

    | Response | Description | |----------|-------------| | 200 (OK) | Returns Stockfish's move | | 400 (Bad Request) | The request could not be processed |

    The POST /api/engine/stockfish endpoint should be implemented in a new file called src/Controller/EngineStockfishController.php.

    See:

    Keep it up, and happy learning and coding!

    good first issue 
    opened by programarivm 0
  • Process movetexts in LAN format in the POST api/play endpoint

    Process movetexts in LAN format in the POST api/play endpoint

    The POST api/play endpoint in src/Controller/PlayController.php should also process movetexts in LAN format. In order for this issue to be resolved though, the Chess\Player\LanPlayer class must be implemented first in the PHP Chess repository.

    See:

    • https://github.com/chesslablab/php-chess/issues/259

    Keep it up, and happy coding and learning!

    help wanted good first issue 
    opened by programarivm 0
  • Make the POST api/play endpoint respond with a 400 HTTP status code

    Make the POST api/play endpoint respond with a 400 HTTP status code

    At this moment, the POST api/play endpoint in src/Controller/PlayController.php will respond with a 500 Internal Server Error HTTP status code if processing an invalid movetext as shown in the image below.

    Screenshot 2022-12-10 19:11:58 Figure 1. API responding with 500 Internal Server Error after 1.e4 e4 has been processed.

    However, it should respond with a 400 Bad Request HTTP status code.

    The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing).

    Keep it up and happy coding and learning!

    good first issue 
    opened by programarivm 0
  • Improve the API documentation

    Improve the API documentation

    The Chess API has recently been updated:

    • https://github.com/chesslablab/chess-api/issues/24
    • https://github.com/chesslablab/chess-api/issues/27

    Attached below are some examples of the newest version up and running.

    Figure-01 Figure 1. POST https://pchess.net/api/grandmaster

    Figure-02 Figure 2. POST https://pchess.net/api/download_image

    The swagger.yaml file should be proofread and updated accordingly. The API controller methods are found in the src/Controller folder.

    This is the sandbox API for testing purposes:

    Should you find a bug or something that needs to be upgraded, please open an issue.

    Keep it up, and happy coding and learning!

    documentation good first issue 
    opened by programarivm 0
  • Allow to download screenshots of Capablanca games

    Allow to download screenshots of Capablanca games

    At this moment, src/Controller/DownloadImageController.php doesn't allow to download screenshots of Capablanca chess games as shown in the animation below.

    output

    However, the /download_image endpoint should accept a new parameter called variant, this way a new $board object can be created accordingly to be then passed to Chess\Player\PgnPlayer.

    For further information on how a $board is initialized please visit:

    This issue is quite similar to the following one which has been solved already:

    • https://github.com/chesslablab/chess-api/issues/24

    Remember:

    There is an easy quick way to get the API up and running without an SSL certificate for when testing endpoints that don't require a database connection, e.g., api/download_image or api/download_mp4. In such cases you may want to use PHP's built-in web server.

    Keep it up, and happy coding and learning!

    help wanted good first issue 
    opened by programarivm 0
  • Allow to download videos of Chess960 and Capablanca games

    Allow to download videos of Chess960 and Capablanca games

    At this moment, src/Controller/DownloadMp4Controller.php allows to download MP4 videos of classical chess games only as shown below.

    output_trimmed_enhanced

    However, the /download_mp4 endpoint should accept a new parameter called variant. This way, a new $board object could be created accordingly to be then passed to Chess\Player\PgnPlayer.

    In Chess960 games the start position needs to be passed to the endpoint as well.

    For further information on how a $board is initialized please visit:

    Keep it up and happy learning and coding!

    help wanted good first issue 
    opened by programarivm 0
  • Implement the POST /api/download_gif endpoint

    Implement the POST /api/download_gif endpoint

    This endpoint is intended to download a GIF animation given a movetext, and is to be implemented as per the specification below.

    | Method | Endpoint | Description | |--------|-------------------|------------------| | POST | /api/download_gif | Downloads a GIF. |

    | Parameter | Type | Description | |-----------|--------|----------------| | movetext | string | PGN movetext |

    | Response | Description | |-------------------|--------------------------------| | 200 (OK) | The PGN movetext is valid. | | 400 (Bad Request) | The PGN movetext is not valid. |

    The POST /api/download_gif endpoint should be implemented in a new file called src/Controller/DownloadGifController.php.

    See:

    • https://php-chess.readthedocs.io/en/latest/media-board-to-gif/

    Keep it up, and happy learning!

    good first issue 
    opened by programarivm 0
  • Implement the POST /api/ascii endpoint

    Implement the POST /api/ascii endpoint

    This endpoint will return an ASCII representation of a chess position, and is to be implemented as per the specification below.

    | Method | Endpoint | Description | |--------|------------|------------------------------------------------------| | POST | /api/ascii | Returns an ASCII representation of a chess position. |

    | Parameter | Type | Description | |-----------|--------|--------------| | fen | string | A FEN string |

    | Response | Description | |-------------------|------------------------------| | 200 (OK) | The FEN string is valid. | | 400 (Bad Request) | The FEN string is not valid. |

    Query examples:

    curl -d '{"fen":"rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6"}' -H "Content-Type: application/json" -X POST http://localhost:8000/api/ascii
    

    Response:

    {
      "ascii":"r  n  b  q  k  b  n  r ... R  N  B  Q  K  B  N  R"
    }
    

    The POST /api/ascii endpoint should be implemented in a new file called src/Controller/AsciiController.php.

    See:

    • https://php-chess.readthedocs.io/en/latest/ascii/#printboard-board-string

    Keep it up, and happy coding.

    good first issue 
    opened by programarivm 0
  • Implement the POST /api/opening endpoint

    Implement the POST /api/opening endpoint

    This endpoint is intended to fetch chess opening data given a set of parameters.

    | Method | Endpoint | Description | |--------|----------|----------------------------------------| | POST | /api/opening | Returns chess opening data. |

    | Parameter | Type | Description | Optional | |-----------|--------|---------------|----------| | eco | string | ECO code. | Yes | | name | string | Opening name. | Yes | | movetext | string | Movetext. | Yes |

    | Response | Description | |------------------|----------------------------------------------| | 200 (OK) | The chess opening is valid. | | 204 (No Content) | The chess opening was found in the database. |

    Query examples:

    curl -d '{"eco":"B94"}' -H "Content-Type: application/json" -X POST http://localhost:8000/api/opening
    

    Response:

    {
      "eco": "B94",
      "name": "Sicilian Defense: Najdorf Variation",
      "movetext": "1.e4 c5 2.Nf3 d6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 a6 6.Bg5"
    }
    

    The POST /api/opening endpoint should be implemented in a new file called src/Controller/OpeningController.php.

    opened by programarivm 0
Owner
ChesslabLab
Open Source community to learn web development and chess. Let's create something awesome!
ChesslabLab
Simple PHP API client for tube-hosting.com rest API

Tube-Hosting API PHP client Explanation This PHP library is a simple api wrapper/client for the tube-hosting.com api. It is based on the provided docu

null 4 Sep 12, 2022
GraphQL API to Studio Ghibli REST API

GhibliQL GhibliQL is a GraphQL wrapper to the Studio Ghibli REST API Usage First, you'll need a GraphQL client to query GhibliQL, like GraphQL IDE Con

Sebastien Bizet 8 Nov 5, 2022
Single file PHP script that adds a REST API to a SQL database

PHP-CRUD-API Single file PHP script that adds a REST API to a MySQL/MariaDB, PostgreSQL, SQL Server or SQLite database. NB: This is the TreeQL referen

Maurits van der Schee 3.2k Jan 8, 2023
PHP REST API Framework

PSX Framework About PSX is a framework written in PHP dedicated to build REST APIs. It is based on multiple components which cover many aspects of the

Apioo 121 Dec 30, 2022
example repository training REST API

adalah codebase REST-API dari tugas peserta yang mengikuti training membangun REST API dengan lumen selama 2 minggu. studi kasus nya adalah REST API website marketplace untuk agrobisniss.

Nuris Akbar 6 Sep 4, 2021
A REST API that should power the Agile Monkeys CRM Service

This is a simple REST API that purposes to power the Agile Monkeys CRM service

Dickens odera 3 Jul 31, 2021
A simple, extensible REST API framework for PHP

Note: This framework is not stable yet. Introduction Aphiria is a suite of small, decoupled PHP libraries that make up a REST API framework. It simpli

Aphiria 119 Dec 23, 2022
A lightweight REST API framework

php-microframework A minimal PHP 8 REST API built onto php-microframework. Features Automatic routing based on URL path, e.g. https://domain/controlle

John 1 Oct 16, 2021
PHP REST API without using any frameworks. Contains all CRUD operations.

PHP REST API without any framework and CRUD operations ?? Hi there, this is a simple REST API built in PHP without using any frameworks. This is built

Hanoak 10 Sep 5, 2022
Laravel Client REST Camunda API

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Uriel Reyes 1 Nov 5, 2021
Osclass REST API plugin.

Osclass API Osclass API plugin, with all the required endpoints and JWT auth. Osclass App (Native + PWA) You always wanted a mobile app for Osclass, d

Defected 4 Sep 10, 2022
Joy VoyagerApi module adds REST Api end points to Voyager with Passport and Swagger support.

Joy VoyagerApi This Laravel/Voyager module adds REST Api with Passport and Swagger support to Voyager. By ?? Ramakant Gangwar. Prerequisites Composer

Ramakant Gangwar 14 Dec 12, 2022
A rest api repository with laravel.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

kyo 2 Jan 17, 2022
A rest api repository with laravel.

API Back End This is the repository for the TKJ 1 Class 12 Web Back End API system, for detailed info, you can go to the main link of the Front End ap

kyo 2 Jan 17, 2022
Phalcon PHP REST API Package, still in beta, please submit issues or pull requests

PhREST API A Phalcon REST API package, based on Apigees guidelines as found in http://apigee.com/about/content/web-api-design Please see the skeleton

PhREST 29 Dec 27, 2022
API-Rest para información sobre administración-politica de Cuba

API_CUBA API desarrollada con Laravel PHP para brindar servicios REST para multiples plataformas sobre información politico administrativa de API_CUBA

Iosvany Alvarez 6 Mar 31, 2022
Extended response classes for Rest API clients/SDKs

Rest Response Extended response classes for Rest API clients/SDKs. About This package is intended to be component of SDKs so the responses can be easi

Ilesanmi Olawale Adedotun 2 Mar 28, 2022
Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.

API Platform is a next-generation web framework designed to easily create API-first projects without compromising extensibility and flexibility: Desig

API Platform 7.7k Jan 7, 2023
A PHP library to support implementing representations for HATEOAS REST web services.

Hateoas A PHP library to support implementing representations for HATEOAS REST web services. Installation Working With Symfony Usage Introduction Conf

William Durand 998 Dec 5, 2022