restserver

package
v0.0.0-...-7db3de3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package restserver provides building blocks to implement a mocked version of out-of-process components Ubuntu Pro for WSL depend on that talk REST, such as MS Store API and the Contracts Server backend DO NOT USE IN PRODUCTION

Package restserver provides building blocks to implement a mocked version of out-of-process components Ubuntu Pro for WSL depend on that talk REST, such as MS Store API and the Contracts Server backend DO NOT USE IN PRODUCTION

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// Name of the application as shown in the help messages.
	Name string
	// Description fo the application as shown in the long help messages.
	Description string
	// The default settings that the application will pass to the server instance.
	DefaultSettings Settings
	// A function capable of translating from the Settings interface into the concrete implementation a particular server will need to run.
	ServerFactory func(Settings) Server
}

App encapsulates creating and managing the CLI and lifecycle.

func (*App) Run

func (app *App) Run() int

Run runs the server CLI.

type Endpoint

type Endpoint struct {
	// OnSuccess is the response returned in the happy path.
	OnSuccess Response

	// Disabled disables the endpoint.
	Disabled bool

	// Blocked means that a response will not be sent back, instead it'll block until the server is stopped.
	Blocked bool
}

Endpoint contains settings for an API endpoint behaviour. Can be modified for testing purposes.

func NewEndpoint

func NewEndpoint() Endpoint

NewEndpoint returns a minimal endpoint configured for success.

type Response

type Response struct {
	Value  string
	Status int
}

Response contains settings for an API endpoint response behaviour. Can be modified for testing purposes.

type Server

type Server interface {
	Stop() error
	Serve(ctx context.Context, addr string) error
	Address() string
}

Server is the minimal interface mock REST servers must provide to the Application.

type ServerBase

type ServerBase struct {
	Mux *http.ServeMux
	// contains filtered or unexported fields
}

ServerBase is the core building block of configurable mock REST servers.

func (*ServerBase) Address

func (s *ServerBase) Address() string

Address returns the server network address configured during Serve. Empty string is returned when called before Serve.

func (*ServerBase) Serve

func (s *ServerBase) Serve(ctx context.Context, address string) error

Serve starts a new HTTP server listening on address with responses defined according to Server Settings. Use Stop to Stop the server and release resources.

func (*ServerBase) Stop

func (s *ServerBase) Stop() error

Stop stops the server.

func (*ServerBase) ValidateRequest

func (s *ServerBase) ValidateRequest(w http.ResponseWriter, r *http.Request, wantMethod string, endpoint Endpoint) (err error)

ValidateRequest extracts common boilerplate used to validate the request from endpoints.

type Settings

type Settings interface {
	Unmarshal(in []byte, unmarshaller func(in []byte, out interface{}) (err error)) (Settings, error)
}

Settings is the minimal interface a settings backend must provide to the Application.

Jump to

Keyboard shortcuts

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