-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from guivaloz/guivaloz/mejoras
Guivaloz/mejoras
- Loading branch information
Showing
8 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,151 @@ | ||
# pjecz-hercules-api-oauth2 | ||
|
||
Hércules API OAuth2 del Poder Judicial del Estado de Coahuila de Zaragoza. | ||
Esta API es usada por los sistemas y aplicaciones. No es para cuentas personales. | ||
|
||
## Requerimientos | ||
|
||
Los requerimientos son | ||
|
||
- Python 3.11 | ||
- PostgreSQL 15 | ||
|
||
## Instalación | ||
|
||
Crear el entorno virtual | ||
|
||
```bash | ||
python3.11 -m venv .venv | ||
``` | ||
|
||
Ingresar al entorno virtual | ||
|
||
```bash | ||
source venv/bin/activate | ||
``` | ||
|
||
Actualizar el gestor de paquetes **pip** | ||
|
||
```bash | ||
pip install --upgrade pip setuptools | ||
``` | ||
|
||
Instalar el paquete **wheel** para compilar las dependencias | ||
|
||
```bash | ||
pip install wheel | ||
``` | ||
|
||
Instalar **poetry 2** en el entorno virtual si no lo tiene desde el sistema operativo | ||
|
||
```bash | ||
pip install poetry | ||
``` | ||
|
||
Configurar **poetry** para que use el entorno virtual dentro del directorio del proyecto | ||
|
||
```bash | ||
poetry config virtualenvs.in-project true | ||
``` | ||
|
||
Instalar las dependencias por medio de **poetry** | ||
|
||
```bash | ||
poetry install | ||
``` | ||
|
||
## Configuración | ||
|
||
Crear un archivo `.env` en la raíz del proyecto con las variables de entorno | ||
|
||
```ini | ||
# Base de datos | ||
DB_HOST=127.0.0.1 | ||
DB_PORT=5432 | ||
DB_NAME=pjecz_plataforma_web | ||
DB_USER=XXXXXXXXXXXX | ||
DB_PASS=XXXXXXXXXXXX | ||
|
||
# Origins | ||
ORIGINS=http://127.0.0.1:3000 | ||
|
||
# Salt sirve para cifrar el ID con HashID, debe ser igual en la API | ||
SALT=XXXXXXXXXXXX | ||
|
||
# Clave secreta para generar los tokens | ||
SECRET_KEY=XXXXXXXXXXXX | ||
``` | ||
|
||
Crear un archivo `.bashrc` que cargue las variables de entorno y el entorno virtual | ||
|
||
```bash | ||
if [ -f ~/.bashrc ] | ||
then | ||
. ~/.bashrc | ||
fi | ||
|
||
if command -v figlet &> /dev/null | ||
then | ||
figlet Hercules API OAuth2 | ||
else | ||
echo "== Hercules API OAuth2" | ||
fi | ||
echo | ||
|
||
if [ -f .env ] | ||
then | ||
echo "-- Variables de entorno" | ||
export $(grep -v '^#' .env | xargs) | ||
# source .env && export $(sed '/^#/d' .env | cut -d= -f1) | ||
echo " DB_HOST: ${DB_HOST}" | ||
echo " DB_PORT: ${DB_PORT}" | ||
echo " DB_NAME: ${DB_NAME}" | ||
echo " DB_USER: ${DB_USER}" | ||
echo " DB_PASS: ${DB_PASS}" | ||
echo " ORIGINS: ${ORIGINS}" | ||
echo " SALT: ${SALT}" | ||
echo | ||
export PGHOST=$DB_HOST | ||
export PGPORT=$DB_PORT | ||
export PGDATABASE=$DB_NAME | ||
export PGUSER=$DB_USER | ||
export PGPASSWORD=$DB_PASS | ||
fi | ||
|
||
if [ -d .venv ] | ||
then | ||
echo "-- Python Virtual Environment" | ||
source .venv/bin/activate | ||
echo " $(python3 --version)" | ||
export PYTHONPATH=$(pwd) | ||
echo " PYTHONPATH: ${PYTHONPATH}" | ||
echo | ||
echo "-- Poetry" | ||
export PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring | ||
echo " $(poetry --version)" | ||
echo | ||
echo "-- FastAPI 127.0.0.1:8000" | ||
alias arrancar="uvicorn --host=127.0.0.1 --port 8000 --reload pjecz_hercules_api_oauth2.main:app" | ||
echo " arrancar" | ||
echo | ||
if [ -d tests ] | ||
then | ||
echo "-- Pruebas unitarias" | ||
echo " python3 -m unittest discover" | ||
echo | ||
fi | ||
fi | ||
``` | ||
|
||
## Arrancar | ||
|
||
Cargar las variables de entorno y el entorno virtual | ||
|
||
```bash | ||
source .bashrc | ||
``` | ||
|
||
Lanzar **FastAPI** por medio del _alias_ que se cargó en el `source .bashrc` | ||
|
||
```bash | ||
arrancar | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Cifrado y descrifado de ID por medio de Hashids | ||
""" | ||
|
||
import re | ||
from typing import Any | ||
|
||
from fastapi import Depends | ||
from hashids import Hashids | ||
|
||
from ..settings import Settings, get_settings | ||
|
||
HASHID_REGEXP = re.compile("[0-9a-zA-Z]{8,16}") | ||
|
||
|
||
def cifrar_id( | ||
un_id: int, | ||
settings: Settings = Depends(get_settings), | ||
) -> str: | ||
"""Cifrar ID""" | ||
hashids = Hashids(settings.salt, min_length=8) | ||
return hashids.encode(un_id) | ||
|
||
|
||
def descifrar_id( | ||
un_id_hasheado: str, | ||
settings: Settings = Depends(get_settings), | ||
) -> Any: | ||
"""Descifrar ID""" | ||
hashids = Hashids(settings.salt, min_length=8) | ||
if HASHID_REGEXP.match(un_id_hasheado): | ||
pag_pago_id = hashids.decode(un_id_hasheado) | ||
if len(pag_pago_id) == 1: | ||
return pag_pago_id[0] | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ class OneBaseOut(BaseModel): | |
|
||
success: bool | ||
message: str | ||
data: list[T] = None | ||
data: list[T] | None = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[project] | ||
name = "pjecz-hercules-api-oauth2" | ||
version = "0.1.0" | ||
description = "" | ||
description = "PJECZ API OAuth2 de Plataforma Web" | ||
authors = [ | ||
{name = "Guillermo Valdes",email = "[email protected]"} | ||
] | ||
|