infracloud/OCI-API-GATEWAY.md

190 lines
5.2 KiB
Markdown

# OCI API Gateway - Configuração para MFEs
## Visão Geral
Este documento detalha como configurar o OCI API Gateway para servir Micro Frontends (MFEs) via Object Storage.
## Arquitetura
```
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Usuário │ ───► │ API Gateway │ ───► │ Object Storage │
│ (navegador) │ │ (URL pública) │ │ (PAR URL) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
```
## Fluxo Completo
### 1. Criar Bucket no Object Storage
```bash
# Via OCI CLI
oci os bucket create \
--namespace-name "grbb7qzeuoag" \
--name "mfe-user-dev" \
--compartment-id "ocid1.compartment.oc1..xxx" \
--storage-tier "Standard"
```
### 2. Fazer Upload dos Arquivos
```bash
# Upload via OCI CLI
cd dist/mfe-user/browser/
find . -type f | while read file; do
oci os object put \
--namespace-name "grbb7qzeuoag" \
--bucket-name "nexus-mfe-user-dev" \
--file "$file" \
--object-name "${file#./}" \
--content-type auto
done
```
### 3. Criar Pre-Authenticated Request (PAR)
```bash
# Criar PAR para acesso público
oci os preauth-request create \
--namespace-name "grbb7qzeuoag" \
--bucket-name "nexus-mfe-user-dev" \
--name "mfe-user-par" \
--access-type "ObjectRead" \
--time-expiry 31536000
```
### 4. Criar API Gateway
```bash
# Verificar/criar subnet
oci network subnet list \
--compartment-id "compartment-ocid" \
--vcn-id "vcn-ocid"
# Criar API Gateway
oci api-gateway gateway create \
--compartment-id "compartment-ocid" \
--display-name "nexus-mfe-user-gateway" \
--subnet-id "subnet-ocid" \
--endpoint-type "PUBLIC"
```
### 5. Criar Deployment com Rotas
O deployment precisa de rotas específicas para SPAs (Angular):
```yaml
routes:
# Rota principal - tudo para index.html (SPA)
- path: /{req.*}
methods: [GET]
backend:
type: HTTP
url: "${PAR_URL}/${request.path[req]}"
# Arquivos estáticos específicos
- path: /{ext}.js
methods: [GET]
backend:
type: HTTP
url: "https://grbb7qzeuoag.objectstorage.sa-saopaulo-1.oci.customer-oci.com/n/grbb7qzeuoag/b/nexus-mfe-user-dev/o/${request.path[req]}"
- path: /{ext}.css
methods: [GET]
backend:
type: HTTP
url: "https://grbb7qzeuoag.objectstorage.sa-saopaulo-1.oci.customer-oci.com/n/grbb7qzeuoag/b/nexus-mfe-user-dev/o/${request.path[req]}"
```
## Configuração via Terraform
### Módulo API Gateway
```hcl
module "api_gateway" {
source = "./modules/api_gateway"
compartment_id = var.compartment_id
subnet_id = var.subnet_id
name = "nexus-mfe-user"
environment = "dev"
}
module "api_gateway_deployment" {
source = "./modules/api_gateway_deployment"
gateway_id = module.api_gateway.gateway_id
bucket_name = "nexus-mfe-user-dev"
namespace = "grbb7qzeuoag"
region = "sa-saopaulo-1"
}
```
## Pipeline Azure DevOps - Fluxo
```
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Build │ ─► │ Upload │ ─► │ CreatePAR │ ─► │ Deploy API │
│ (npm build)│ │ (OCI OS) │ │ (OCI) │ │ (Gateway) │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
```
## URLs de Acesso
### Por Ambiente
| Ambiente | URL API Gateway |
|----------|----------------|
| Dev | `https://gateway-ocid.apigateway.sa-saopaulo-1.oci.customer-oci.com` |
| HML | `https://gateway-ocid.apigateway.sa-saopaulo-1.oci.customer-oci.com` |
| PROD | `https://gateway-ocid.apigateway.sa-saopaulo-1.oci.customer-oci.com` |
## Troubleshooting
### Problema: Arquivos não carregam
1. Verificar se PAR está ativa
2. Verificar tipos de conteúdo (content-type)
3. Verificar rotas no deployment
### Problema: SPA routing não funciona
1. Garantir rota catch-all `/{req.*}`
2. Backend deve resolver para index.html
### Problema: 403 Forbidden
1. Verificar se PAR não expirou
2. Verificar se bucket está público ou PAR está válida
## Comandos Úteis
```bash
# Listar API Gateways
oci api-gateway gateway list --compartment-id <compartment-id>
# Ver deployment
oci api-gateway deployment get --deployment-id <deployment-id>
# Atualizar deployment
oci api-gateway deployment update \
--deployment-id <deployment-id> \
--spec file://deployment-spec.yaml
# Listar PARs
oci os preauth-request list \
--namespace-name "grbb7qzeuoag" \
--bucket-name "nexus-mfe-user-dev"
# Deletar PAR
oci os preauth-request delete \
--namespace-name "grbb7qzeuoag" \
--bucket-name "nexus-mfe-user-dev" \
--par-id <par-id>
```
## Referências
- [OCI API Gateway Docs](https://docs.oracle.com/en-us/iaas/Content/APIGateway/apigateway.htm)
- [Static Website Hosting](https://docs.oracle.com/en/learn/oci-api-gateway-web-hosting/index.html)