This project was developed during the RocketSeat's Next Level Week - Copa event. It aims to create a backend server application for providing world cup pools for friends which are loooking for placing their bets on the Fifa 2022 World Cup Games.
- Users register/login;
- Available pools listing;
- New pools creation;
- Users count;
- Available games listing;
- New guesses creation;
- Available guesses listing;
- New games creation;
- Pools ranking listing;
- Games results setting (with auto score updating for guesses and participants);
During the development of this project, the following techologies were used:
$ python -m venv env
$ .\env\Scripts\activate # On Windows machines
$ source ./env/bin/activate # On MacOS/Unix machines
(env) $ pip install -r requirements.txt
In order to provide results according to the specified languages set in the request headers (Accept-Language), we make use of Flask-Babel. Here are a few commands for its use:
(env) $ pybabel extract -F babel.cfg -k _l -o messages.pot . # Gets list of texts to be translated
(env) $ pybabel init -i messages.pot -d app/translations -l pt # Creates file (messages.po) with 'pt' translations (replace 'pt' with required language code)
(env) $ pybabel update -i messages.pot -d app/translations -l pt # Updates file (messages.po) with 'pt' translations (replace 'pt' with required language code)
(env) $ pybabel compile -d app/translations # Compiles the translation files
It's important to compile the translation files before running the application, should it provide the correct translations for the system users.
Create an .env file on the root directory, with all needed variables, credentials and API keys, according to the sample provided (.env.example).
When using the Microsoft SQL Server, it is also required to download and install the ODBC Driver for SQL Server. Otherwise, it won`t be possible to connect with the SQL server.
Also, in order to install pyodbc on Linux, it might be necessary to install unixodbc-dev with the command below:
$ sudo apt-get install unixodbc-dev
When using the Microsoft SQL Server, it is required to choose a default charset which won't conflict with some models fields data length. The 'utf8/utf8_general_ci' should work.
In order to be able to send push notifications to mobile applications, currently the Firebase Cloud Messaging solution it's being used. Aside from setting the .env file, you must also have your service account JSON credentials file present on the app's root folder.
Since the application allows working with different time zones, it might be interesting to use the same time zone as the machine where the application is running when defining the TZ variable on the .env file, since internal database functions (which are used for creating columns like created_at and updated_at) usually make use of the system's time zone (when not set manually).
Also, on server's migration, the database backup could be coming from a machine with a different time zone definition. In this case, it might be necessary to convert the datetime records to the new machine time zone, or set the new machine time zone to the same as the previous machine.
To run the project in a development environment, execute the following command on the root directory, with the virtual environment activated.
(env) $ python run.py
In order to leave the virtual environment, you can simply execute the command below:
(env) $ deactivate
In order to execute the project in a production server, you must make use of a Web Server Gateway Interface (WSGI), such as uWSGI for Linux or waitress for Windows.
In Windows, you could run the wsgi.py file, like so:
(env) $ python wsgi.py
After that, a Windows Task can be created to restart the application, activating the virtual environment and calling the script, whenever the machine is booted.
In Linux systems, you can use the following command to check if the server is working, changing the port number to the one you're using in the app:
(env) $ gunicorn --worker-class eventlet --bind 0.0.0.0:8080 wsgi:app --reload
The nlw-copa.service file must be updated and placed in the '/etc/systemd/system/' directory. After that, you should execute the following commands to enable and start the service:
$ sudo systemctl start nlw-copa
$ sudo systemctl enable nlw-copa
$ sudo systemctl status nlw-copa
In order to serve the application with Nginx, it can be configured like so (adjusting the paths, server name, etc.):
# Flask Server
server {
listen 80;
server_name nlw-copa.mhsw.com.br;
location / {
include proxy_params;
proxy_pass http://localhost:8080;
client_max_body_size 16M;
}
location /socket.io {
include proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://localhost:8080/socket.io;
}
}
You can also add security with SSL/TLS layer used for HTTPS protocol. One option is to use the free Let's Encrypt certificates.
For this, you must install the Certbot's package and use its plugin, with the following commands (also, adjusting the srver name):
$ sudo apt install snapd # Installs snapd
$ sudo snap install core; sudo snap refresh core # Ensures snapd version is up to date
$ sudo snap install --classic certbot # Installs Certbot
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot # Prepares the Certbot command
$ sudo certbot --nginx -d nlw-copa.mhsw.com.br
- Como servir os aplicativos Flask com o Gunicorn e o Nginx no Ubuntu 18.04
- Como servir aplicativos Flask com o uWSGI e o Nginx no Ubuntu 18.04
- How to host multiple flask apps under a single domain hosted on nginx?
- Deploying Flask on Windows
- Deploy de aplicativo Python / Flask no windows
- How to serve static files in Flask
- Get list of all routes defined in the Flask app
- Switching from SQLite to MySQL with Flask SQLAlchemy
- Token-Based Authentication With Flask
- Define Relationships Between SQLAlchemy Data Models
- Packaging Python Projects
- Uploading Files
- Python - Flask - Working with middleware for specific route
- Como Instalar o Python 3 e Configurar um Ambiente de Programação no Ubuntu 18.04 [Quickstart]
- What is the meaning of "Failed building wheel for X" in pip install?
- Install Certbot on ubuntu 20.04
- The Flask Mega-Tutorial Part XIII: I18n and L10n
- Flask Babel - 'translations/de/LC_MESSAGES/messages.po' is marked as fuzzy, skipping
- List of tz database time zones
- How to translate text with python
- deep-translator - PyPI
- How to Install FFmpeg on Windows, Mac, Linux Ubuntu and Debian
- Using PYTHONPATH
- How do you set your pythonpath in an already-created virtualenv?
- cannot import local module inside virtual environment from subfolder
- Socket.IO
- Gunicorn ImportError: cannot import name 'ALREADY_HANDLED' from 'eventlet.wsgi' in docker
- Error when running Flask application with web sockets and Gunicorn
- eventlet worker: ALREADY_HANDLED -> WSGI_LOCAL #2581
- polling-xhr.js:157 Error 502 Bad Gateway While trying to establish connection between the client and the server using SocketIO #1804
- Firebase Admin Python SDK
- Firebase cloud messaging and python 3
This project is under the MIT license. For more information, access LICENSE.