Files

76 lines
2.6 KiB
Go

package config
import (
"errors"
"reflect"
"git.avsdev.uk/Go/job-search/internal/util"
)
// JSCKServer is the server section's configuration key.
const JSCKServer string = "server"
// JSCServer configures the outward server. Host is the address to listen
// on (empty means every interface) and Port the port (0 means any free
// port). MaxHeaderBytes is the largest request header block; 0 means the
// default (8KB), and -1 (no limit) is rejected (see validate). MaxBodyBytes is
// the largest request body; 0 means the default (1MB) and -1 no limit.
// AccessLog is the file the access log is appended to, one line per request
// in the Combined Log Format; "-" means stdout, and empty means no access
// log.
type JSCServer struct {
Host string `cfg:"host" default:"127.0.0.1"`
Port int `cfg:"port" default:"9393"`
MaxHeaderBytes util.ByteSize `cfg:"max_header_bytes" default:"8KB"`
MaxBodyBytes util.ByteSize `cfg:"max_body_bytes" default:"1MB"`
AccessLog string `cfg:"access_log"`
Timeouts JSCServerTimeouts `cfg:"timeouts"`
RateLimits JSCServerRateLimits `cfg:"rate_limits"`
Proxy JSCServerProxy `cfg:"proxy"`
CSRF JSCServerCSRF `cfg:"csrf"`
TLS JSCServerTLS `cfg:"tls"`
Headers JSCServerHeaders `cfg:"headers"`
Frontend JSCServerFrontend `cfg:"frontend"`
WebSocket JSCServerWebSocket `cfg:"websocket"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServer) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServer) Flatten() map[string]any {
return flattenOf(JSCKServer, c)
}
// TypeOf implements Section.
func (c JSCServer) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServer) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServer) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ---------- Section specific methods ----------
// validate checks values before they are stored (see validator).
// max_header_bytes can't be -1 (no limit): the HTTP server always limits the
// header block, so -1 would quietly mean its own default instead.
func (c *JSCServer) validate(key string, value any) error {
if key == "max_header_bytes" &&
value.(util.ByteSize) == util.ByteSizeNoLimit {
return errors.New(
"config: max_header_bytes can't be -1 (no limit): set an explicit size, or 0 for the default",
)
}
return nil
}