Intranet de la Rez Flask web app

Related tags

Flaskintrarez
Overview

IntraRez

Application Flask de l'Intranet de la Rez.

Exigences

  • Python : Probablement >= 3.10 à terme, pour l'instant >= 3.8 suffit ;
  • Autres packages Linux : mysql-server postfix git, plus pour le déploiement : supervisor nginx ;
  • Packages Python : Voir requirements.txt, plus pour le déploiement : gunicorn pymysql cryptography ;
  • Pour le déploiement : un utilisateur Linux intrarez dédié.

Installation

Je reprends pour l'essentiel le déploiement conseillé dans le tutoriel : https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux

  • Installer l'application :

    cd /home/intrarez
    git clone https://github.com/GRI-ESPCI/intrarez
    cd intrarez
    python3 -m venv env
    source env/bin/activate
    pip install -r requirements.txt
    pip install gunicorn pymysql cryptography
    cp .conf_models/model.env .env
    
  • Créer et initialiser la base de données MySQL :

    sudo mysql -u root
    
    CREATE DATABASE intrarez CHARACTER SET utf8 COLLATE utf8_bin;
    CREATE USER 'intrarez'@'localhost' IDENTIFIED BY '<mdp-db>';
    GRANT ALL PRIVILEGES ON intrarez.* TO 'intrarez'@'localhost';
    FLUSH PRIVILEGES;
    QUIT;
    flask db upgrade
    
  • Modifier le fichier .env créé depuis le modèle. Pour générer une SECRET_KEY aléatoire :

    python3 -c "import uuid; print(uuid.uuid4().hex)"
    
  • Enregistrer l'application dans les variables d'environment :

    echo "export FLASK_APP=intrarez.py" >> ~/.profile
    
  • Compiler les traductions (fichiers binaires) :

    flask translate compile
    

L'application peut alors normalement être lancée avec flask run.

On a alors une version de développement installée : flask run n'est pas approprié à de la production (peu optimisé), et il faut configurer l'accès depuis l'extérieur (même si c'est un extérieur interne, dans notre cas).

Passage en production

On utilise Gunicorn en interne : le serveur Python n'est pas accessible de l'extérieur, c'est Nginx qui lui servira les requêtes non-statiques.

Gunicorn est lancé et contrôlé par Supervisor, qui fait à peu près le travail d'un service mais en plus pratique :

sudo cp .conf_models/supervisor.conf /etc/supervisor/conf.d/intrarez.conf
sudo supervisorctl reload

Le nombre de workers de Gunicorn (le -w 4 dans le fichier de conf) peut être adapté selon la machine.

Configuration de Nginx :

sudo cp .conf_models/nginx.conf /etc/nginx/sites-enabled/intrarez
sudo service nginx reload

Note : pour l'instant, l'application est configurée pour ne fonctionner qu'en HTTP, pas en HTTPS (problèmes de certificats en réseau interne).

Il faudra tester et voir ce qui marche mieux en terme d'avertissements des navigateurs et autres entre ça et un certificat auto-host : voir .conf_models/nginx.conf, avec

mkdir certs
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
  -keyout certs/key.pem -out certs/cert.pem

Compliqué d'avoir un vrai certificat, parce qu'il faut un nom de domaine associé, mais si on veut mettre des services accessibles depuis l'extérieur ce sera une étape obligée.

Mise à jour

Pour mettre à jour l'application, dans le dossier intrarez :

git pull
source env/bin/activate
supervisorctl stop intrarez
flask db upgrade
flask translate compile
sudo supervisorctl start intrarez

(côté développement, voir plus bas)

Notes de développement

Je vais ici noter pas à pas ce que je fais, pour simplifier au maximum l'appréhension du code par d'éventuels GRI futurs.

Début de l'installation

11/09/2021 - Loïc 137

Tout a été créé en suivant ce tutoriel : https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

Requirements : je pars sur Python 3.10, parce que le nouveau statement match me fait beaucoup trop de l'oeil.

