DServer is a lightweight, configurable, and dynamic mock server built in Go. It allows developers to simulate RESTful APIs for testing and development purposes, using a simple configuration file.
- Dynamic Routing: Easily define API routes with support for different methods like GET, POST, etc.
- JSON Response: Serve customizable JSON responses out of the box.
- Delay Simulation: Simulate delays globally or at the per-response level to test timeout handling in clients.
- Hot Reloading: Automatically reloads configuration when the file is updated, ensuring seamless development.
- Fallback Logic:
- Use a global
response_body
if noresponses
array is provided. - Apply global defaults for
status_code
andcontent_type
anddelay_ms
where not explicitly overridden in responses.
- Use a global
- Go: Ensure you have Go (version 1.21 or higher) installed. Install Go.
- Clone the repository:
git clone https://github.com/kpiljoong/dserver.git
cd dserver
- Build the binary:
go build -o dserver
- Alternatively, run directly:
go run main.go
Define your API endpoints in a config.toml
file. Example:
[[routes]]
path = "/api/v1/resource"
method = "GET"
status_code = 200 # Global status code
delay_ms = 500 # Global delay in milliseconds
content_type = "application/json" # Global content type
response_body = """{
"message": "Fallback response"
}"""
[[routes]]
path = "/api/v1/resource"
method = "POST"
status_code = 201
content_type = "application/json"
responses = [
{ query = {}, response_body = """{ "message": "Default resources" }""" },
{ query = { "type" = "active" }, delay_ms = 1000, response_body = """{ "message": "Active resources" }""" },
{ query = { "type" = "inactive" }, response_body = """{ "message": "Inactive resources" }""" }
]
Start the server with:
./dserver -config=config.toml -port=8080
Flags:
-config
: Path to the configuration file (default:config.toml
).-port
: Port to run the server on (default:8080
).-verbose
: Enable verbose logging (default:false
).
Example:
./dserver -config=custom-config.toml -port=9090 -verbose
DServer supports default values at the [[routes]]
level:
status_code
: Used for all responses unless explicitly overridden.content_type
: Global content type for the route.delay_ms
: Default delay applied if not overridden in individual response.
Individual responses can override the global defaults:
status_code
content_type
delay_ms
If no responses
array is provided, the server falls back to the response_body
defined at the [[routes]]
level.
DServer automatically reloads the configuration file when it is updated. Simply edit the config.toml
file, and the changes will be applied without restarting the server.
Run the test suite:
go test ./...
Contributions are welcome! Feel free to open a pull request or create an issue for feature requests.
This project is licensed under the MIT License. See the LICENSE file for details.
- Built with Go.