Skip to main content

httpserver package

Package httpserver provides a convenient way to create HTTP servers.

Methods

New()

Usage

httpserver.New(definer.MyCustomDefiner)

Description

Creates a new ModuleFactory.

Definer()

Usage

func ApiDefiner(ctx context.Context, config cfg.Config, logger log.Logger) (*httpserver.Definitions, error) {
definitions := &httpserver.Definitions{}
definitions.GET("/hello-world", httpserver.CreateHandler(helloWorldHandler))
return definitions, nil
}

application.WithModuleFactory("api", httpserver.New(definer.ApiDefiner))

Description

This method returns a Definitions pointer.

The idiomatic way to create an httpserver is to:

  1. Instantiate a new Definitions object
  2. Use its many methods to add functionality to it,
  3. Declare a Definer(), which returns this object
  4. Call New().

Handle()

Usage

def := &httpserver.Definitions{}
def.Handle(http.PostRequest, relativePath, handlers...)

Description

With this method, you can define functionality to be run whenever your server receives an HTTP call to a given path.

Configuration

Further Reading

Check out our guide on configuring and running your server.

Settings

Configures an HTTP server.

FieldTypeDefaultDescription
Portstring8080Port the server listens to.
ModestringreleaseMode is either debug, release, test.
CompressionCompressionSettings-Compression settings.
TimeoutTimeoutSettings-Timeout settings.

Timeout settings

Configures IO timeouts.

FieldTypeDefaultDescription
Readtime.Duration60sRead timeout is the maximum duration for reading the entire request, including the body.
Writetime.Duration60sWrite timeout is the maximum duration before timing out writes of the response.
Idletime.Duration60sIdle timeout is the maximum amount of time to wait for the next request when keep-alives are enabled

Compression settings

Controls gzip support for requests and responses. By default, compressed requests are accepted and compressed responses are returned (if accepted by the client).

FieldTypeDefaultDescription
Levelstringdefault-
Decompressionbooltrue-
ExcludeCompressionExcludeSettings-Exclude files by path, extension, or regular expression from being considered for compression. Useful if you are serving a format unknown to Gosoline.

CompressionExcludeSettings

Allows you to enable gzip support.

FieldTypeDefaultDescription
Extensionarray of strings--
Patharray of strings--
PathRegexarray of strings--