go-srvc / mods / httpmod

github.com/go-srvc/mods/httpmod

pkg.go.dev source
go get github.com/go-srvc/mods/httpmod@v1.16.0

README

httpmod

httpmod runs an http.Server as a srvc module and shuts it down gracefully on exit.

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/go-srvc/mods/httpmod"
	"github.com/go-srvc/mods/sigmod"
	"github.com/go-srvc/srvc"
)

func main() {
	srvc.RunAndExit(
		sigmod.New(os.Interrupt),
		httpmod.New(
			httpmod.WithAddr(":8080"),
			httpmod.WithHandler(http.HandlerFunc(hello)),
		),
	)
}

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "hello, world")
}

TLS

Set a tls.Config with a certificate and the server serves HTTPS, including HTTP/2.

cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil {
	log.Fatal(err)
}

httpmod.New(
	httpmod.WithAddr(":8443"),
	httpmod.WithTLSConfig(&tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: tls.VersionTLS12}),
	httpmod.WithHandler(http.HandlerFunc(hello)),
)

Overview

Package httpmod provides http server as module.

Constants

const ID = "httpmod"

Types

type Opt

type Opt func(s *Server) error

func WithAddr

func WithAddr(addr string) Opt

WithAddr sets http.Server.Addr.

func WithAddrFn

func WithAddrFn(fn func() (string, error)) Opt

WithAddrFn sets http.Server.Addr using value returned from fn.

func WithHandler

func WithHandler(h http.Handler) Opt

WithHandler sets http.Server.Handler.

func WithHandlerFn

func WithHandlerFn(fn func() (http.Handler, error)) Opt

WithHandlerFn sets http.Server.Handler using value returned from fn.

func WithServer

func WithServer(srv *http.Server) Opt

WithServer sets http.Server for module.

func WithServerFn

func WithServerFn(fn func() (*http.Server, error)) Opt

WithServerFn sets http.Server using value returned from fn.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Opt

WithShutdownTimeout sets timeout for graceful shutdown.

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) Opt

WithTLSConfig sets http.Server.TLSConfig, which makes the server serve HTTPS.

type Server

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

func New

func New(opts ...Opt) *Server

New creates Server with given options.

func (*Server) ID

func (s *Server) ID() string

func (*Server) Init

func (s *Server) Init() error

Init starts net.Listener after applying all options. Options are applied in same order as they were provided.

func (*Server) Run

func (s *Server) Run() error

Run starts serving http requests, over TLS when TLSConfig is set, and can be called after initialization.

func (*Server) Stop

func (s *Server) Stop() error

Stop calls shutdown for server.

func (*Server) URL

func (s *Server) URL() string

URL returns server's URL and can be called after initialization.

Examples

ExampleNew

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/go-srvc/mods/httpmod"
	"github.com/go-srvc/srvc"
)

func main() {
	srvc.RunAndExit(
		httpmod.New(
			httpmod.WithServer(&http.Server{ReadHeaderTimeout: time.Second}),
			httpmod.WithAddr("127.0.0.1:0"),
			httpmod.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				fmt.Fprint(w, "Hello")
			})),
		),
	)
}