http

package
v0.5.14 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MPL-2.0 Imports: 29 Imported by: 0

Documentation

Overview

This succint etag middleware has been borrowed from:

https://github.com/wtg/shuttletracker/blob/cdd56dc4aeca922f333c913f09c1796851d6f677/api/etag.go

It's very well articulated too by the author on their blog:

https://sidney.kochman.org/2018/etag-middleware-go/

Package http provides an HTTP interface allowing HTTP clients to interact with otf.

Index

Constants

View Source
const (
	APIBasePath     = "/otfapi"
	APIPingEndpoint = "ping"
	DefaultURL      = "https://localhost:8080"
)

Variables

View Source
var Encoder = schema.NewEncoder()

Encoder for encoding structs into queries: caches structs, and safe for sharing

View Source
var ErrParseURL = errors.New("url must begin with http:// or https:// and contain a hostname or ip address")
View Source
var InsecureTransport http.RoundTripper

Functions

func Absolute

func Absolute(r *http.Request, path string) string

Absolute returns an absolute URL for the given path. It uses the http request to determine the correct hostname and scheme to use. Handles situations where otf is sitting behind a reverse proxy, using the X-Forwarded-* headers the proxy sets.

func ExternalHost

func ExternalHost(r *http.Request) string

ExternalHost uses the incoming HTTP request to determine the host:port on which this server can be reached externally by clients and the internet.

func GetClientIP

func GetClientIP(r *http.Request) (string, error)

GetClientIP gets the client's IP address

func HeadersFromContext added in v0.2.0

func HeadersFromContext(ctx context.Context) (http.Header, error)

func ParseURL added in v0.3.0

func ParseURL(address string) (*url.URL, error)

ParseURL parses address into a URL. The URL must be absolute with an http(s) scheme.

func SanitizeHostname

func SanitizeHostname(hostname string) (string, error)

SanitizeHostname ensures hostname is in the format <host>:<port>

Types

type Client

type Client struct {
	Token string
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config ClientConfig) (*Client, error)

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *retryablehttp.Request, v any) error

Do sends an API request and returns the API response. The API response is JSONAPI decoded and the document's primary data is stored in the value pointed to by v, or returned as an error if an API error has occurred.

If v implements the io.Writer interface, the raw response body will be written to v, without attempting to first decode it.

The provided ctx must be non-nil. If it is canceled or times out, ctx.Err() will be returned.

func (*Client) Hostname

func (c *Client) Hostname() string

Hostname returns the server host:port.

func (*Client) NewRequest

func (c *Client) NewRequest(method, path string, v any) (*retryablehttp.Request, error)

NewRequest creates an API request with proper headers and serialization.

A relative URL path can be provided, in which case it is resolved relative to the baseURL of the Client. Relative URL paths should always be specified without a preceding slash. Adding a preceding slash allows for ignoring the configured baseURL for non-standard endpoints.

If v is supplied, the value will be JSONAPI encoded and included as the request body. If the method is GET, the value will be parsed and added as query parameters.

type ClientConfig added in v0.5.1

type ClientConfig struct {
	// The URL of the otf API.
	URL string
	// The base path on which the API is served.
	BasePath string
	// API token used to access the otf API.
	Token string
	// Headers that will be added to every request.
	Headers http.Header
	// Toggle retrying requests upon encountering transient errors.
	RetryRequests bool
	// Override default http transport
	Transport http.RoundTripper
	// Logger for logging an error upon retry
	Logger logr.Logger
}

ClientConfig provides configuration details to the API client.

type Server

type Server struct {
	logr.Logger
	ServerConfig
	// contains filtered or unexported fields
}

Server is the http server for OTF

func NewServer

func NewServer(logger logr.Logger, cfg ServerConfig) (*Server, error)

NewServer constructs the http server for OTF

func (*Server) Start

func (s *Server) Start(ctx context.Context, ln net.Listener) (err error)

Start starts serving http traffic on the given listener and waits until the server exits due to error or the context is cancelled.

type ServerConfig

type ServerConfig struct {
	SSL                  bool
	CertFile, KeyFile    string
	EnableRequestLogging bool

	Handlers []internal.Handlers
	// middleware to intercept requests, executed in the order given.
	Middleware []mux.MiddlewareFunc
}

ServerConfig is the http server config

Directories

Path Synopsis
Package decode contains decoders for various HTTP artefacts
Package decode contains decoders for various HTTP artefacts
Package html contains code relating specifically to the web UI.
Package html contains code relating specifically to the web UI.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL