Commit inicial - upload de todos os arquivos da pasta
This commit is contained in:
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
# Python-generated files
|
||||
__pycache__/
|
||||
*.py[oc]
|
||||
build/
|
||||
dist/
|
||||
wheels/
|
||||
*.egg-info
|
||||
|
||||
# Virtual environments
|
||||
.venv
|
||||
|
||||
|
||||
*.json
|
||||
*.sqlite3*
|
||||
1
.python-version
Normal file
1
.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.10
|
||||
25
Dockerfile
Normal file
25
Dockerfile
Normal file
@@ -0,0 +1,25 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y \
|
||||
sqlite3 \
|
||||
curl \
|
||||
dos2unix \
|
||||
wget \
|
||||
unzip \
|
||||
&& apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||
|
||||
ENV PATH="/root/.local/bin:${PATH}"
|
||||
|
||||
RUN uv sync
|
||||
|
||||
RUN dos2unix run.sh && chmod +x run.sh
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["bash", "run.sh"]
|
||||
7
Dockerfile.test
Normal file
7
Dockerfile.test
Normal file
@@ -0,0 +1,7 @@
|
||||
FROM python:3.11-slim
|
||||
|
||||
RUN apt-get update && apt-get install -y curl findutils
|
||||
|
||||
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||
|
||||
RUN find / -name uv -type f || echo "uv not found"
|
||||
30
github.bat
Normal file
30
github.bat
Normal file
@@ -0,0 +1,30 @@
|
||||
@echo off
|
||||
echo === INICIANDO UPLOAD PARA GITHUB ===
|
||||
|
||||
REM Inicializar repositório Git
|
||||
echo Inicializando repositorio Git...
|
||||
git init
|
||||
|
||||
REM Adicionar todos os arquivos
|
||||
echo Adicionando todos os arquivos...
|
||||
git add .
|
||||
|
||||
REM Fazer commit inicial
|
||||
echo Realizando commit inicial...
|
||||
git commit -m "Commit inicial - upload de todos os arquivos da pasta"
|
||||
|
||||
REM Adicionar repositório remoto
|
||||
echo Conectando ao repositorio remoto...
|
||||
git remote add origin https://gitea.aplicativopro.com/wander/CNES.git
|
||||
|
||||
REM Definir branch principal
|
||||
echo Definindo branch principal como 'main'...
|
||||
git branch -M main
|
||||
|
||||
REM Fazer push para o GitHub
|
||||
echo Fazendo upload para o GitHub...
|
||||
git push -u origin main
|
||||
|
||||
echo === UPLOAD CONCLUIDO COM SUCESSO! ===
|
||||
|
||||
pause
|
||||
29
main.py
Normal file
29
main.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import sqlite3
|
||||
import threading
|
||||
from fastapi import FastAPI
|
||||
|
||||
class SQLiteManager:
|
||||
def __init__(self, db_path: str):
|
||||
self.db_path = db_path
|
||||
self.connection = sqlite3.connect(db_path, check_same_thread=False)
|
||||
self.connection.row_factory = sqlite3.Row
|
||||
self.connection.execute("PRAGMA journal_mode=WAL")
|
||||
self.lock = threading.RLock()
|
||||
|
||||
def query(self, sql: str, params: tuple = ()):
|
||||
with self.lock:
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute(sql, params)
|
||||
return cursor.fetchone()
|
||||
|
||||
db_manager = SQLiteManager("db.sqlite3")
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/cnes/{cnes_code}")
|
||||
async def get_cnes(cnes_code: str):
|
||||
result = db_manager.query(
|
||||
"SELECT * FROM cnes_estabelecimentos WHERE CO_CNES = ? LIMIT 1",
|
||||
(cnes_code,)
|
||||
)
|
||||
return dict(result) if result else None
|
||||
20
make_database.py
Normal file
20
make_database.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import json
|
||||
import sqlite3
|
||||
|
||||
with open('cnes_estabelecimentos.json', 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
conn = sqlite3.connect('db.sqlite3')
|
||||
cursor = conn.cursor()
|
||||
|
||||
if data:
|
||||
columns = ', '.join([f"{key} TEXT" for key in data[0].keys()])
|
||||
cursor.execute(f"CREATE TABLE IF NOT EXISTS cnes_estabelecimentos ({columns})")
|
||||
|
||||
placeholders = ', '.join(['?' for _ in data[0].keys()])
|
||||
for record in data:
|
||||
cursor.execute(f"INSERT INTO cnes_estabelecimentos VALUES ({placeholders})",
|
||||
list(record.values()))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
3
prepare_data.sh
Normal file
3
prepare_data.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
wget https://s3.sa-east-1.amazonaws.com/ckan.saude.gov.br/CNES/cnes_estabelecimentos_json.zip
|
||||
unzip cnes_estabelecimentos_json.zip
|
||||
rm cnes_estabelecimentos_json.zip
|
||||
20
pyproject.toml
Normal file
20
pyproject.toml
Normal file
@@ -0,0 +1,20 @@
|
||||
[project]
|
||||
name = "cnes-api"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"fastapi[standard]>=0.116.1",
|
||||
"ipykernel>=6.30.1",
|
||||
"pandas>=2.3.1",
|
||||
"pyarrow>=21.0.0",
|
||||
"requests>=2.32.4",
|
||||
"unidecode>=1.4.0",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
[tool.setuptools]
|
||||
py-modules = ["main", "make_database", "continue_tutorial", "test"]
|
||||
6
run.sh
Normal file
6
run.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||
uv sync
|
||||
sh prepare_data.sh
|
||||
uv run python make_database.py
|
||||
uv run fastapi dev main.py --host 0.0.0.0 --port 8000
|
||||
44
test.py
Normal file
44
test.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import requests
|
||||
|
||||
cnes_codes = [
|
||||
"4261062",
|
||||
"9018255",
|
||||
"5973147",
|
||||
"2205734",
|
||||
"9039503",
|
||||
"7354754",
|
||||
"6717014",
|
||||
"2168618",
|
||||
"2768488",
|
||||
"103845",
|
||||
"926191",
|
||||
"7823924",
|
||||
"5509844",
|
||||
"90734",
|
||||
"7092377",
|
||||
"4508166",
|
||||
"9342710",
|
||||
"506648",
|
||||
"7916728",
|
||||
"7782594",
|
||||
"2776790",
|
||||
"9738142",
|
||||
"9384162",
|
||||
"6335225",
|
||||
"5227200",
|
||||
"8851912",
|
||||
"499811",
|
||||
"6797091",
|
||||
"9636072",
|
||||
"6516181",
|
||||
"7866766",
|
||||
"1469334",
|
||||
"6734464",
|
||||
"3097264",
|
||||
]
|
||||
|
||||
|
||||
for cnes_code in cnes_codes:
|
||||
print(
|
||||
f"{cnes_code} -> {requests.get(f'http://127.0.0.1:8000/cnes/{cnes_code}').text}"
|
||||
)
|
||||
Reference in New Issue
Block a user