httpserver

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 10 Imported by: 1

README

HTTP Server

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Info

type Info struct {
	Addr string
}

Info holds relevant information about the Server. This can be used in the future to hold information about: - number of requests received - average response time.

type Option

type Option interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

An Option configures an App using the functional options paradigm popularized by Rob Pike. If you're unfamiliar with this style, see nolint: revive // line too long. https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html and https://github.com/uber-go/guide/blob/master/style.md#functional-options

func WithAddress

func WithAddress(address string) Option

WithAddress will set the address field of the server.

Example
package main

import (
	"fmt"

	"github.com/purposeinplay/go-commons/httpserver"
)

func main() {
	opt := httpserver.WithAddress(":8080")

	fmt.Println(opt)
}
Output:

server.Address: :8080

func WithBaseContext

func WithBaseContext(ctx context.Context, cancelContextOnShutdown bool) Option

WithBaseContext sets a predefined base context for all incoming http requests.

If cancelOnShutdown is set to true it will mark the baseContext as done(close the Done channel) whenever the server.Shutdown() method is called.

This is intended to use with long living tcp connections like Websockets in order to cancel the current open connections and allow the server to shutdown.

Example

nolint: revive // line too long.

package main

import (
	"context"
	"fmt"

	"github.com/purposeinplay/go-commons/httpserver"
)

func main() {
	type key string

	ctx := context.WithValue(context.Background(), key("server"), "example")

	opt := httpserver.WithBaseContext(ctx, true)

	fmt.Println(opt)
}
Output:

server.BaseContext: context.Background.WithValue(type httpserver_test.key, val example)
server.CancelContextOnShutdown: true

func WithServerTimeouts

func WithServerTimeouts(
	writeTimeout,
	readTimeout,
	idleTimeout,
	readHeaderTimeout time.Duration,
) Option

WithServerTimeouts will set the timeouts for the underlying HTTP server.

Example
package main

import (
	"fmt"
	"time"

	"github.com/purposeinplay/go-commons/httpserver"
)

func main() {
	opt := httpserver.WithServerTimeouts(
		time.Nanosecond,
		2*time.Nanosecond,
		3*time.Nanosecond,
		4*time.Nanosecond,
	)

	fmt.Println(opt)
}
Output:

server.WriteTimeout: 1
server.ReadTimeout: 2
server.IdleTimeout: 3
server.ReadHeaderTimeout: 4

func WithShutdownSignalsOption

func WithShutdownSignalsOption(signals ...os.Signal) Option

WithShutdownSignalsOption will attempt to shutdown the server when one of the proved os signals is sent to the application.

Example
package main

import (
	"fmt"
	"syscall"

	"github.com/purposeinplay/go-commons/httpserver"
)

func main() {
	opt := httpserver.WithShutdownSignalsOption(
		syscall.SIGINT,
		syscall.SIGTERM)

	fmt.Println(opt)
}
Output:

server.ShutdownSignals: [interrupt terminated]

type Server

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

Server handles the setup and shutdown of the http server for an http.Handler.

func New

func New(log *zap.Logger, handler http.Handler, options ...Option) *Server

New will build a server with the defaults in place. You can use Options to override the defaults. Default list: - Address: ":8080".

func (*Server) Info

func (s *Server) Info() Info

Info returns the server.Info object.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe is a wrapper over http.Server.ListenAndServe() that logs basic information and blocks execution until the Server.Shutdown() method is called.

func (*Server) Serve

func (s *Server) Serve(ln net.Listener) error

Serve is a wrapper over http.Server.Serve(), and accepts incoming connections on the provided listener.

func (*Server) Shutdown

func (s *Server) Shutdown(timeout time.Duration) error

Shutdown is a wrapper over http.Server.Shutdown() that also closes the Server done channel and sets a timeout for the shutdown operation.

Jump to

Keyboard shortcuts

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