Go + Oracle - CRUD Simples

API HTTP simples em Go usando net/http e Oracle.

O exemplo conecta no Oracle Free Full 23ai que foi configurado em:

repositorio/oracle/versoes/free-full-23ai

Objetivo

Demonstrar um CRUD mínimo, didático e moderno usando:

  • Go;
  • net/http com rotas por método e path;
  • database/sql;
  • driver Oracle puro Go github.com/sijms/go-ora/v2;
  • httptest nos testes;
  • Oracle Free 23ai em container Podman.

Endpoint

/api/v1/llm

Métodos disponíveis:

GET    /health
GET    /api/v1/llm
GET    /api/v1/llm?id=1
POST   /api/v1/llm
PUT    /api/v1/llm?id=1
DELETE /api/v1/llm?id=1

Conexão Padrão

A aplicação usa por padrão:

ORACLE_DSN=oracle://system:OraclePwd123@localhost:1522/FREEPDB1
HTTP_ADDR=:8080

Você pode sobrescrever:

ORACLE_DSN='oracle://system:OraclePwd123@localhost:1522/FREEPDB1' HTTP_ADDR=':8080' go run .

Quick Start

1. Subir Oracle

Se ainda não estiver rodando:

cd repositorio/oracle/versoes/free-full-23ai/script
./up.sh

Acompanhe os logs até aparecer DATABASE IS READY TO USE:

podman logs -f oracle-free-full-23ai

2. Criar Tabela

Conecte em Oracle SQL Developer, CloudBeaver, DBeaver ou equivalente usando:

Host: localhost
Port: 1522
Service Name: FREEPDB1
User: system
Password: OraclePwd123

Execute:

CREATE TABLE modelos_llms (
  id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  nome VARCHAR2(120) NOT NULL,
  input_token_limit NUMBER NOT NULL,
  output_token_limit NUMBER NOT NULL,
  provider VARCHAR2(80) DEFAULT 'openai' NOT NULL,
  created_at TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL
);

3. Rodar API

cd repositorio/go.oracle/v1
go run .

Ao iniciar, a aplicação lista os endpoints disponíveis no terminal.

4. Healthcheck

curl -s http://localhost:8080/health

Resposta esperada:

{"status":"ok"}

Curls Para Teste

Criar Modelo

curl -s -X POST http://localhost:8080/api/v1/llm \
  -H 'Content-Type: application/json' \
  -d '{
    "nome": "gpt-5.4",
    "input_token_limit": 200000,
    "output_token_limit": 128000,
    "provider": "openai"
  }'

Listar Modelos

curl -s http://localhost:8080/api/v1/llm

Buscar Por ID

curl -s 'http://localhost:8080/api/v1/llm?id=1'

Atualizar Modelo

curl -s -X PUT 'http://localhost:8080/api/v1/llm?id=1' \
  -H 'Content-Type: application/json' \
  -d '{
    "nome": "gpt-5.4-mini",
    "input_token_limit": 128000,
    "output_token_limit": 64000,
    "provider": "openai"
  }'

Remover Modelo

curl -s -X DELETE 'http://localhost:8080/api/v1/llm?id=1'

Resposta esperada:

{"deleted":true,"id":1}

Testes

go test ./...

Os testes usam apenas biblioteca padrão, incluindo net/http/httptest.

Logs

A aplicação usa slog apenas para eventos simples dos handlers e conexão com Oracle.

Exemplo:

level=INFO msg="oracle conectado" dsn=oracle://system:OraclePwd123@localhost:1522/FREEPDB1
Servidor HTTP iniciado em :8080
Endpoints disponíveis:
  GET    /health
  GET    /api/v1/llm
  GET    /api/v1/llm?id=1
  POST   /api/v1/llm
  PUT    /api/v1/llm?id=1
  DELETE /api/v1/llm?id=1

Arquivos

v1/
├── README.md
├── go.mod
├── go.sum
├── main.go
└── main_test.go