From ad740af3d0608bda003834fa774ab161c7cebc2c Mon Sep 17 00:00:00 2001 From: Francisco Sandoval Date: Wed, 20 Dec 2023 12:35:48 -0300 Subject: [PATCH] actualizacion de readme.md --- readme.md | 265 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 196 insertions(+), 69 deletions(-) diff --git a/readme.md b/readme.md index 689264a..db98597 100644 --- a/readme.md +++ b/readme.md @@ -1,64 +1,177 @@ -# Dependencias +# Estructura de carpetas -* **Django**: framework django -* **psycopg2-binary**: permite uso de base datos postgres -* **djangorestframework**: permite crear rest api json -* **django-cors-headers**: requerido para desarrollo de frontend, permite acceso desde una dominio distinto al backend -* **django-filter**: permite filtrar resultado usando paraquetros en querystring -* **coreapi**: genera documentación de rest api -* **python-decouple**: lee archivo .env para variables de ambiente -* **PyJWT**: permite generar json-web-token -* **pymongo**: permite conectarse a base datos mongo -* **Pillow**: permite imagenar imagenes -* **openpyxl**: permite generar archivos excel +Se recomienda la siguiente estructura de carpeta para iniciar proyecto + +~~~txt +/app + |-- /transporte + | |-- (codigo del proyecto backend) + | + |-- /dumps + | |-- (respaldos de la base de datos transporte) + | + |-- /proto + | |-- (codigo del proyecto) + | + |-- docker-compose.yml + |-- restore_db.sh +~~~ +
+
+
# Docker Para iniciar la aplicacion usando docker ~~~bash -cd /docker +cd /app docker compose up -d ~~~ +
+
+
+# Proyectos involucrados -# Script para levantar aplicacion python +## Transporte -**start.sh** +Ref: https://gitlab.com/m3f_usm/admin_transporte/backend -~~~bash +## Proto + +Ref: https://gitlab.com/m3f_usm/proto + +## Respaldos de base de datos + +Ver información en: + +https://gitlab.com/m3f_usm/admin_transporte/backend/-/wikis/Databases + +
+
+
+ +# archivo: restore_db.sh + +~~~sh #!/bin/sh -cd /app -# si no existe directorio de ambiente, se crea -if [ ! -d /app/libs ]; then - python -m venv libs - . ./libs/bin/activate - pip install -r requirements.txt -fi +# obtener el ultimo respaldo de la carpeta +LASTFILEDUMP=$(ls -t /dumps/*.sql.gz | sort -r | head -n 1) +echo "FILE: $LASTFILEDUMP" -# activar ambiente con dependencias -. ./libs/bin/activate - -# si no existe un proyecto inicial, lo crea -if [ ! -d /app/project ]; then - django-admin startproject project -fi - -# iniciar servidor con puerto 8000 -python /app/project/manage.py runserver 0.0.0.0:8000 -~~~ - -# Extraer models de base de datos - -~~~bash -python manage.py inspectdb > api/models.py +# restaurar la base datos +gunzip -c "$LASTFILEDUMP" | psql -U postgres "$POSTGRES_DB" ~~~ -# file: docker-compose.yml +# archivo produccion: docker-compose.yml + +~~~yml +version: '3' +name: transporte + +services: + + backend: + image: python:3.11-alpine + depends_on: + - db + environment: + - PORT=4000 + - DBHOST=db + - DBPORT=5432 + - DBNAME=transporte + - DBSCHEMA=public + - DBUSER=postgres + - DBPASS=password + - DB_REDIS_HOST=dbproto + - DB_REDIS_PORT=6379 + - SECRET_JWT="kf6Jc!f30Z!1k1N0#!%#" + ports: + - 4000:4000 + volumes: + - ./transporte:/app + - venv:/root/venv + working_dir: /app + command: sh -c " + [ -d /root/venv/bin ] || ( \ + python -m venv /root/venv/ \ + && . /root/venv/bin/activate \ + && pip install -r requirements.txt \ + ) ; + + . /root/venv/bin/activate ; + + cd /app ; + [ -d project ] || django-admin startproject project ; + [ -f .env ] || ( [ -f .env.develop ] && cp .env.develop .env ) ; + chmod -R o+w project/ ; + python project/manage.py runserver 0.0.0.0:$$PORT + " + + db: + image: postgis/postgis:14-3.4-alpine + volumes: + - db:/var/lib/postgresql/data + - ./dumps:/dumps + - ./restore_db.sh:/docker-entrypoint-initdb.d/02_restore_db.sh + ports: + - 5432:5432 + environment: + POSTGRES_PASSWORD: password + POSTGRES_DB: transporte + PGDATA: /var/lib/postgresql/data/pgdata + + proto: + image: python:3.11-alpine + environment: + - DB_REDIS_HOST=dbproto + - DB_REDIS_PORT=6379 + - TZ=America/Santiago + volumes: + - ./proto:/app + - venv_proto:/root/venv + working_dir: /app + command: sh -c " + [ -d /root/venv/bin ] || ( \ + python -m venv /root/venv/ \ + && . /root/venv/bin/activate \ + && pip install -r requirements.txt \ + ) ; + + . /root/venv/bin/activate ; + + cd /app ; + [ -f .env ] || ( [ -f .env.develop ] && cp .env.develop .env ) ; + watch -n 30 -t python main.py + " + + dbproto: + image: redis:7.2-alpine + + +volumes: + db: + venv: + venv_proto: +~~~ + +
+
+
+ +# tunnel para desarrollo + +Ver información en: + +https://gitlab.com/m3f_usm/admin_transporte/backend/-/wikis/home + + +# archivo desarrollo: docker-compose.yml ~~~yml version: '3' @@ -74,7 +187,7 @@ services: - 3000:3000 environment: - VITE_PORT=3000 - - VITE_BACKEND=/api + - VITE_BACKEND=http://localhost:4000/api working_dir: /app command: sh -c " [ ! -d node_modules ] && npm install ; @@ -84,22 +197,24 @@ services: backend: image: python:3.11-alpine + depends_on: + - db environment: - PORT=4000 - DBHOST=db - DBPORT=5432 - - DBNAME=database - - DBSCHEMA=desarrollo1 + - DBNAME=transporte + - DBSCHEMA=public - DBUSER=postgres - DBPASS=password - DB_REDIS_HOST=dbproto - DB_REDIS_PORT=6379 - SECRET_JWT="kf6Jc!f30Z!1k1N0#!%#" - volumes: - - ./backend:/app - - venv:/root/venv ports: - 4000:4000 + volumes: + - ./transporte:/app + - venv:/root/venv working_dir: /app command: sh -c " [ -d /root/venv/bin ] || ( \ @@ -117,31 +232,43 @@ services: python project/manage.py runserver 0.0.0.0:$$PORT " extra_hosts: - - dbproto: + - db: - db: - #image: postgis/postgis:14-3.4-alpine - image: postgres:14-alpine-geometry - volumes: - - db:/var/lib/postgresql/data - - ./backups:/backups - #- ./backend/docs/database/01_create_tables.sql:/docker-entrypoint-initdb.d/01_create_tables.sql - ports: - - 5436:5432 - environment: - POSTGRES_PASSWORD: password - POSTGRES_DB: database - PGDATA: /var/lib/postgresql/data/pgdata - - adminer: - image: adminer - environment: - ADMINER_DEFAULT_SERVER: db - ADMINER_DEFAULT_USER: postgres - ADMINER_DEFAULT_PASSWORD: password - ADMINER_DESIGN: lucas-sandery volumes: - db: venv: ~~~ + + +# Script para levantar aplicacion python sin docker + +## file: start.sh + +~~~bash +#!/bin/sh +cd /app + +# si no existe directorio de ambiente, se crea +if [ ! -d /root/venv ]; then + python -m venv /root/venv + . /root/venv/bin/activate + pip install -r requirements.txt +fi + +# activar ambiente con dependencias +. /root/venv/bin/activate + +# si no existe un proyecto inicial, lo crea +if [ ! -d /app/project ]; then + django-admin startproject project +fi + +# iniciar servidor con puerto 8000 +python /app/project/manage.py runserver 0.0.0.0:8000 +~~~ + +# Extraer models de base de datos + +~~~bash +python manage.py inspectdb > api/models.py +~~~ \ No newline at end of file