À ce jour, Python 3.10 n'est disponible qu'en version release candidate 2 (donc quasiment finale), et devrait sortir début octobre (donc avant la release de l'IntraRez).

Installation propre de plusieurs versions de Python sur un même OS : https://hackersandslackers.com/multiple-python-versions-ubuntu-20-04

Installation d'un virtual env fresh

Utilisation de SQLAlchemy 1.4 (2.x pas prêt)

Gestion des migrations de db : lors du développement d'une nouvelle version modifiant le modèle de données,

  • En local : flask db migrate -m "Migration to <version>" ;
  • Vérifier le fichier créé dans migrations/versions ;
  • flask db upgrade pour appliquer localement ;
  • (autres modifs hors db)
  • Release de la version ;
  • En prod : pull puis flask db upgrade.

Je pars sur une structure en modules (basée sur les blueprints Flask), détaillée au chapitre XV du tuto : https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure

Application bilingue (utilisant Flask-Babel) : lorsque le code est modifié,

  • Exécuter flask translate update ;
  • Modifier/ajouter les clés de traduction dans app/translations/en/LC_MESSAGES/messages.po. Les entrées modifiées sont indiquées avec #, fuzzy : supprimer ce commentaire après avoir vérifié qu'il n'y avait pas d'erreur / modifié la traduction ;
  • Exécuter flask translate compile.
You might also like...
Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application.

Flask-Bcrypt Flask-Bcrypt is a Flask extension that provides bcrypt hashing utilities for your application. Due to the recent increased prevelance of

Flask Project Template A full feature Flask project template.

Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS

A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask
A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask

Flask-Fastx Flask-Fastx is a Fast API style support for Flask. It Gives you MyPy types with the flexibility of flask. Compatibility Flask-Fastx requir

Flask pre-setup architecture. This can be used in any flask project for a faster and better project code structure.

Flask pre-setup architecture. This can be used in any flask project for a faster and better project code structure. All the required libraries are already installed easily to use in any big project.

Pf-flask-rest-com - Flask REST API Common Implementation by Problem Fighter Library

In the name of God, the Most Gracious, the Most Merciful. PF-Flask-Rest-Com Docu

Flask-Discord-Bot-Dashboard - A simple discord Bot dashboard created in Flask Python
Flask-Discord-Bot-Dashboard - A simple discord Bot dashboard created in Flask Python

Flask-Discord-Bot-Dashboard A simple discord Bot dashboard created in Flask Pyth

Open-source Flask Sample built on top of flask-dance library
Open-source Flask Sample built on top of flask-dance library

Open-source Flask Sample built on top of flask-dance library. The project implements the social login for Github and Twitter - Originally coded by TestDriven.IO.

Flask-redmail - Email sending for Flask

Flask Red Mail: Email Sending for Flask Flask extension for Red Mail What is it?

Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications.

Flask Sitemapper Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications. This allows you to create a nice and

Comments
  • Ban is not terminated when a subscription is taken

    Ban is not terminated when a subscription is taken

    Cause: Ban termination is done in scripts/update_sub_states, which never detects new subscriptions as the sub state is instantly changed when subing.

    Solution: Add ban termination in subscription validation

    bug 
    opened by loic-simon 0
Alexa Skills Kit for Python

Program the Amazon Echo with Python Flask-Ask is a Flask extension that makes building Alexa skills for the Amazon Echo easier and much more fun. Flas

John Wheeler 1.9k Dec 30, 2022
YAML-formatted plain-text file based models for Flask backed by Flask-SQLAlchemy

Flask-FileAlchemy Flask-FileAlchemy is a Flask extension that lets you use Markdown or YAML formatted plain-text files as the main data store for your

Siddhant Goel 20 Dec 14, 2022
retorna informações de pessoas que não existem

random-person-api API que entrega dados aleatórios sobre pessoas que (provavelmente) não existem. Como usar? Copie o link abaixo https://random-person

Miguel 3 Aug 09, 2022
PatientDB is a flask app to store patient information.

PatientDB PatientDB on Heroku "PatientDB is a simple web app that stores patient information, able to edit the information, and able to query the data

rbb 2 Jan 31, 2022
The Snoopy boilerplate in flask framework for development enterprise application.

Snoopy What is snoopy! The "Snoopy" boilerplate in flask framework for development enterprise application. Motivation In my 10 years of development ex

Bekhzod 2 Sep 29, 2022
Harmony, a discord clone, allows users to chat with other users in real time via servers, channels, and direct messages

Harmony, a discord clone, allows users to chat with other users in real time via servers, channels, and direct messages

Seth Holland 3 Feb 03, 2022
Heroku Flask Setup

Heroku Flask Setup

Abhimanyu Haralukallu 0 Dec 07, 2021
Flask-template - A simple template for make an flask api

flask-template By GaGoU :3 a simple template for make an flask api notes: you ca

GaGoU 2 Feb 17, 2022
Simple flask api. Countdown to next train for each station in the subway system.

Simple flask api. Countdown to next train for each station in the subway system.

Kalyani Subbiah 0 Apr 17, 2022
Source code for backpainfree.org - a Q&A platform similar to StackOverFlow

Source code for backpainfree.org - a Q&A platform similar to StackOverFlow, which is designed specifically for people with back pain problems. Users can ask questions, post answers and comments, vote

Olzhas Arystanov 8 Dec 11, 2022
Formatting of dates and times in Flask templates using moment.js.

Flask-Moment This extension enhances Jinja2 templates with formatting of dates and times using moment.js. Quick Start Step 1: Initialize the extension

Miguel Grinberg 358 Nov 28, 2022
Flask-Discord-Bot-Dashboard - A simple discord Bot dashboard created in Flask Python

Flask-Discord-Bot-Dashboard A simple discord Bot dashboard created in Flask Pyth

Ethan 8 Dec 22, 2022
Flask Project Template A full feature Flask project template.

Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS

Bruno Rocha 96 Dec 23, 2022
Making a simple app using React, Flask and MySQL.

Samys-Cookbook Making a simple app using React and Flask. What This will be a simple site to host my recipes. It will have a react front-end, a flask

Samridh Anand Paatni 1 Jul 07, 2022
Flask-app scaffold, generate flask restful backend

Flask-app scaffold, generate flask restful backend

jacksmile 1 Nov 24, 2021
An python flask app with webserver example

python-flask-example-keepalive How it works? Basically its just a python flask webserver which can be used to keep any repl/herokuapp or any other ser

KangersHub 2 Sep 28, 2022
A flask app that turn image into ASCII art

ASCII art A flask app that turn image into ASCII art. This app has been deployed to https://motmaytinh.herokuapp.com Getting Started These instruction

Trần Ngọc Quý 1 Jan 13, 2022
Flask-Rebar combines flask, marshmallow, and swagger for robust REST services.

Flask-Rebar Flask-Rebar combines flask, marshmallow, and swagger for robust REST services. Features Request and Response Validation - Flask-Rebar reli

PlanGrid 223 Dec 19, 2022
flask extension for integration with the awesome pydantic package

Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v

249 Jan 06, 2023