I have been trying to wrap my head around mock api services, such as the one that Stoplight provides. I want to create something similar, but I keep hitting a wall and I can’t imagine why it should be so hard to figure out. Here’s the details.
Stoplight has a feature where each project has its own mock api server. The mock server works out of the Open API spec (OAS) specifically for that project. This means that every time someone creates a new project, a new mock server is also created. I assume this happens automatically behind the scenes through some scripting, unless it’s a truly dynamic process. Stoplight has an open source mock server project called Prism, and I can only assume thats is what is used as the tool for the mock servers.
When you run Prism, you must give it a url for the OAS. That means that one instance of the server can handle one OAS. Based on this info, Stoplight would need to spin up a Docker instance for each new project. That would end up with a lot of containers, most of them probably never used. Thats doesn’t seem like a very good solution, so they must do something else.
Prism also has the ability to serve multiple OAS through something they call reverse proxy. This means that you can use one base URL to server many different OAS. However, behind the scenes, Prism is still running one container for each OAS. This is the most likely situation so far, but I can’t figure out how new mock servers are created in a dynamic fashion.
Does anyone have any input on how to achieve such a scenario? Is one Docker container per OAS really the way to go? And if so, what would I need to do to start, stop and reload containers from my backend?
2
Answers
You could create a mock API server in Node.js to support multiple APIs by using prism’s libraries. The key one you should look into is https://npmjs.com/package/@stoplight/prism-http.
Create a webserver with Express or any other web framework, and create prism-http instances per OpenAPI definition. Route incoming requests to different prism-http instances based on the base path defined in ‘servers’ in OpenAPI file.
A note: the OAS file needs to have an entry without host part to make prism-http work.
You can try using APIGit, a tool that utilizes Git technology to manage API specifications. This allows you to create a repository and import your OpenAPI Specification (OAS) files, and generate mock server scripts directly from the selected OAS. The tool’s native Git support enables you to publish and maintain multiple versions of mock servers simultaneously, making it a convenient solution for API development and testing.
automatic-mockserver-generation