diff --git a/.gitignore b/.gitignore index d443543..1af7677 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ __pycache__/ /node_modules .env .pytest_cache -__pycache__ \ No newline at end of file +__pycache__ +schema.yml \ No newline at end of file diff --git a/README.md b/README.md index a96a801..c02d361 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Base de conhecimento TUPAN-BACK - ## Ambiente ### Criar, ativar e instalar as dependencias do ambiente virtual @@ -21,6 +20,17 @@ cp ./src/.env_sample ./src/.env ``` Logo em seguida altere as variáveis para os valores desejados. +## Swagger + +### Gerando o schema para o swagger +Execute o seguinte comando no terminal para gerar o .yml que será utilizado pelo swagger: +``` +python ./src/tupan/manage.py spectacular --color --file schema.yml +``` + +### Como acessar o swagger +Rode a aplicação e acesse o endpoint: +> /api/schema/swagger-ui ## Testes diff --git a/requirements.txt b/requirements.txt index 8aa8876..c49bda8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ python-dotenv==1.0.1 sqlparse==0.5.1 typing_extensions==4.12.2 tzdata==2024.1 +drf-spectacular \ No newline at end of file diff --git a/src/tupan/tupan/settings.py b/src/tupan/tupan/settings.py index 25a14d8..6dcf951 100644 --- a/src/tupan/tupan/settings.py +++ b/src/tupan/tupan/settings.py @@ -45,8 +45,20 @@ 'rest_framework', 'alertas', 'estacoes' + 'drf_spectacular', ] +REST_FRAMEWORK = { + 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', +} + +SPECTACULAR_SETTINGS = { + 'TITLE': 'Tupã API', + 'DESCRIPTION': '', + 'VERSION': '1.0.0', + 'SERVE_INCLUDE_SCHEMA': False, +} + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/src/tupan/tupan/urls.py b/src/tupan/tupan/urls.py index 903bfe4..2fd62b6 100644 --- a/src/tupan/tupan/urls.py +++ b/src/tupan/tupan/urls.py @@ -16,8 +16,13 @@ """ from django.contrib import admin from django.urls import include, path +from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView urlpatterns = [ path('admin/', admin.site.urls), - path('', include('estacoes.urls')) + path('', include('estacoes.urls')), + path('admin/', admin.site.urls), + path('api/schema/', SpectacularAPIView.as_view(), name='schema'), + path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), + path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), ]