Modern Web application development process typically involves frontend and backend development. Frontend presents business logic to the user via API provided by backend. The design of API is crucial to the entire project. In the early stage of the development, the frontend and backend developers work in parallel on a common design goal. In this stage, an API mock tool is desirable. Such tool is beneficial to the frontend developers to allow them to immediately test design ideas that involves querying on backend API. It also good for backend developers to conform to the design goal while sprinting to their minimal viable product.
This project aims for a configurable API mock tool. The user can create an endpoint and define its behavior by POSTing a configuration json to the API mock server. Afterwards, the endpoint responses to various queries from any user based on such configuration.
The API mock server is optimized to be
responsive and it can survive modest amount
of concurrent incoming requests.
This is my first serious Rust project. Please open issues on whatever reproducible problems you have ever encountered.
The project is ready to be compiled by cargo build
.
However, additional preparation on environment
is necessary, namely:
- Server port: the env var
MOCK_SERVER_PORT
defines te server port. If it is not privided, the server listens on port 53500 on the localhost. - Data root folder: the env var
MOCK_SERVER_DB_ROOT
defines the root folder where the server persists the data from the user. Such folder must exist and must contain a writable subfolder with nameprojects
.
To compile and test the code, run the following inside the folder where Cargo.toml
is located:
$ mkdir -p projects
$ export MOCK_SERVER_PORT=8001 && export RUST_BACKTRACE=1 && export MOCK_SERVER_DB_ROOT=`pwd` && cargo build && cargo test
To run the server, just modify the last word in the above command,
from test
to run
:
$ mkdir -p projects
$ export MOCK_SERVER_PORT=8001 && export RUST_BACKTRACE=1 && export MOCK_SERVER_DB_ROOT=`pwd` && cargo build && cargo run
The following instructions assumes that the API mock server is successfully launched at port 8001 on localhost.
To start off, you can check out the API documentation
at endpoint
http://localhost:8001/api-doc
, or directly open the file api-doc.html for help.
The API mock server provides the following endpoints:
- GET /projects/:name - Retrieve a project's configuration
- POST /projects/:name - Create a new project configuration
- PUT /projects/:name - Update an existing project configuration
- GET /projects/:project_name/:path - Mock an API endpoint based on project configuration
- POST /llm/:name - Generate a project configuration using a Language Model (LLM)
- GET /api-doc - Returns the API documentation page
-
Implemented full functionalities
-
Added
schema.rs
file as a format constraint for the configuration json -
Added LLM support, so that the user can create mock API from natural language.
-
Refactored codes
-
Performance optimization
-
Rewrote the regex for the endpoint with query string and redesigned the logic behind the
mock_request
-
Remove the code for thread pool and use
tikio
to handle concurrent requests. -
Added
/api-doc
page -
Added tests
More stress tests.
Avoid using third-party libraries as much as possible.
These data are used to match the request data.
operators: is
, is!
, contains
, contains!
{
"description": "my-project",
"endpoints": {
"/hello": {
"when": [
{
"method": "GET",
"request": {
"queries": {
"name": {
"operator": "is!",
"value": "foo"
}
},
"headers": {
"token": "go"
}
},
"response": {
"status": 200,
"body": {},
"headers": {}
},
"delay": 400
},
{
"method": "POST",
"request": {
"headers": {
"content-type": "xxx"
},
"body": {
"name": "foo"
}
},
"response": {
"status": 200,
"body": {},
"headers": {}
}
}
]
}
}
}