server

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package server provides a small, gracefully-stoppable HTTP server.

A Server binds its listener at construction time (so the bound address is immediately available via Server.Addr), but does not begin serving until Server.ServeHTTP or Server.ServeHTTPHandler is called. Those methods block until the provided context is cancelled, at which point the server is shut down with a short grace period.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HealthzHandler added in v0.2.0

func HealthzHandler() http.Handler

HealthzHandler returns a liveness handler that always responds 200 {"status":"ok"}. Liveness means "the process is up"; for dependency checks use ReadyzHandler.

func ReadyzHandler added in v0.2.0

func ReadyzHandler(checks map[string]func(context.Context) error) http.Handler

ReadyzHandler returns a readiness handler that runs the named checks with the request context. If every check returns nil it responds 200 {"status":"ok"}; otherwise it responds 503 listing only the failing check names:

{"status":"unavailable","failed":["database"]}

Check error details are logged server-side, never returned in the body, so a publicly-reachable probe cannot leak internal hostnames, addresses, or paths. A check that panics is treated as failed rather than crashing the probe. Checks should be fast and side-effect free (e.g. a ping); wrap slow checks in your own caching if probes are frequent.

Example
package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/mikehelmick/go-bananas/server"
)

func main() {
	// Register named readiness checks; details of failures are logged
	// server-side while the public body lists only the failing names.
	h := server.ReadyzHandler(map[string]func(context.Context) error{
		"database": func(_ context.Context) error {
			return errors.New("dial tcp: connection refused") // logged, not returned
		},
		"cache": func(_ context.Context) error { return nil },
	})

	w := httptest.NewRecorder()
	h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/readyz", nil))

	fmt.Println(w.Code)
	fmt.Print(w.Body.String())
}
Output:
503
{"failed":["database"],"status":"unavailable"}

Types

type Server

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

Server is a gracefully-stoppable HTTP server. It is safe for concurrent use.

func New

func New(port string) (*Server, error)

New creates a server listening on the provided port. The listener is bound immediately, so the server is ready to accept connections before serving begins. An empty port lets the operating system choose a free one.

func NewFromListener

func NewFromListener(listener net.Listener) (*Server, error)

NewFromListener creates a server on the given listener, useful for customizing the listener type or bind options. The listener must be TCP.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the server's listening address (host:port).

func (*Server) IP

func (s *Server) IP() string

IP returns the server's listening IP.

func (*Server) Port

func (s *Server) Port() string

Port returns the server's listening port.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(ctx context.Context, srv *http.Server) error

ServeHTTP starts the server with the provided http.Server and blocks until ctx is cancelled, after which it gracefully shuts down with a 5-second timeout. A server is not safe to reuse after it has stopped.

func (*Server) ServeHTTPHandler

func (s *Server) ServeHTTPHandler(ctx context.Context, handler http.Handler) error

ServeHTTPHandler is a convenience wrapper around Server.ServeHTTP that builds an http.Server from handler with sensible timeouts. Observability or tracing wrappers, if desired, are the caller's responsibility (wrap handler before passing it in).

Jump to

Keyboard shortcuts

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