Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewServer ¶
func NewServer(serverConfig *config.ServerConfig, uniConfig *config.UniConfig) (*http.Server, error)
NewServer initializes a new HTTP server with the provided configurations. Both serverConfig and uniConfig parameters are required.
If serverConfig is nil, default values will be used:
- Port: "8080"
- LogLevel: "info"
uniConfig must be non-nil and contain at least one section or scenario.
Usage examples:
1. Using environment variables and file config:
// Load server configuration from environment variables
// UNIMOCK_PORT and UNIMOCK_LOG_LEVEL
serverConfig := config.FromEnv()
// Load unified configuration from file
uniConfig, err := config.LoadFromYAML(serverConfig.ConfigPath)
if err != nil {
log.Fatal(err)
}
// Initialize server
srv, err := pkg.NewServer(serverConfig, uniConfig)
2. Using direct configuration:
// Create server configuration
serverConfig := &config.ServerConfig{
Port: "8080",
LogLevel: "debug",
}
// Create unified configuration
uniConfig := &config.UniConfig{
Sections: map[string]config.Section{
"users": {
PathPattern: "/users/*",
BodyIDPaths: []string{"/id"},
HeaderIDNames: []string{"X-User-ID"},
},
},
Scenarios: []config.ScenarioConfig{
{
Method: "GET",
Path: "/api/test",
StatusCode: 200,
Data: `{"message": "test"}`,
},
},
}
// Initialize server
srv, err := pkg.NewServer(serverConfig, uniConfig)
For a complete server setup:
srv, err := pkg.NewServer(serverConfig, uniConfig)
if err != nil {
log.Fatal(err)
}
// Start the server
log.Printf("Listening on %s", srv.Addr)
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
Types ¶
type ConfigError ¶
type ConfigError struct {
Message string
}
ConfigError represents a configuration error
func (*ConfigError) Error ¶
func (e *ConfigError) Error() string
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client provides an HTTP client for interacting with Unimock APIs and mock endpoints.
|
Package client provides an HTTP client for interacting with Unimock APIs and mock endpoints. |
|
Package config provides configuration structures for the Unimock server.
|
Package config provides configuration structures for the Unimock server. |
|
Package model provides data structures for Unimock's HTTP mocking and scenarios.
|
Package model provides data structures for Unimock's HTTP mocking and scenarios. |
Click to show internal directories.
Click to hide internal directories.