Skip to content

Commit 7eaacad

Browse files
hotfix/bug-server
2 parents 0b06b31 + e7fb1ac commit 7eaacad

File tree

5 files changed

+123
-50
lines changed

5 files changed

+123
-50
lines changed

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI - Argus IA
2+
3+
on:
4+
push:
5+
branches: [ "main", "develop" ]
6+
pull_request:
7+
branches: [ "main", "develop" ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout do código
15+
uses: actions/checkout@v3
16+
17+
- name: Configurar Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: "3.10"
21+
22+
- name: Instalar dependências
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install -r requirements.txt || true
26+
27+
- name: Rodar testes (opcional – se adicionar testes)
28+
run: |
29+
echo "Nenhum teste configurado por enquanto"
30+
continue-on-error: true
31+
32+
- name: Build Docker
33+
uses: docker/setup-buildx-action@v2
34+
35+
- name: Construir imagem Docker
36+
run: |
37+
docker build -t argusia .
38+
39+
- name: Verificar sucesso do build
40+
run: |
41+
echo "Build do Docker finalizado com sucesso!"

Dockerfile

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
1-
# Use uma imagem oficial do Python
1+
# Use imagem oficial do Python
22
FROM python:3.10-slim
33

4-
# Defina o diretório de trabalho dentro do container
4+
# Diretório de trabalho
55
WORKDIR /app
66

7-
# Copie os arquivos de requirements e instale as dependências
7+
# Configurações
8+
ENV PYTHONDONTWRITEBYTECODE=1
9+
ENV PYTHONUNBUFFERED=1
10+
11+
# Dependências do sistema
12+
RUN apt-get update && apt-get install -y \
13+
gcc \
14+
libpq-dev \
15+
&& rm -rf /var/lib/apt/lists/*
16+
17+
# Copiar requirements
818
COPY requirements.txt .
19+
920
RUN pip install --upgrade pip
1021
RUN pip install -r requirements.txt
1122

12-
# Copie todo o projeto para dentro do container
23+
# Copiar projeto
1324
COPY . .
1425

15-
# Exponha a porta que o Django vai rodar
26+
# Coletar estáticos
27+
RUN python manage.py collectstatic --noinput
28+
29+
# Expor porta
1630
EXPOSE 8000
1731

18-
# Comando para rodar o servidor Django
19-
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
32+
# O Railway vai sobrescrever esse CMD com o startCommand
33+
CMD ["gunicorn", "argus_ia.wsgi:application", "--bind", "0.0.0.0:8000"]

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: mkdir -p staticfiles && python manage.py collectstatic --noinput && python manage.py migrate --noinput && gunicorn argus_ia.wsgi:application --bind 0.0.0.0:$PORT
1+
web: python manage.py migrate && gunicorn argus_ia.wsgi:application --bind 0.0.0.0:$PORT

argus_ia/settings.py

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
import os
66
from pathlib import Path
7+
import sys
78

89
BASE_DIR = Path(__file__).resolve().parent.parent
910

1011
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-dev-key-argus-ia-2024')
1112

1213
DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true'
1314

14-
ALLOWED_HOSTS = ['argus-ia.up.railway.app', '.railway.app', 'localhost', '127.0.0.1']
15+
ALLOWED_HOSTS = [
16+
h.strip() for h in os.environ.get(
17+
'ALLOWED_HOSTS',
18+
'argus-ia.up.railway.app,localhost,127.0.0.1'
19+
).split(',')
20+
]
1521

1622
INSTALLED_APPS = [
1723
'django.contrib.admin',
@@ -54,71 +60,83 @@
5460

5561
WSGI_APPLICATION = 'argus_ia.wsgi.application'
5662

57-
# Database Configuration
58-
# DATABASES
59-
DATABASES = {
60-
'default': {
61-
'ENGINE': 'django.db.backends.sqlite3',
62-
'NAME': BASE_DIR / 'db.sqlite3',
63-
}
64-
}
6563

64+
# ================= DATABASE =====================
6665
if 'DATABASE_URL' in os.environ:
6766
import dj_database_url
68-
DATABASES['default'] = dj_database_url.config(
69-
conn_max_age=600,
70-
ssl_require=False
71-
)
67+
DATABASES = {
68+
'default': dj_database_url.config(
69+
conn_max_age=600,
70+
conn_health_checks=True,
71+
ssl_require=False
72+
)
73+
}
74+
else:
75+
DATABASES = {
76+
'default': {
77+
'ENGINE': 'django.db.backends.sqlite3',
78+
'NAME': BASE_DIR / 'db.sqlite3',
79+
}
80+
}
81+
7282

73-
# Password validation
83+
# ================= PASSWORD =====================
7484
AUTH_PASSWORD_VALIDATORS = [
75-
{
76-
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
77-
},
78-
{
79-
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
80-
},
81-
{
82-
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
83-
},
84-
{
85-
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
86-
},
85+
{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'},
86+
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'},
87+
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'},
88+
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'},
8789
]
8890

91+
92+
# ================= LOCALE =====================
8993
LANGUAGE_CODE = 'pt-br'
9094
TIME_ZONE = 'America/Sao_Paulo'
9195
USE_I18N = True
9296
USE_TZ = True
9397

94-
# Static files
98+
99+
# ================= STATIC FILES =====================
95100
STATIC_URL = '/static/'
96101
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
97-
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
98102

99-
# Whitenoise configuration
100-
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
103+
if DEBUG:
104+
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
101105

102-
# Corrige o warning do Whitenoise
106+
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
103107
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'staticfiles')
104108

109+
105110
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
106111

107-
# Session configuration
112+
113+
# ================= SESSION =====================
108114
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
109-
SESSION_COOKIE_AGE = 3600 # 1 hora
115+
SESSION_COOKIE_AGE = 3600
110116
SESSION_SAVE_EVERY_REQUEST = True
111117
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
112118

113-
# CSRF configuration
119+
120+
# ================= CSRF =====================
114121
CSRF_TRUSTED_ORIGINS = [
115122
'https://argus-ia.up.railway.app',
116-
'https://*.railway.app'
123+
'https://*.railway.app',
117124
]
118125

119-
# if not DEBUG:
120-
# SECURE_SSL_REDIRECT = True
121-
# SESSION_COOKIE_SECURE = True
122-
# CSRF_COOKIE_SECURE = True
123-
# SECURE_BROWSER_XSS_FILTER = True
124-
# SECURE_CONTENT_TYPE_NOSNIFF = True
126+
# Segurança em produção — NÃO aplicar no ambiente local
127+
if not DEBUG and not ('localhost' in ALLOWED_HOSTS or '127.0.0.1' in ALLOWED_HOSTS):
128+
SECURE_SSL_REDIRECT = True
129+
SESSION_COOKIE_SECURE = True
130+
CSRF_COOKIE_SECURE = True
131+
SECURE_BROWSER_XSS_FILTER = True
132+
SECURE_CONTENT_TYPE_NOSNIFF = True
133+
SECURE_HSTS_SECONDS = 31536000
134+
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
135+
SECURE_HSTS_PRELOAD = True
136+
else:
137+
# Ambiente local — não usar SSL
138+
SECURE_SSL_REDIRECT = False
139+
SESSION_COOKIE_SECURE = False
140+
CSRF_COOKIE_SECURE = False
141+
142+
PORT = os.environ.get('PORT', 8000)

railway.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"builder": "NIXPACKS"
44
},
55
"deploy": {
6-
"startCommand": "python manage.py migrate --noinput && gunicorn argus_ia.wsgi:application --bind 0.0.0.0:$PORT",
6+
"startCommand": "python manage.py migrate && gunicorn argus_ia.wsgi:application --bind 0.0.0.0:$PORT",
77
"restartPolicyType": "ON_FAILURE",
88
"restartPolicyMaxRetries": 10
99
}

0 commit comments

Comments
 (0)