httpserver

package module
v0.0.0-...-8240373 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 6 Imported by: 1

README

HTTP server bootstrap

Designed for go-flags package but can be used independently.

Motivation: I tired to write the same bootstrap code again and again in my project.

Features:

  • native integration with go-flags
  • context-aware: gracefully finished execution when context closed
  • supports TLS and Auto-TLS (Let's Encrypt)

Installation

go get github.com/reddec/run-http-server

Usage

package main

import (
	"context"
	"net/http"
	"os"
	"os/signal"

	"github.com/jessevdk/go-flags"
	"github.com/reddec/run-http-server"
)

func main() {
	var server httpserver.Server
	flags.Parse(&server)

	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		writer.Write([]byte("hello world"))
	})

	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	panic(server.Run(ctx))
}



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	Bind             string        `long:"bind" env:"BIND" description:"Binding address" default:"127.0.0.1:8080"`
	GracefulShutdown time.Duration `long:"graceful-shutdown" env:"GRACEFUL_SHUTDOWN" description:"Timeout for HTTP server graceful shutdown" default:"10s"`
	TLS              bool          `long:"tls" env:"TLS" description:"Enable TLS server"`
	TLSCert          string        `long:"tls-cert" env:"TLS_CERT" description:"Path to TLS certificate" default:"server.crt"`
	TLSKey           string        `long:"tls-key" env:"TLS_KEY" description:"Path to TLS private key" default:"server.key"`
	AutoTLS          bool          `long:"auto-tls" env:"AUTO_TLS" description:"Enable automatic TLS certificates by Let's Encrypt. Forces bind to :443"`
	AutoTLSDomains   []string      `long:"auto-tls-domains" env:"AUTO_TLS_DOMAINS" description:"Restrict automatic TLS certificates to specified domains"`
	AutoTLSCache     string        `long:"auto-tls-cache" env:"AUTO_TLS_CACHE" description:"Directory to store obtained certificates" default:".certs"`
}

func (Server) Run

func (config Server) Run(ctx context.Context) error

func (Server) Serve

func (config Server) Serve(global context.Context, server *http.Server) error

Jump to

Keyboard shortcuts

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