Documentation
¶
Index ¶
- Constants
- Variables
- func ClearModulesCache()
- func DirectiveValues(content, directive string) ([]string, error)
- func FmtCode(content string) (fmtContent string, err error)
- func GetAccessLogPath() (path string)
- func GetConfEntryPath() (path string)
- func GetConfNameBySymlinkName(name string) string
- func GetConfPath(dir ...string) (confPath string)
- func GetConfSymlinkPath(path string) string
- func GetDirectives() (map[string]Directive, error)
- func GetErrorLogPath() string
- func GetLoadModuleRegex() *regexp.Regexp
- func GetLogLevel(output string) (level int)
- func GetModules() *orderedmap.OrderedMap[string, *Module]
- func GetModulesPath() string
- func GetNginxExeDir() string
- func GetPIDPath() (path string)
- func GetPrefix() string
- func GetSbinPath() (path string)
- func GetStatusSnapshot() (*ControlResult, *ControlOperation)
- func IsModuleLoaded(module string) bool
- func IsRunning() bool
- func Reload() (stdOut string, stdErr error)
- func Restart()
- func RewriteDirectiveValues(content string, replacements []DirectiveValueReplacement) (string, int, error)
- func TestConfig() (stdOut string, stdErr error)
- type ControlFunc
- type ControlOperation
- type ControlOperationState
- type ControlResp
- type ControlResult
- func (t *ControlResult) GetError() error
- func (t *ControlResult) GetLevel() int
- func (t *ControlResult) GetOutput() string
- func (t *ControlResult) GetStdErr() error
- func (t *ControlResult) GetStdOut() string
- func (t *ControlResult) IsError() bool
- func (t *ControlResult) Resp(c *gin.Context)
- func (t *ControlResult) RespError(c *gin.Context)
- type Directive
- type DirectiveValueReplacement
- type ErrorCategory
- type Module
- type NamespaceInfo
- type NgxConfig
- type NgxDirective
- type NgxLocation
- type NgxServer
- type NgxUpstream
- type Sandbox
- type SandboxBuildError
- type SandboxReason
- type SandboxStatus
- type TestConfigResult
- type TestScope
Constants ¶
const ( Debug = iota Info Notice Warn Error Crit Alert Emerg )
The values below must stay aligned with the indexes of logLevel, because GetLogLevel returns an index into that slice.
const ( // AccessLogRegexPattern matches access_log directive with unquoted path // Matches: access_log /path/to/file AccessLogRegexPattern = `(?m)^\s*access_log\s+([^\s;]+)` // ErrorLogRegexPattern matches error_log directive with unquoted path // Matches: error_log /path/to/file ErrorLogRegexPattern = `(?m)^\s*error_log\s+([^\s;]+)` // PIDRegexPattern matches pid directive with unquoted path // Matches: pid /path/to/file; PIDRegexPattern = `(?m)^\s*pid\s+([^\s;]+)` )
Regular expressions for parsing directives from nginx -T output
const ( Server = "server" Location = "location" Upstream = "upstream" Http = "http" Stream = "stream" )
const (
ModuleStream = "stream"
)
const Unknown = -1
Variables ¶
var ( ErrNginx = e.New(50000, "nginx error: {0}") ErrBlockIsNil = e.New(50001, "block is nil") ErrReloadFailed = e.New(50002, "reload nginx failed: {0}") ErrNginxTOutputEmpty = e.New(50003, "nginx -T output is empty") )
var ErrControlOperationRunning = errors.New("another nginx control operation is already running")
Functions ¶
func ClearModulesCache ¶
func ClearModulesCache()
ClearModulesCache clears the modules cache (public version for external use)
func DirectiveValues ¶
func GetAccessLogPath ¶
func GetAccessLogPath() (path string)
GetAccessLogPath returns the path of the nginx access log file
func GetConfEntryPath ¶
func GetConfEntryPath() (path string)
GetConfEntryPath returns the absolute path to the main nginx.conf. It prefers the value from `nginx -V --conf-path=...`. If that can't be parsed, it falls back to "<confDir>/nginx.conf".
func GetConfPath ¶
GetConfPath returns the nginx configuration directory (e.g. "/etc/nginx"). It tries to derive it from `nginx -V --conf-path=...`. If parsing fails, it falls back to a reasonable default instead of returning "".
func GetConfSymlinkPath ¶
func GetDirectives ¶
func GetErrorLogPath ¶
func GetErrorLogPath() string
GetErrorLogPath returns the path of the nginx error log file
func GetLoadModuleRegex ¶
GetLoadModuleRegex returns a compiled regular expression to match nginx load_module statements. It matches both quoted and unquoted module paths:
- load_module "/usr/local/nginx/modules/ngx_stream_module.so";
- load_module modules/ngx_http_upstream_fair_module.so;
The regex captures the module name (without path and extension).
func GetLogLevel ¶
func GetModules ¶
func GetModules() *orderedmap.OrderedMap[string, *Module]
func GetModulesPath ¶
func GetModulesPath() string
GetModulesPath returns the path of the nginx modules
func GetNginxExeDir ¶
func GetNginxExeDir() string
GetNginxExeDir Returns the directory containing the nginx executable
func GetPIDPath ¶
func GetPIDPath() (path string)
GetPIDPath returns the nginx master process PID file path. Resolution order:
- User override via settings (PIDPath)
- Runtime override from `nginx -T` for external containers
- Compile-time default from `nginx -V --pid-path=...`
- Probing common candidate paths (Docker-aware)
func GetSbinPath ¶
func GetSbinPath() (path string)
GetSbinPath returns the path of the nginx executable
func GetStatusSnapshot ¶
func GetStatusSnapshot() (*ControlResult, *ControlOperation)
GetStatusSnapshot returns the command result and operation from the same state revision.
func IsModuleLoaded ¶
IsModuleLoaded checks if a module is loaded in Nginx
func RewriteDirectiveValues ¶
func RewriteDirectiveValues(content string, replacements []DirectiveValueReplacement) (string, int, error)
Types ¶
type ControlFunc ¶
type ControlOperation ¶
type ControlOperation struct {
ID string `json:"id"`
Action string `json:"action"`
State ControlOperationState `json:"state"`
StartedAt time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
Message string `json:"message,omitempty"`
Level int `json:"level"`
ExitCode *int `json:"exit_code,omitempty"`
}
func GetControlOperation ¶
func GetControlOperation() *ControlOperation
GetControlOperation returns a snapshot of the latest tracked control operation.
func StartRestart ¶
func StartRestart(operationID string) (*ControlOperation, error)
StartRestart starts a tracked asynchronous restart operation.
type ControlOperationState ¶
type ControlOperationState string
const ( ControlOperationRunning ControlOperationState = "running" ControlOperationSucceeded ControlOperationState = "succeeded" ControlOperationFailed ControlOperationState = "failed" )
type ControlResp ¶
type ControlResult ¶
type ControlResult struct {
// contains filtered or unexported fields
}
func Control ¶
func Control(controlFunc ControlFunc) *ControlResult
func GetLastResult ¶
func GetLastResult() *ControlResult
GetLastResult returns the last output of the nginx command
func (*ControlResult) GetError ¶
func (t *ControlResult) GetError() error
func (*ControlResult) GetLevel ¶
func (t *ControlResult) GetLevel() int
func (*ControlResult) GetOutput ¶
func (t *ControlResult) GetOutput() string
func (*ControlResult) GetStdErr ¶
func (t *ControlResult) GetStdErr() error
func (*ControlResult) GetStdOut ¶
func (t *ControlResult) GetStdOut() string
func (*ControlResult) IsError ¶
func (t *ControlResult) IsError() bool
func (*ControlResult) Resp ¶
func (t *ControlResult) Resp(c *gin.Context)
func (*ControlResult) RespError ¶
func (t *ControlResult) RespError(c *gin.Context)
type ErrorCategory ¶
type ErrorCategory string
const ( ErrorCategoryNone ErrorCategory = "" ErrorCategoryMissingInclude ErrorCategory = "missing_include" ErrorCategorySandboxBuildError ErrorCategory = "sandbox_build_error" ErrorCategorySyntaxError ErrorCategory = "syntax_error" ErrorCategoryNginxRuntimeError ErrorCategory = "nginx_runtime_error" )
func DetectErrorCategory ¶
func DetectErrorCategory(message string, stdErr error) ErrorCategory
type NamespaceInfo ¶
NamespaceInfo represents minimal namespace info for sandbox
type NgxConfig ¶
type NgxConfig struct {
FileName string `json:"file_name"`
Name string `json:"name"`
RootBlock string `json:"root_block,omitempty"`
Upstreams []*NgxUpstream `json:"upstreams"`
Servers []*NgxServer `json:"servers"`
Custom string `json:"custom"`
// contains filtered or unexported fields
}
func NewNgxConfig ¶
func ParseNgxConfig ¶
func ParseNgxConfigByContent ¶
func (*NgxConfig) BuildConfig ¶
type NgxDirective ¶
type NgxDirective struct {
Directive string `json:"directive"`
Params string `json:"params"`
Comments string `json:"comments"`
// Raw, when non-empty, is the verbatim source text of the directive (including
// any block body). BuildConfig prefers Raw over Directive/Params so that block
// directives (e.g. ssl_certificate_by_lua_block) and quoted parameters survive
// a maintenance-config rebuild without being flattened. Keep Directive and
// Params populated for callers that consume this struct via JSON.
Raw string `json:"-"`
}
func (*NgxDirective) Orig ¶
func (d *NgxDirective) Orig() string
func (*NgxDirective) ParseDirective ¶
func (d *NgxDirective) ParseDirective(directive config.IDirective, deep int)
func (*NgxDirective) TrimParams ¶
func (d *NgxDirective) TrimParams()
type NgxLocation ¶
type NgxLocation struct {
Path string `json:"path"`
Content string `json:"content"`
Comments string `json:"comments"`
}
func (*NgxLocation) ParseLocation ¶
func (l *NgxLocation) ParseLocation(directive config.IDirective, deep int)
type NgxServer ¶
type NgxServer struct {
Directives []*NgxDirective `json:"directives"`
Locations []*NgxLocation `json:"locations"`
Comments string `json:"comments"`
}
func NewNgxServer ¶
func NewNgxServer() *NgxServer
func (*NgxServer) ParseServer ¶
func (s *NgxServer) ParseServer(directive config.IDirective)
type NgxUpstream ¶
type NgxUpstream struct {
Name string `json:"name"`
Directives []*NgxDirective `json:"directives"`
Comments string `json:"comments"`
}
type Sandbox ¶
type Sandbox struct {
Dir string
ConfigPath string
Namespace *NamespaceInfo
}
Sandbox represents an isolated nginx test environment
type SandboxBuildError ¶
type SandboxBuildError struct {
Category ErrorCategory
Message string
}
func (*SandboxBuildError) Error ¶
func (e *SandboxBuildError) Error() string
type SandboxReason ¶
type SandboxReason string
const ( SandboxReasonNone SandboxReason = "" SandboxReasonRemoteNamespace SandboxReason = "remote_namespace" SandboxReasonSeparateContainer SandboxReason = "separate_container" SandboxReasonCustomTestCommand SandboxReason = "custom_test_command" )
type SandboxStatus ¶
type SandboxStatus string
const ( SandboxStatusOK SandboxStatus = "ok" SandboxStatusSkipped SandboxStatus = "skipped" SandboxStatusFailed SandboxStatus = "failed" )
type TestConfigResult ¶
type TestConfigResult struct {
Message string `json:"message"`
Level int `json:"level"`
TestScope TestScope `json:"test_scope"`
SandboxStatus SandboxStatus `json:"sandbox_status,omitempty"`
SandboxReason SandboxReason `json:"sandbox_reason,omitempty"`
ErrorCategory ErrorCategory `json:"error_category,omitempty"`
}
func NewSandboxBuildFailureResult ¶
func NewSandboxBuildFailureResult(err error) TestConfigResult
func NewTestConfigResult ¶
func NewTestConfigResult(stdOut string, stdErr error, scope TestScope, sandboxStatus SandboxStatus) TestConfigResult
func SandboxTestConfigWithPaths ¶
func SandboxTestConfigWithPaths(namespace *NamespaceInfo, sitePaths, streamPaths []string) TestConfigResult
SandboxTestConfigWithPaths tests nginx config in an isolated sandbox with provided paths.