Search code examples
openapistoplight

Serve static content in Stoplight Prism mock server


I have OpenAPI file describes my API design, using the Stoplight Prism to provide a mock server for the front-end developers by their Docker image stoplight/prism:4

My question is, how can I serve static content within that server?

I need to provide an API documentation for the front-end developers to know how to use the API, I have this documentation as HTML file, for now I serve this documentation by a separate web server, how can I include it within Prism?

All my API endpoints are available at /api/ path, can I serve my HTML file at something like /static/doc.html?


Solution

  • Prism CLI doesn't support serving static file. A simple approach is to use Docker Compose to start the mock server and web server.

    Below example bases on the way described in Serving Multiple OpenAPI Documents, to use Caddy as both reverse proxy and static file server.

    ❯ tree
    .
    ├── Caddyfile
    ├── docker-compose.yaml
    ├── html
    │   └── index.html
    └── myapi.yaml
    

    docker-compose.yaml:

    version: '3'
    services:
      proxy:
        image: caddy
        volumes:
          - ${PWD}/Caddyfile:/etc/caddy/Caddyfile
          - ${PWD}/html:/var/www/static
        ports:
          - '8080:80'
        depends_on:
          - prism
      prism:
        image: stoplight/prism:4
        volumes:
          - ./myapi.yaml:/usr/src/prism/packages/cli/myapi.yaml
        command: >
          mock -p 4010 --host 0.0.0.0 myapi.yaml
    

    Caddyfile:

    http:// {
        root * /var/www
        reverse_proxy /api/* prism:4010
        file_server /static/*
    }
    

    After run the command docker-compose up, you can view document on http://127.0.0.1:8080/static and call API on http://127.0.0.1:8080/api/....