http

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package http provides a HTTP server which implements the app.Runtime interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runtime

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

Runtime is a bedrock.Runtime for running a HTTP service.

func NewRuntime

func NewRuntime(opts ...RuntimeOption) *Runtime

NewRuntime returns a fully initialized HTTP runtime.

func (*Runtime) Run

func (rt *Runtime) Run(ctx context.Context) error

Run implements app.Runtime interface.

Example
rt := NewRuntime(
	ListenOnPort(8080),
	HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, world")
	}),
)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

go func() {
	rt.Run(ctx)
}()

resp, err := http.Get("http://localhost:8080")
if err != nil {
	fmt.Println(err)
	return
}
defer resp.Body.Close()

b, err := io.ReadAll(resp.Body)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(b))
Output:

Hello, world

type RuntimeOption

type RuntimeOption func(*runtimeOptions)

RuntimeOption are options for configuring the HTTP runtime.

func Handle

func Handle(pattern string, h http.Handler) RuntimeOption

Handle registers a http.Handler for the given path pattern.

func HandleFunc

func HandleFunc(pattern string, f func(http.ResponseWriter, *http.Request)) RuntimeOption

HandleFunc registers a http.HandlerFunc for the given path pattern.

func Http2Only

func Http2Only() RuntimeOption

Http2Only configures the HTTP server to only accept HTTP/2 connections.

func ListenOnPort

func ListenOnPort(port uint) RuntimeOption

ListenOnPort will configure the HTTP server to listen on the given port.

Default port is 8080.

func LogHandler

func LogHandler(h slog.Handler) RuntimeOption

LogHandler configures the underlying slog.Handler.

func TLSConfig

func TLSConfig(cfg *tls.Config) RuntimeOption

TLSConfig configues the HTTP server to serve HTTPS via the given TLS config.

Directories

Path Synopsis
Package httpclient provides a production ready http.Client.
Package httpclient provides a production ready http.Client.
Package httphealth provides a http.Handler for exposing health.Metrics
Package httphealth provides a http.Handler for exposing health.Metrics
Package httpvalidate provides helpers for validating incoming HTTP requests.
Package httpvalidate provides helpers for validating incoming HTTP requests.

Jump to

Keyboard shortcuts

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