{"modulePath":"github.com/Compogo/http","version":"v0.0.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package http\n\n## Constants\n\n```go\nconst (\n\tInterfaceFieldName       = \"server.http.interface\"\n\tPortFieldName            = \"server.http.port\"\n\tShutdownTimeoutFieldName = \"server.http.timeout.shutdown\"\n\n\tInterfaceDefault       = \"0.0.0.0\"\n\tPortDefault            = uint16(8080)\n\tShutdownTimeoutDefault = 30 * time.Second\n)\n```\n\n## Variables\n\n```go\nvar Component = \u0026component.Component{\n\tName: \"http.server\",\n\tDependencies: component.Components{\n\t\trunner.Component,\n\t},\n\tInit: component.StepFunc(func(container container.Container) error {\n\t\treturn container.Provides(\n\t\t\tNewConfig,\n\t\t\tNewServer,\n\t\t)\n\t}),\n\tBindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error {\n\t\treturn container.Invoke(func(config *Config) {\n\t\t\tflagSet.StringVar(\u0026config.Interface, InterfaceFieldName, InterfaceDefault, \"interface for listening to incoming requests\")\n\t\t\tflagSet.Uint16Var(\u0026config.Port, PortFieldName, PortDefault, \"port for listening to incoming requests\")\n\t\t\tflagSet.DurationVar(\n\t\t\t\t\u0026config.ShutdownTimeout,\n\t\t\t\tShutdownTimeoutFieldName,\n\t\t\t\tShutdownTimeoutDefault,\n\t\t\t\t\"timeout during which the HTTP server must be turned off after receiving a shutdown signal\",\n\t\t\t)\n\t\t})\n\t}),\n\tConfiguration: component.StepFunc(func(container container.Container) error {\n\t\treturn container.Invoke(Configuration)\n\t}),\n\tPreWait: component.StepFunc(func(container container.Container) error {\n\t\treturn container.Invoke(func(r runner.Runner, server Server) error {\n\t\t\treturn r.RunProcess(server)\n\t\t})\n\t}),\n}\n```\nComponent is a ready-to-use Compogo component that provides an HTTP server. It automatically:\n\n  - Registers Config and Server in the DI container\n  - Adds command-line flags for server configuration\n  - Configures the server during PreRun phase\n  - Starts the server as a runner task during PostRun phase\n  - Performs graceful shutdown during Stop phase\n\nUsage:\n\n\tcompogo.WithComponents(\n\t    runner.Component,\n\t    http.Component,\n\t    myRouterComponent,  // must implement http.Router\n\t)\n\n## Types\n\n```go\ntype Config struct {\n\tInterface       string\n\tPort            uint16\n\tShutdownTimeout time.Duration\n}\n```\n\n```go\nfunc Configuration(config *Config, configurator configurator.Configurator) *Config\n```\n\n```go\nfunc NewConfig() *Config\n```\n\n```go\ntype Middleware interface {\n\t// Middleware wraps the next handler and returns a new handler.\n\t// The returned handler should call next.ServeHTTP() to continue the chain.\n\tMiddleware(next http.Handler) http.Handler\n}\n```\nMiddleware defines the interface for HTTP middleware. Middlewares wrap http.Handler to add cross-cutting concerns such as logging, authentication, metrics, or recovery.\n\nExample:\n\n\tfunc LoggerMiddleware(next http.Handler) http.Handler {\n\t    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t        log.Printf(\"%s %s\", r.Method, r.URL.Path)\n\t        next.ServeHTTP(w, r)\n\t    })\n\t}\n\n```go\ntype MiddlewareFunc func(next http.Handler) http.Handler\n```\nMiddlewareFunc is a function adapter that allows ordinary functions to be used as Middleware implementations.\n\n```go\nfunc (m MiddlewareFunc) Middleware(next http.Handler) http.Handler\n```\nMiddleware implements the Middleware interface by calling the underlying function.\n\n```go\ntype Router interface {\n\t// http.Handler makes Router usable as a standard HTTP handler.\n\thttp.Handler\n\n\t// Use adds middleware to the router.\n\t// Middlewares are applied in the order they are added.\n\tUse(middlewares ...Middleware)\n\n\t// Group creates a new sub-router with inherited middleware.\n\t// All routes defined inside the function will share the same prefix.\n\tGroup(fn func(r Router))\n\n\t// Route creates a new sub-router with the given prefix.\n\t// Similar to Group but without inheriting parent middleware.\n\tRoute(pattern string, fn func(r Router))\n\n\t// Mount attaches another http.Handler at the specified pattern.\n\t// Useful for mounting sub-applications or third-party handlers.\n\tMount(pattern string, h http.Handler)\n\n\t// Handle registers a handler for the given pattern with all HTTP methods.\n\tHandle(pattern string, h http.Handler)\n\n\t// HandleFunc registers a handler function for the given pattern with all HTTP methods.\n\tHandleFunc(pattern string, h http.HandlerFunc)\n\n\t// Method registers a handler for the given pattern with a specific HTTP method.\n\tMethod(method, pattern string, h http.Handler)\n\n\t// MethodFunc registers a handler function for the given pattern with a specific HTTP method.\n\tMethodFunc(method, pattern string, h http.HandlerFunc)\n\n\t// Connect registers a handler for CONNECT method.\n\tConnect(pattern string, h http.HandlerFunc)\n\n\t// Delete registers a handler for DELETE method.\n\tDelete(pattern string, h http.HandlerFunc)\n\n\t// Get registers a handler for GET method.\n\tGet(pattern string, h http.HandlerFunc)\n\n\t// Head registers a handler for HEAD method.\n\tHead(pattern string, h http.HandlerFunc)\n\n\t// Options registers a handler for OPTIONS method.\n\tOptions(pattern string, h http.HandlerFunc)\n\n\t// Patch registers a handler for PATCH method.\n\tPatch(pattern string, h http.HandlerFunc)\n\n\t// Post registers a handler for POST method.\n\tPost(pattern string, h http.HandlerFunc)\n\n\t// Put registers a handler for PUT method.\n\tPut(pattern string, h http.HandlerFunc)\n\n\t// Trace registers a handler for TRACE method.\n\tTrace(pattern string, h http.HandlerFunc)\n\n\t// NotFound sets the handler for routes that don't match any pattern.\n\tNotFound(h http.HandlerFunc)\n\n\t// MethodNotAllowed sets the handler for routes that match the path\n\t// but not the HTTP method.\n\tMethodNotAllowed(h http.HandlerFunc)\n}\n```\nRouter defines the interface for HTTP routing. It's designed to be compatible with popular routers like chi, gorilla/mux, or the standard library's ServeMux.\n\nThe interface includes:\n\n  - Standard HTTP handler methods (Get, Post, Put, etc.)\n  - Middleware support (Use)\n  - Route grouping (Group, Route)\n  - Subtree mounting (Mount)\n\n```go\ntype Server interface {\n\t// Process runner.Process allows the server to be run as a task in the runner.\n\t// The server will block until the context is canceled or an error occurs.\n\trunner.Process\n\n\t// SetRouter attaches a Router to the server.\n\t// Must be called before starting the server.\n\tSetRouter(router Router)\n}\n```\nServer defines the interface for an HTTP server component. It integrates with runner.Runner for lifecycle management and supports graceful shutdown via io.Closer.\n\n```go\nfunc NewServer(config *Config, logger logger.Logger) Server\n```\nNewServer creates a new HTTP server instance with the given configuration. The server is not started until it's passed to runner.Runner. The logger is automatically namespaced with \"server.http\" for better log filtering.\n\n","path":"github.com/Compogo/http","name":"http","synopsis":"","isRedistributable":true}
