Documentation
¶
Overview ¶
Package uhttp is the HTTP Module.
The HTTP module is built on top of Gorilla Mux (https://github.com/gorilla/mux), but the details of that are abstracted away through uhttp.RouteHandler.
package main import ( "io" "net/http" "go.uber.org/fx" "go.uber.org/fx/modules/uhttp" "go.uber.org/fx/service" ) func main() { svc, err := service.WithModules( uhttp.New(registerHTTP), ).Build() if err != nil { log.Fatal("Could not initialize service: ", err) } svc.Start(true) } func registerHTTP(service service.Host) []uhttp.RouteHandler { handleHome := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ulog.Logger(r.Context()).Info("Inside the handler") io.WriteString(w, "Hello, world") }) return []uhttp.RouteHandler{ uhttp.NewRouteHandler("/", handleHome) } }
HTTP handlers are set up with inbound middleware that inject tracing, authentication information etc. into the request context. Request tracing, authentication and context-aware logging are set up by default. With context-aware logging, all log statements include trace information such as traceID and spanID. This allows service owners to easily find logs corresponding to a request within and even across services.
HTTP Client ¶
The http client serves similar purpose as http module, but for making requests. It has a set of auth and tracing outbound middleware for http requests and a default timeout set to 2 minutes.
package main import ( "log" "net/http" "go.uber.org/fx/modules/uhttp/client" "go.uber.org/fx/service" ) func main() { svc, err := service.WithModules().Build() if err != nil { log.Fatal("Could not initialize service: ", err) } client := client.New(svc) client.Get("https://www.uber.com") }
Benchmark results:
Current performance benchmark data: BenchmarkClientMiddleware/empty-8 100000000 10.8 ns/op 0 B/op 0 allocs/op BenchmarkClientMiddleware/tracing-8 500000 3918 ns/op 1729 B/op 27 allocs/op BenchmarkClientMiddleware/auth-8 1000000 1866 ns/op 719 B/op 14 allocs/op BenchmarkClientMiddleware/default-8 300000 5604 ns/op 2477 B/op 41 allocs/op
Index ¶
Constants ¶
const ( // ContentType is the header key that contains the body type ContentType = "Content-Type" // ContentLength is the length of the HTTP body ContentLength = "Content-Length" // ContentTypeText is the plain content type ContentTypeText = "text/plain" // ContentTypeJSON is the JSON content type ContentTypeJSON = "application/json" )
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(hookup GetHandlersFunc, options ...modules.Option) service.ModuleCreateFunc
New returns a new HTTP module
func WithInboundMiddleware ¶
func WithInboundMiddleware(m ...InboundMiddleware) modules.Option
WithInboundMiddleware adds inbound middleware to uhttp Module that will be applied to all incoming http requests.
Types ¶
type Config ¶
type Config struct { modules.ModuleConfig Port int `yaml:"port"` Timeout time.Duration `yaml:"timeout"` Debug *bool `yaml:"debug"` }
Config handles config for HTTP modules
type GetHandlersFunc ¶
type GetHandlersFunc func(service service.Host) []RouteHandler
GetHandlersFunc returns a slice of registrants from a service host
type InboundMiddleware ¶
type InboundMiddleware interface {
Handle(w http.ResponseWriter, r *http.Request, next http.Handler)
}
InboundMiddleware applies inbound middleware on requests or responses such as adding tracing to the context.
type InboundMiddlewareFunc ¶
InboundMiddlewareFunc is an adaptor to call normal functions to apply inbound middleware.
func (InboundMiddlewareFunc) Handle ¶
func (f InboundMiddlewareFunc) Handle(w http.ResponseWriter, r *http.Request, next http.Handler)
Handle implements Handle from the InboundMiddleware interface and simply delegates to the function
type Module ¶
type Module struct { modules.ModuleBase // contains filtered or unexported fields }
A Module is a module to handle HTTP requests
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
A Route represents a handler for HTTP requests, with restrictions
func FromGorilla ¶
FromGorilla turns a gorilla mux route into an UberFx route
func (Route) GorillaMux ¶
GorillaMux returns the underlying mux if you need to use it directly
type RouteHandler ¶
A RouteHandler is an HTTP handler for a single route
func NewRouteHandler ¶
func NewRouteHandler(path string, handler http.Handler) RouteHandler
NewRouteHandler creates a route handler