Features
Try-It Console
pkg/tryit provides the configuration types behind an interactive API console: the console settings, environment management, and request history.
import "github.com/gopackx/open-swag-go/pkg/tryit"ConsoleConfig
type ConsoleConfig struct {
Enabled bool // Enable the console
DefaultServer string // Default server URL
RequestTimeout int // Request timeout (milliseconds)
ShowCodeSnippets bool // Show generated code snippets
EnabledLanguages []string // Snippet languages
CustomHeaders map[string]string // Headers attached to every request
ProxyURL string // Proxy URL for requests
CORSProxy bool // Enable a CORS proxy for browser requests
}Build one with NewConsole and functional options (it starts from DefaultConsoleConfig()):
console := tryit.NewConsole(
tryit.WithDefaultServer("http://localhost:8080"),
tryit.WithTimeout(30000),
)Options
| Option | Effect |
|---|---|
WithTimeout(ms int) | Set the request timeout in milliseconds |
WithDefaultServer(url string) | Set the default server URL |
WithLanguages(langs ...string) | Choose snippet languages |
WithCustomHeader(key, value string) | Add a header to every request |
WithProxy(url string) | Route requests through a proxy |
WithCORSProxy(enabled bool) | Enable a CORS proxy for browser requests |
DisableSnippets() | Hide generated code snippets |
Disable() | Disable the console |
Custom headers
console := tryit.NewConsole(
tryit.WithCustomHeader("X-API-Version", "2024-01-01"),
tryit.WithCustomHeader("X-Source", "docs-console"),
)Environments
EnvironmentManager lets you define multiple environments and interpolate {{variable}} references.
type Environment struct {
Name string // Display name (e.g. "Production")
Variables map[string]string // Variables available for interpolation
IsActive bool // Whether this is the active environment
}mgr := tryit.NewEnvironmentManager(tryit.DefaultEnvironmentConfig())
mgr.Add(tryit.Environment{
Name: "Development",
Variables: map[string]string{"base_url": "http://localhost:8080"},
})
mgr.Add(tryit.Environment{
Name: "Production",
Variables: map[string]string{"base_url": "https://api.example.com"},
})
mgr.SetActive("Production")
url := mgr.Interpolate("{{base_url}}/users") // "https://api.example.com/users"Request History
type HistoryConfig struct {
Enabled bool
MaxEntries int
Storage string
StorageKey string
}hist := tryit.NewHistory(tryit.HistoryConfig{
Enabled: true,
MaxEntries: 100,
})
hist.Add(tryit.HistoryEntry{
Method: "GET",
URL: "https://api.example.com/users",
StatusCode: 200,
})
entries := hist.Get()When MaxEntries is reached, the oldest entry is evicted. See the Try-It API reference for the full type and method list.
Full Example
package main
import (
"fmt"
"github.com/gopackx/open-swag-go/pkg/tryit"
)
func main() {
console := tryit.NewConsole(
tryit.WithDefaultServer("https://api.example.com"),
tryit.WithCustomHeader("X-API-Version", "2024-01-01"),
tryit.WithLanguages("curl", "javascript", "go", "python"),
tryit.WithCORSProxy(true),
)
fmt.Printf("console enabled: %v, timeout: %dms\n",
console.Enabled, console.RequestTimeout)
}