yethttp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 7 Imported by: 0

README

GoDoc

yethttp

yethttp reduces the amount of boilerplate to write a simple http server with graceful shutdown.

Usage

type WebApp struct {
    yethttp.EmbeddableServerWrapper
}

func NewWebApp(logger yetlog.Logger) WebApp {
    serverWrapper := yethttp.NewEmbeddableServerWrapper(logger, 8080)

    return WebApp{
        EmbeddableServerWrapper: serverWrapper,
    }
}

func main() {
    webApp := NewWebApp(yetlog.NewNullLogger())
    if err := webApp.Serve(context.Background(), yethttp.DefaultRoutesFunc); err != nil {
        panic(err)
    }

    if err := webApp.WaitForShutdown(context.Background()); err != nil {
        panic(err)
    }      
}

Documentation

Overview

Package yethttp provides helpers for working with http.

Example:

type WebApp struct {
  yethttp.EmbeddableServerWrapper
}

func NewWebApp(logger yetlog.Logger) WebApp {
  serverWrapper := yethttp.NewEmbeddableServerWrapper(logger, 8080)

  return WebApp{
    EmbeddableServerWrapper: serverWrapper,
  }
}

func main() {
  webApp := NewWebApp(yetlog.NewNullLogger())
  if err := webApp.Serve(context.Background()); err != nil {
    panic(err)
  }

  if err := webApp.WaitForShutdown(context.Background()); err != nil {
    panic(err)
  }
}

Index

Constants

This section is empty.

Variables

View Source
var DefaultRoutesFunc = func() http.Handler {
	return http.DefaultServeMux
}

DefaultRoutesFunc uses the http.DefaultServeMux which shouldn't be used in any productive application. This method is meant to be used for prototyping or testing.

View Source
var DefaultStartServerFunc = func(server *http.Server) error {
	return server.ListenAndServe()
}

DefaultStartServerFunc provides a default implementation for starting a http.Server which basically calls its ListenAndServe() method.

Functions

This section is empty.

Types

type AfterShutdownFunc added in v0.6.0

type AfterShutdownFunc func()

AfterShutdownFunc is a function which will be executed after the server shuts down.

type EmbeddableServerWrapper added in v0.4.0

type EmbeddableServerWrapper struct {
	HttpServer *http.Server
	Port       int
	// contains filtered or unexported fields
}

EmbeddableServerWrapper wraps a http.Server and can handle server startup, routing and graceful shutdown.

func NewEmbeddableServerWrapper added in v0.4.0

func NewEmbeddableServerWrapper(logger yetlog.Logger, port int) EmbeddableServerWrapper

NewEmbeddableServerWrapper returns a new EmbeddableServerWrapper.

func (*EmbeddableServerWrapper) AddAfterShutdownFunc added in v0.6.0

func (e *EmbeddableServerWrapper) AddAfterShutdownFunc(afterShutdownFunc AfterShutdownFunc)

AddAfterShutdownFunc will add an AfterShutdownFunc to the server.

func (*EmbeddableServerWrapper) GracefulShutdown added in v0.4.0

func (e *EmbeddableServerWrapper) GracefulShutdown(ctx context.Context) error

GracefulShutdown will shutdown the underlying http.Server gracefully. This method can be overwritten to be able to use framework specific function calls.

func (*EmbeddableServerWrapper) Serve added in v0.4.0

func (e *EmbeddableServerWrapper) Serve(ctx context.Context, routesFunc RoutesFunc) error

Serve starts the server and listens for new connections.

func (*EmbeddableServerWrapper) SetStartServerFunc added in v0.4.1

func (e *EmbeddableServerWrapper) SetStartServerFunc(startServerFunc StartServerFunc)

SetStartServerFunc is used to overwrite the internal StartServerFunc. It should be called before using the Serve() method.

func (*EmbeddableServerWrapper) WaitForShutdown added in v0.4.0

func (e *EmbeddableServerWrapper) WaitForShutdown(ctx context.Context) error

WaitForShutdown blocks the go routine and will only continue when it receives a kill signal (SIGINT, SIGTERM, ...).

type RoutesFunc added in v0.5.1

type RoutesFunc func() http.Handler

RoutesFunc is used to define the server routes and handlers.

type StartServerFunc added in v0.4.1

type StartServerFunc func(server *http.Server) error

StartServerFunc is a function which can be used to customize the server start behavior.

Jump to

Keyboard shortcuts

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