httpx

package module
Version: v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2019 License: ISC Imports: 6 Imported by: 0

README

httpx

import "acln.ro/httpx"

GoDoc

Convenience extensions to net/http. Not much to see here. You probably don't want to use this anyway.

See also:

Documentation

Overview

Package httpx provides convenience extensions to net/http, enabling a certain style of web programming which resembles the implementation of recursive descent parsers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Path

func Path(req *http.Request) string

Path returns the original URL.Path associated with req. If the context associated with req does not store a path, Path returns the empty string.

func RequestID

func RequestID(req *http.Request) string

RequestID returns the identifier associated with the request.

func RequestLogger

func RequestLogger(base *log.Logger, req *http.Request) *log.Logger

RequestLogger returns a logger scoped to the specified request. The logger records the "method", "path", "remote_addr" and "user_agent" keys. If present, it also records the "request_id" key.

func Shift

func Shift(req *http.Request) string

Shift shifts req.URL.Path forward by one segment, and returns the segment, if any. req.URL.Path must either be empty, or have a "/" prefix.

For the paths "" and "/", Shift is a no-op and returns "".

For the path "/abc", Shift sets req.URL.Path to "" and returns "abc".

For the path "/abc/anything", Shift sets req.URL.Path to "/anything", and returns "abc".

func WithPath

func WithPath(req *http.Request) *http.Request

WithPath stores req.URL.Path in the context associated with req, and returns the new *http.Request, with the updated context.

The value can later be retrieved by calling Path on the request.

If the request context stores a path already, WithPath is a no-op and returns req.

func WithRequestID

func WithRequestID(req *http.Request, id string) *http.Request

WithRequestID assigns an identifier to an HTTP request, if one is not assigned already.

Types

type Summary

type Summary struct {
	// Status is the first HTTP status code written, or http.StatusOK
	// if no status was written explicitly.
	Status int

	// Duration measures the duration of the request.
	Duration time.Duration

	// Written typically counts the number of bytes written to the HTTP
	// response body.
	Written int64
}

Summary is a summary of an HTTP server response.

func ServeInstrumented

func ServeInstrumented(h http.Handler, w http.ResponseWriter, req *http.Request) Summary

ServeInstrumented instruments w, wraps h, and calls the wrapped handler with the instrumented http.ResponseWriter and the specified *http.Request. It returns a summary of the request.

func (Summary) KV

func (s Summary) KV() log.KV

KV returns key-value pairs representing the Summary, suitable for logging using a acln.ro/log.Logger. The "status", "duration" and "written" keys are used.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto