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:
- Instantiate a new
Definitionsobject - Use its many methods to add functionality to it,
- Declare a
Definer(), which returns this object - 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.
Related methods
Configuration
Further Reading
Check out our guide on configuring and running your server.
Settings
Configures an HTTP server.
| Field | Type | Default | Description |
|---|---|---|---|
| Port | string | 8080 | Port the server listens to. |
| Mode | string | release | Mode is either debug, release, test. |
| Compression | CompressionSettings | - | Compression settings. |
| Timeout | TimeoutSettings | - | Timeout settings. |
Timeout settings
Configures IO timeouts.
| Field | Type | Default | Description |
|---|---|---|---|
| Read | time.Duration | 60s | Read timeout is the maximum duration for reading the entire request, including the body. |
| Write | time.Duration | 60s | Write timeout is the maximum duration before timing out writes of the response. |
| Idle | time.Duration | 60s | Idle 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).
| Field | Type | Default | Description |
|---|---|---|---|
| Level | string | default | - |
| Decompression | bool | true | - |
| Exclude | CompressionExcludeSettings | - | 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.
| Field | Type | Default | Description |
|---|---|---|---|
| Extension | array of strings | - | - |
| Path | array of strings | - | - |
| PathRegex | array of strings | - | - |