httpserver

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

README

httpserver: HTTP server GoDoc

Basic HTTP server for Go.

This server supports:

  • TCP sockets
  • UNIX sockets (with user, group and mode options)
  • Graceful shutdown

Example

package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"

	"batou.dev/httpserver"
)

func main() {
	m := http.NewServeMux()
	m.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
		rw.Write([]byte("Hi there!\n"))
	})

	s := &httpserver.Server{
		Addr:            "localhost:8080", // or "unix:/path/to/server.sock?mode=0600"
		Handler:         m,
		ShutdownTimeout: 10 * time.Second,
	}

	// Wait for termination signal, then cancel context
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, syscall.SIGINT)
	go func() { <-sigCh; cancel() }()

	// Start serving HTTP connections
	err := s.Run(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidAddr is an invalid address error.
	ErrInvalidAddr = errors.New("invalid address")
)

Functions

This section is empty.

Types

type Server

type Server struct {
	// Addr is the address for the server to listen on.
	// e.g. "localhost:8080" or
	// "unix:/path/to/server.sock?mode=0600&user=www-data&group=www-data"
	Addr string

	// Handler is a HTTP handler serving requests.
	Handler http.Handler

	// ShutdownTimeout is the duration to wait before forcefully shuting down
	// the server.
	ShutdownTimeout time.Duration
}

Server is a HTTP server.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run starts accepting connections and serving HTTP requests.

Jump to

Keyboard shortcuts

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