httpserver

package
v0.0.0-...-5e472c2 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 9 Imported by: 0

README

httpserver

Package httpserver offers a fx module that utilizes http.Server and httprouter to handle HTTP requests.

Starts:

  • HTTP server

Depends on:

  • Logger, optional (type: *slog.Logger, name: "logger")
  • Routes (type: []Route, group: "routes")

You can configure it using these environment variables:

  • HTTP_SERVER_ADDRESS
  • HTTP_SERVER_READ_TIMEOUT
  • HTTP_SERVER_WRITE_TIMEOUT
  • HTTP_SERVER_IDLE_TIMEOUT
  • HTTP_SERVER_MAX_HEADER_BYTES

The default values:

  • HTTP_SERVER_ADDRESS: ":8080"
  • HTTP_SERVER_READ_TIMEOUT: "5s"
  • HTTP_SERVER_WRITE_TIMEOUT: "10s"
  • HTTP_SERVER_IDLE_TIMEOUT: "120s"
  • HTTP_SERVER_MAX_HEADER_BYTES: "1048576" (1MB)

Documentation

Overview

Package httpserver offers a fx module that utilizes http.Server and httprouter to handle HTTP requests.

Starts:

  • HTTP server

Depends on:

  • Logger, optional (type: *slog.Logger, name: "logger")
  • Routes (type: []Route, group: "routes")

You can configure it using these environment variables:

  • HTTP_SERVER_ADDRESS
  • HTTP_SERVER_READ_TIMEOUT
  • HTTP_SERVER_WRITE_TIMEOUT
  • HTTP_SERVER_IDLE_TIMEOUT
  • HTTP_SERVER_MAX_HEADER_BYTES

The default values:

  • HTTP_SERVER_ADDRESS: ":8080"
  • HTTP_SERVER_READ_TIMEOUT: "5s"
  • HTTP_SERVER_WRITE_TIMEOUT: "10s"
  • HTTP_SERVER_IDLE_TIMEOUT: "120s"
  • HTTP_SERVER_MAX_HEADER_BYTES: "1048576" (1MB)

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Module(
	"http_server",
	fx.Provide(
		New,
	),
	fx.Provide(
		fx.Private,

		parseConfig,
	),
	fx.Invoke(
		Start,
	),
	fx.Decorate(
		wrapLogger,
	),
)

Module is the http server module.

Functions

func Start

func Start(server *Server, l fx.Lifecycle)

Start starts the http server.

Types

type Config

type Config struct {
	Address        string
	ReadTimeout    time.Duration
	WriteTimeout   time.Duration
	IdleTimeout    time.Duration
	MaxHeaderBytes int
}

Config is the http server config.

type Middleware

type Middleware func(r Route) Route

Middleware is a HTTP middleware.

type Parameter

type Parameter struct {
	fx.In

	Logger *slog.Logger `optional:"true"`
	Routes []Route      `group:"routes"`
	Config Config
}

Parameter is the input parameter of the New function.

type Result

type Result struct {
	fx.Out

	Server *Server
}

Result is the output result of the New function.

func New

func New(parameter Parameter) (Result, error)

New creates a new http server.

type Route

type Route interface {
	http.Handler

	Method() string
	Path() string
}

Route defines the behavior of a route.

type Server

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

Server is the http server.

Jump to

Keyboard shortcuts

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