http

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MPL-2.0 Imports: 27 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (

	// DefaultBasePath on which the API is served.
	DefaultBasePath = "/api/v2/"
	// PingEndpoint is a no-op API endpoint used to configure the rate limiter
	PingEndpoint = "ping"
)
View Source
const (
	ModuleV1Prefix = "/v1/modules/"
	APIPrefixV2    = "/api/v2/"
)
View Source
const (
	DefaultAddress = "localhost:8080"
)

Variables

View Source
var (

	// endpoints with these prefixes require authentication
	AuthenticatedPrefixes = []string{
		APIPrefixV2,
		ModuleV1Prefix,
		paths.UIPrefix,
	}
)
View Source
var Encoder = schema.NewEncoder()

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

Functions

func APIRouter

func APIRouter(r *mux.Router) *mux.Router

APIRouter wraps the given router with a router suitable for API routes.

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 SanitizeAddress

func SanitizeAddress(address string) (string, error)

SanitizeAddress ensures address is in format https://<host>:<port>

func SanitizeHostname

func SanitizeHostname(hostname string) (string, error)

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

Types

type Client

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

func NewClient

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

func (*Client) Do

func (c *Client) Do(ctx context.Context, req *retryablehttp.Request, v interface{}) 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 interface{}) (*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.

func (*Client) RetryServerErrors

func (c *Client) RetryServerErrors(retry bool)

RetryServerErrors configures the retry HTTP check to also retry unexpected errors or requests that failed with a server error.

type Config

type Config struct {
	// The address of the otf API.
	Address 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
	// A custom HTTP client to use.
	HTTPClient *http.Client
	// RetryLogHook is invoked each time a request is retried.
	RetryLogHook RetryLogHook
	// Insecure skips verification of upstream TLS certs.
	// NOTE: this does not take effect if HTTPClient is non-nil
	Insecure bool
}

Config provides configuration details to the API client.

func NewConfig

func NewConfig() Config

NewConfig constructs a new http client config with defaults.

type RetryLogHook

type RetryLogHook func(attemptNum int, resp *http.Response)

RetryLogHook allows a function to run before each retry.

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
	DevMode              bool

	Handlers   []internal.Handlers
	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.
paths
Package paths provides rails-style path helpers for use with the web app.
Package paths provides rails-style path helpers for use with the web app.

Jump to

Keyboard shortcuts

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