chukmunnlee / caddy-openapi

A Caddy module to validate HTTP request and response against a OpenAPI spec (V3) file
Apache License 2.0
23 stars 7 forks source link

Is it possible to have a URL prefix (i.e. /api/) that's not part of the specification? #1

Closed benjaoming closed 2 years ago

benjaoming commented 2 years ago

Thanks for an amazing project! Very useful for bootstrapping new API-driven projects!

Looking at how specifications are conventionally done, they don't include for instance /api/v1 or /api/ in theirpath` components.

Is it possible to write a configuration where the specification is nested inside /some/path/* but /some/path/* does not have to be written into the paths of the specification file?

(I tried to work around this, but couldn't find a way)

chukmunnlee commented 2 years ago

Hi Ben thank you for using my project.

Try the following

:8080 {
  @api {
    path /api/*
  }

  route @api {
    uri strip_prefix /api
    reverse_proxy {
      to localhost:3000
    }
    openapi {
      spec ./customer.yaml
      policy_bundle ../policy/bundle.tar.gz
      check {
        req_body
        resp_body
      }
      log_error
    }
  }
  handle_errors {
    respond @api "Resource: {http.request.orig_uri}. Error: {openapi.error}" {openapi.status_code} {
      close
    }
  }
}

Remove all /api references from your OAS spec and your code.

HTH