Categorygithub.com/casualjim/go-httpd
modulepackage
1.6.1
Repository: https://github.com/casualjim/go-httpd.git
Documentation: pkg.go.dev

# README

HTTPD

A small go package to create a golang http application server.

Features

  • pflag integration (can be configured from cobra)

  • Unix Domain Socket connections

  • HTTP connections

  • HTTPS connections

  • Optionally can serve the admin endpoints on a different set of listeners

The TLS configuration that is provided is optimized for modern browsers and should get you a perfect A+ score from SSL labs.

Usage

package main

import (
  // ... elided...
  "net/http"
  "github.com/NYTimes/gziphandler"

  "github.com/justinas/alice"

  "github.com/e-dard/netbug"

  ghandlers "github.com/gorilla/handlers"
  "github.com/prometheus/client_golang/prometheus/promhttp"
)

var adminServer = &httpd.HTTPFlags{
  Prefix: "admin",
  Port: 12034,
  ListenLimit: 10,
  KeepAlive: 5*time.Second,
  ReadTimeout: 3*time.Second,
  WriteTimeout: 3*time.Second,
}

func main() {
  api := swaggerapi.New(/* ... elided ... */)

  adminHandler := http.NewServeMux()
  netbug.RegisterHandler("/debug/", adminHandler) // trailing slash required in this call
  adminHandler.Handle("/metrics", promhttp.Handler())
  adminHandler.HandleFunc("/healthz", healthzEndpoint)
  adminHandler.HandleFunc("/readyz", readyzEndpoint)
  adminHandler.Handle("/", http.NotFoundHandler())

  ll := &zapLogger{lg: logger.Bg()}
  rhandler := alice.New(
    ghandlers.RecoveryHandler(
      ghandlers.RecoveryLogger(ll),
      ghandlers.PrintRecoveryStack(true),
    ),
    gziphandler.GzipHandler,
    ghandlers.ProxyHeaders,
  ).Then(api.Serve(nil))

  server := httpd.New(
    httpd.LogsWith(ll),
    httpd.HandlesRequestsWith(rhandler),
    httpd.WithAdmin(adminHandler, adminServer),
    httpd.OnShutdown(func() {
      // perform cleanup here
    }),
  )

  if err := server.Listen(); err != nil {
    logger.Bg().Fatal("", zap.Error(err))
  }

  if err := server.Serve(); err != nil {
    logger.Bg().Fatal("", zap.Error(err))
  }
}

func healthzEndpoint(rw http.ResponseWriter, r *http.Request) {
  rw.Write([]byte("OK"))
}

func readyzEndpoint(rw http.ResponseWriter, r *http.Request) {
  rw.Write([]byte("OK"))
}

# Functions

No description provided by the author
EnablesSchemes overrides the enabled schemes.
HandlesAdminWith configures the handler (maybe mux) for the admin endpoint (like /healthz, /readyz, /metrics).
HandlesRequestsWith handles the http requests to the server.
Hooks allows for registering one or more hooks for the server to call during its lifecycle.
LogsWith provides a logger to the server.
New creates a new application server.
OnShutdown runs the provided functions on shutdown.
RegisterFlags to the specified pflag set.
SplitHostPort splits a network address into a host and a port.
WithAdminListeners configures the handler and the listeners for the admin endpoint (like /healthz, /readyz, /metrics).
WithAdminListeners configures the listeners for the admin endpoint (like /healthz, /readyz, /metrics).
WithExtaListeners appends the provided listeners to the default listeners.
WithListeners replaces the default listeners with the provided listeres.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

Hook allows for hooking into the lifecycle of the server.
No description provided by the author
Server is the interface a server implements.
No description provided by the author

# Type aliases

ByteSize used to pass byte sizes to a go-flags CLI.
Option for the server.