server

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2025 License: BSD-3-Clause Imports: 21 Imported by: 1

README

go-http/v3/server

Go package to provide interfaces and implementation for HTTP servers.

It is mostly a syntactic wrapper around existing HTTP server implementation with a common interface configured using a URI-based syntax.

Example

Error hanldling has been removed for the sake of brevity.

Using a server
package main

import (
	"context"
	"flag"
	"fmt"
	"net/http"

	"github.com/aaronland/go-http/v3/server"
)

func NewHandler() http.Handler {

	fn := func(rsp http.ResponseWriter, req *http.Request) {
		msg := fmt.Sprintf("Hello, %s", req.Host)
		rsp.Write([]byte(msg))
	}

	h := http.HandlerFunc(fn)
	return h
}

func main() {

	server_uri := flag.String("server-uri", "http://localhost:8080", "...")

	flag.Parse()

	ctx := context.Background()

	s, _ := server.NewServer(ctx, *server_uri)

	mux := http.NewServeMux()
	mux.Handle("/", NewHandler())

	log.Printf("Listening on %s", s.Address())
	s.ListenAndServe(ctx, mux)
}

Writing a server
package server

import (
	"context"
	"net/http"
	"net/url"
)

func init() {
	ctx := context.Background()
	RegisterServer(ctx, "http", NewHTTPServer)
}

type HTTPServer struct {
	Server
	url *url.URL
}

func NewHTTPServer(ctx context.Context, uri string) (Server, error) {

	u, _ := url.Parse(uri)

	u.Scheme = "http"

	server := HTTPServer{
		url: u,
	}

	return &server, nil
}

func (s *HTTPServer) Address() string {
	return s.url.String()
}

func (s *HTTPServer) ListenAndServe(ctx context.Context, mux *http.ServeMux) error {
	return http.ListenAndServe(s.url.Host, mux)
}

Server schemes

The following schemes/implementations are included by default with this package.

functionurl://

An AWS Lambda Function URL compatible HTTP server.

http://{HOST}

A standard, plain-vanilla, HTTP server.

https://{HOST}?cert={TLS_CERTIFICATE}&key={TLS_KEY}

This is an alias to the tls:// scheme.

lambda://

An AWS Lambda function + API Gateway compatible HTTP server.

mkcert://{HOST}

A thin wrapper to invoke the mkcert tool to generate locally signed TLS certificate and key files. Once created this implementation will invoke the tls:// scheme with the files create by mkcert. It is hoped this will be a short-lived scheme but it is necessary in the absence of an ACME compatibility with the mkcert tool.

tls://{HOST}?cert={TLS_CERTIFICATE}&key={TLS_KEY}

A standard, plain-vanilla, HTTPS/TLS server. You must provide TLS certificate and key files.

See also

Documentation

Index

Constants

View Source
const MKCERT string = "mkcert"

Variables

This section is empty.

Functions

func RegisterServer

func RegisterServer(ctx context.Context, scheme string, f ServerInitializeFunc) error

RegisterServer() associates 'scheme' with 'f' in an internal list of avilable `Server` implementations.

func Schemes

func Schemes() []string

Schemes() returns the list of schemes that have been "registered".

Types

type HTTPServer

type HTTPServer struct {
	Server
	// contains filtered or unexported fields
}

HTTPServer implements the `Server` interface for a basic `net/http` server.

func (*HTTPServer) Address

func (s *HTTPServer) Address() string

Address returns the fully-qualified URI where the server instance can be contacted.

func (*HTTPServer) ListenAndServe

func (s *HTTPServer) ListenAndServe(ctx context.Context, mux http.Handler) error

ListenAndServe starts the server and listens for requests using 'mux' for routing.

type LambdaFunctionURLServer

type LambdaFunctionURLServer struct {
	Server
	// contains filtered or unexported fields
}

LambdaFunctionURLServer implements the `Server` interface for a use in a AWS LambdaFunctionURL + API Gateway context.

func (*LambdaFunctionURLServer) Address

