Giter Club home page Giter Club logo

nlw-copa-flask's Introduction

NLW Copa Server

NLW Copa Server

πŸ’‘ Project's Idea

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.

πŸ” Features

  • Users register/login;
  • Available pools listing;
  • New pools creation;
  • Users count;
  • Available games listing;
  • New guesses creation;
  • Available guesses listing;

πŸ’Ή Extras

  • New games creation;
  • Pools ranking listing;
  • Games results setting (with auto score updating for guesses and participants);

πŸ›  Technologies

During the development of this project, the following techologies were used:

πŸ’» Project Configuration

First, create a new virtual environment on the root directory

$ python -m venv env

Activate the created virtual environment

$ .\env\Scripts\activate # On Windows machines
$ source ./env/bin/activate # On MacOS/Unix machines

Install the required packages/libs

(env) $ pip install -r requirements.txt

Internationalization (i18n) and Localization (l10n)

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.

🌐 Setting up config files

Create an .env file on the root directory, with all needed variables, credentials and API keys, according to the sample provided (.env.example).

Microsoft SQL Server

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

MySQL Server

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.

Firebase Cloud Messaging

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.

Time Zones

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.

⏯️ Running

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

πŸ”¨ Production Server

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.

πŸ’» 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.

⌨ Linux

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;
    }
}

πŸ“œ SSL/TLS

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

Documentation:

πŸ“„ License

This project is under the MIT license. For more information, access LICENSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.