It is already deployed on Heroku, and the TicTacToe Project is already set up to use that URL.
This Server manages the backend of my TicTacToe AI, storing the game data inside a PostgresSQL database.
- ⚠ This Server does not need to be run! ⚠
- Flask REST API
- Contents
- Installing dependencies
- Setting up Environment Variables
- Creating the Tables
- Running the Server
- Routes
- Rate Limiting
For development, it is preferred to use pipenv to install dependencies.
pipenv shell
pipenv installHeroku doesn't use pipenv, so a requirements.txt file needs to be generated with the following command:
pipenv lock -r > requirements.txtIf you do not use pipenv either, you may install the dependencies directly from requirements.txt with pip.
pip install requirements.txtThese .env variables will be loaded and used to configure the server.
This is useful to hide any criticial information, or vary variables across development, testing, and production enviroments.
For this reason, avoid committing .env variables, ignore them by adding them to .gitignore!
Copy .env.example and rename the copy to .env.
Put your PostgreSQL or SQLite URI inside and rename the file to .env.
DATABASE_URLshould be your database's URI.FLASK_ENVshould be set todevelopmentif you want to see debug information.
The dotenv library will then handle loading in the .env file. Alternatively, pipenv automatically loads .env files.
import dotenv
dotenv.load_dotenv()Afterwards, you can access those variables through the os library.
import os
DATABASE_URL = os.getenv("DATABASE_URL")If you don't want to setup a PostgreSQL instance locally, you can use SQLite instead.
All you need to do is set the DATABASE_URL to "sqlite+pysqlite:///tictactoe.db", and a database of the name tictactoe.db will be created automatically.
However, Heroku doesn't support SQLite, and you will need to use another database for production.
To create the required tables, run pipenv create_db.py.
pipenv create_db.pyTo run the server in development mode:
pipenv run python app.pyPython is single threaded, and won't be able to handle much load on it's own in production. guinicorn is used to scale the server, and restart it if it crashes.
To run the server in production mode:
pipenv run gunicorn app:appFinally, if you want the Frontend GUI to use your server, go to /TicTacToe/util.py and change SERVER_URL to the Flask Server's URL, which will be http://127.0.0.1:5000 by default. If you want to deploy the server onto Heroku, follow this tutorial.
Retrieves a JSON response of all the games stored on the database.
Returns the JSON data and a 200 response code if successful. An example of the JSON data:
[
{
"draw": true,
"moves": 9,
"player1": "Player",
"player2": "AIPlayer",
"startingPlayer": "Player",
"win": false,
"winner": ""
},
{
"draw": false,
"moves": 7,
"player1": "Player",
"player2": "AIPlayer",
"startingPlayer": "AIPlayer",
"win": true,
"winner": "AIPlayer"
}
]Adds a game to the database.
Requires a JSON body along with the request, such as:
{
"draw": false,
"moves": 7,
"player1": "Player",
"player2": "AIPlayer",
"startingPlayer": "AIPlayer",
"win": true,
"winner": "AIPlayer"
}Returns the new game added and a 201 response code if successful. Otherwise, will return some error text along with a 400 response code.
To mitigate DDoS attacks, the routes are rate limited
from flask import Flask
from flask_limiter import Limiter
app = Flask(__name__)
Limiter(app, key_func=get_remote_address, default_limits=["500 per day", "60 per hour"])