func (s *LambdaFunctionURLServer) Address() string

Address returns the fully-qualified URL used to instantiate 's'.

func (*LambdaFunctionURLServer) ListenAndServe

func (s *LambdaFunctionURLServer) ListenAndServe(ctx context.Context, mux http.Handler) error

ListenAndServe starts the serve and listens for requests using 'mux' for routing.

type LambdaServer

type LambdaServer struct {
	Server
	// contains filtered or unexported fields
}

LambdaServer implements the `Server` interface for a use in a AWS Lambda + API Gateway context.

func (*LambdaServer) Address

func (s *LambdaServer) Address() string

Address returns the fully-qualified URL used to instantiate 's'.

func (*LambdaServer) ListenAndServe

func (s *LambdaServer) ListenAndServe(ctx context.Context, mux http.Handler) error

ListenAndServe starts the serve and listens for requests using 'mux' for routing.

type Server

type Server interface {
	// ListenAndServe starts the server and listens for requests using a `http.Handler` instance for routing.
	ListenAndServe(context.Context, http.Handler) error
	// Address returns the fully-qualified URI that the server is listening for requests on.
	Address() string
}

type Server is an interface for creating server instances that serve requests using a `http.Handler` router.

func NewHTTPServer

func NewHTTPServer(ctx context.Context, uri string) (Server, error)

NewHTTPServer returns a new `HTTPServer` instance configured by 'uri' which is expected to be defined in the form of:

{SCHEME}://{ADDRESS}:{PORT}?{PARAMETERS}

Where {SCHEME} is either 'http' or 'https'; {ADDRESS} and {PORT} are the address and port to listen for requests on. Valid parameters are: * `tls_cert={CERTIFICATE}` The path for a TLS certificate to use; required if {SCHEME} is 'https'. * `tls_key={KEY}` The path for a TLS key to use; required if {SCHEME} is 'https' * `read_timeout={SECONDS}` A custom setting for HTTP read timeouts. Default is 2 seconds. * `write_timeout={SECONDS}` A custom setting for HTTP write timeouts. Default is 10 seconds. * `idle_timeout={SECONDS}` A custom setting for HTTP idle timeouts. Default is 15 seconds. * `header_timeout={SECONDS}` A custom setting for HTTP header timeouts. Default is 2 seconds.

func NewLambdaFunctionURLServer

func NewLambdaFunctionURLServer(ctx context.Context, uri string) (Server, error)

NewLambdaFunctionURLServer returns a new `LambdaFunctionURLServer` instance configured by 'uri' which is expected to be defined in the form of:

functionurl://?{PARAMETERS}

Valid parameters are: * `binary_type={MIMETYPE}` One or more mimetypes to be served by AWS FunctionURLs as binary content types.

func NewLambdaServer

func NewLambdaServer(ctx context.Context, uri string) (Server, error)

NewLambdaServer returns a new `LambdaServer` instance configured by 'uri' which is expected to be defined in the form of:

lambda://?{PARAMETERS}

Valid parameters are: * `binary_type={MIMETYPE}` One or more mimetypes to be served by AWS API Gateway as binary content types.

func NewMkCertServer

func NewMkCertServer(ctx context.Context, uri string) (Server, error)

NewMkCertServer returns a new `HTTPServer` instance configured using 'uri' in the form of:

mkcert://?{PARAMETERS}

Valid parameters are:

  • `root={PATH}` An optional path to specify where `mkcert` certificates and keys should be created. If missing the operating system's temporary directory will be used.

func NewServer

func NewServer(ctx context.Context, uri string) (Server, error)

NewServer() returns a new instance of `Server` for the scheme associated with 'uri'. It is assumed that this scheme will have previously been "registered" with the `RegisterServer` method.

type ServerInitializeFunc

type ServerInitializeFunc func(context.Context, string) (Server, error)

ServeritializeFunc is a function used to initialize an implementation of the `Server` interface.

Jump to

Keyboard shortcuts

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