FYI, this worked really well. Here is the config I used. You effectively have a way to manage to define routes in your OAS spec without having to duplicate them in your CaddyFile. Its a good “API first” approach.
FYI: @chukmunnlee and @jhonnmick
CaddyFile
{
order oas_validator before reverse_proxy
order request_id before reverse_proxy
order circuit_breaker before reverse_proxy
metrics
debug
log {
output stdout
format json
}
}
:8080 {
# HTTPBin API endpoints
handle /api/* {
request_id
oas_validator {
spec /etc/caddy/openapi/httpbin-api.json
{$DEBUG:+fall_through}
{$DEBUG:+log_error}
{$DEBUG:+multi_error}
}
uri strip_prefix /api
reverse_proxy httpbin:80
}
# Echo API endpoints
handle /echo* {
request_id
oas_validator {
spec /etc/caddy/openapi/echo-api.json
{$DEBUG:+log_error}
}
reverse_proxy echo:80
}
# Catch-all for unmatched paths
handle {
respond "Not Found" 404
}
}
:{$MONITORING_PORT:5555} {
handle {$HEALTH_PATH:/health} {
respond "OK" 200
}
handle {$METRICS_PATH:/metrics} {
metrics {$METRICS_PATH:/metrics}
}
}
OAS for echo-api
{
"openapi": "3.0.0",
"info": {
"title": "Echo API",
"version": "1.0.0",
"description": "Echo server endpoints"
},
"servers": [
{
"url": "http://localhost:8080"
}
],
"paths": {
"/echo": {
"get": {
"operationId": "echoGet",
"summary": "Echo GET request",
"description": "Echoes back request information",
"parameters": [
{
"name": "request-id",
"in": "header",
"required": false,
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object"
},
"example": {
"method": "GET",
"headers": {
"request-id": "123e4567-e89b-12d3-a456-426614174000"
},
"query": {},
"body": null
}
}
}
},
"500": {
"description": "Internal server error"
}
}
},
"post": {
"operationId": "echoPost",
"summary": "Echo POST request",
"description": "Echoes back POST request data",
"parameters": [
{
"name": "request-id",
"in": "header",
"required": false,
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
},
"example": {
"message": "Hello World"
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object"
},
"example": {
"method": "POST",
"headers": {
"content-type": "application/json",
"request-id": "123e4567-e89b-12d3-a456-426614174000"
},
"body": {
"message": "Hello World"
}
}
}
}
},
"400": {
"description": "Bad request"
},
"500": {
"description": "Internal server error"
}
}
},
"put": {
"operationId": "echoPut",
"summary": "Echo PUT request",
"description": "Echoes back PUT request data",
"parameters": [
{
"name": "request-id",
"in": "header",
"required": false,
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
},
"example": {
"id": 1,
"data": "Updated content"
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object"
},
"example": {
"method": "PUT",
"headers": {
"content-type": "application/json",
"request-id": "123e4567-e89b-12d3-a456-426614174000"
},
"body": {
"id": 1,
"data": "Updated content"
}
}
}
}
},
"400": {
"description": "Bad request"
},
"500": {
"description": "Internal server error"
}
}
}
},
"/echo/users/{userId}": {
"get": {
"operationId": "getUser",
"summary": "Get user by ID",
"parameters": [
{
"name": "userId",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "User data",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
},
"/echo/users": {
"get": {
"operationId": "getUsers",
"summary": "Get users list",
"parameters": [
{
"name": "limit",
"in": "query",
"required": true,
"schema": {
"type": "integer",
"minimum": 1
}
}
],
"responses": {
"200": {
"description": "Users list",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object"
}
}
}
}
}
}
}
}
}
}