Unimock Example
This example demonstrates how to use Unimock as a library with in-code configuration.
How It Works
The example shows:
- Creating a mock configuration with API endpoint patterns
- Setting up server configuration
- Initializing and running the Unimock server
- Handling graceful shutdown
Running the Example
Run the example with:
go run .
This will start a mock server on port 8081. You can then make requests to the server:
# Create a user
curl -X POST -H "Content-Type: application/json" -d '{"id":"123","name":"Test User"}' http://localhost:8081/users
# Get the user
curl http://localhost:8081/users/123
# Update the user
curl -X PUT -H "Content-Type: application/json" -d '{"id":"123","name":"Updated User"}' http://localhost:8081/users/123
# Delete the user
curl -X DELETE http://localhost:8081/users/123
Configuration Details
The example configures two API endpoints:
/api/* - For general API requests
/users/* - For user-specific requests
Each endpoint is configured with:
- Path pattern matching
- JSON body ID extraction paths
- Header-based ID extraction
Custom Configuration
To customize the configuration, modify the uniConfig and serverConfig objects in main.go:
// Server configuration
serverConfig := &config.ServerConfig{
Port: "8081", // Change the port
LogLevel: "debug", // Change log level: debug, info, warn, error
ConfigPath: "config.yaml", // Required but not used for in-code config
}
// Mock configuration
uniConfig := &config.UniConfig{
Sections: map[string]config.Section{
"custom": {
PathPattern: "/custom/*", // Custom path pattern
BodyIDPaths: []string{"/customId"}, // Custom ID paths
HeaderIDNames: []string{"X-Custom-ID"}, // Custom headers (multiple supported)
},
},
}