-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fxhttpclient): Provided module (#38)
- Loading branch information
Showing
13 changed files
with
1,222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: "fxhttpclient-ci" | ||
|
||
on: | ||
push: | ||
branches: | ||
- "feat**" | ||
- "fix**" | ||
- "hotfix**" | ||
- "chore**" | ||
paths: | ||
- "fxhttpclient/**.go" | ||
- "fxhttpclient/go.mod" | ||
- "fxhttpclient/go.sum" | ||
pull_request: | ||
types: | ||
- opened | ||
- synchronize | ||
- reopened | ||
branches: | ||
- main | ||
paths: | ||
- "fxhttpclient/**.go" | ||
- "fxhttpclient/go.mod" | ||
- "fxhttpclient/go.sum" | ||
|
||
jobs: | ||
ci: | ||
uses: ./.github/workflows/common-ci.yml | ||
secrets: inherit | ||
with: | ||
module: "fxhttpclient" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
run: | ||
timeout: 5m | ||
concurrency: 8 | ||
|
||
linters: | ||
enable: | ||
- asasalint | ||
- asciicheck | ||
- bidichk | ||
- bodyclose | ||
- containedctx | ||
- contextcheck | ||
- cyclop | ||
- decorder | ||
- dogsled | ||
- dupl | ||
- durationcheck | ||
- errcheck | ||
- errchkjson | ||
- errname | ||
- errorlint | ||
- exhaustive | ||
- forbidigo | ||
- forcetypeassert | ||
- gocognit | ||
- goconst | ||
- gocritic | ||
- gocyclo | ||
- godot | ||
- godox | ||
- gofmt | ||
- goheader | ||
- gomoddirectives | ||
- gomodguard | ||
- goprintffuncname | ||
- gosec | ||
- gosimple | ||
- govet | ||
- grouper | ||
- importas | ||
- ineffassign | ||
- interfacebloat | ||
- logrlint | ||
- maintidx | ||
- makezero | ||
- misspell | ||
- nestif | ||
- nilerr | ||
- nilnil | ||
- nlreturn | ||
- nolintlint | ||
- nosprintfhostport | ||
- prealloc | ||
- predeclared | ||
- promlinter | ||
- reassign | ||
- staticcheck | ||
- tenv | ||
- thelper | ||
- tparallel | ||
- typecheck | ||
- unconvert | ||
- unparam | ||
- unused | ||
- usestdlibvars | ||
- whitespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
# Fx Http Client Module | ||
|
||
[![ci](https://github.com/ankorstore/yokai/actions/workflows/fxhttpclient-ci.yml/badge.svg)](https://github.com/ankorstore/yokai/actions/workflows/fxhttpclient-ci.yml) | ||
[![go report](https://goreportcard.com/badge/github.com/ankorstore/yokai/fxhttpclient)](https://goreportcard.com/report/github.com/ankorstore/yokai/fxhttpclient) | ||
[![codecov](https://codecov.io/gh/ankorstore/yokai/graph/badge.svg?token=ghUBlFsjhR&flag=fxhttpclient)](https://app.codecov.io/gh/ankorstore/yokai/tree/main/fxhttpclient) | ||
[![Deps](https://img.shields.io/badge/osi-deps-blue)](https://deps.dev/go/github.com%2Fankorstore%2Fyokai%2Ffxhttpclient) | ||
[![PkgGoDev](https://pkg.go.dev/badge/github.com/ankorstore/yokai/fxhttpclient)](https://pkg.go.dev/github.com/ankorstore/yokai/fxhttpclient) | ||
|
||
> [Fx](https://uber-go.github.io/fx/) module for [httpclient](https://github.com/ankorstore/yokai/tree/main/httpclient). | ||
<!-- TOC --> | ||
* [Installation](#installation) | ||
* [Features](#features) | ||
* [Documentation](#documentation) | ||
* [Dependencies](#dependencies) | ||
* [Loading](#loading) | ||
* [Configuration](#configuration) | ||
* [Override](#override) | ||
<!-- TOC --> | ||
|
||
## Installation | ||
|
||
```shell | ||
go get github.com/ankorstore/yokai/fxhttpclient | ||
``` | ||
|
||
## Features | ||
|
||
This module provides the possibility to provide to your Fx application a `http.Client` with: | ||
|
||
- configurable transport | ||
- automatic and configurable request / response logging | ||
- configurable request / response tracing | ||
|
||
## Documentation | ||
|
||
### Dependencies | ||
|
||
This module is intended to be used alongside: | ||
|
||
- the [fxconfig](https://github.com/ankorstore/yokai/tree/main/fxconfig) module | ||
- the [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module | ||
- the [fxtrace](https://github.com/ankorstore/yokai/tree/main/fxtrace) module | ||
|
||
### Loading | ||
|
||
To load the module in your Fx application: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ankorstore/yokai/fxconfig" | ||
"github.com/ankorstore/yokai/fxhttpclient" | ||
"github.com/ankorstore/yokai/fxlog" | ||
"github.com/ankorstore/yokai/fxtrace" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func main() { | ||
fx.New( | ||
fxconfig.FxConfigModule, // load the module dependencies | ||
fxlog.FxLogModule, | ||
fxtrace.FxTraceModule, | ||
fxhttpclient.FxHttpClientModule, // load the module | ||
fx.Invoke(func(httpClient *http.Client) { // invoke the client | ||
resp, err := httpClient.Get("https://example.com") | ||
}), | ||
).Run() | ||
} | ||
``` | ||
|
||
### Configuration | ||
|
||
Configuration reference: | ||
|
||
```yaml | ||
# ./configs/config.yaml | ||
app: | ||
name: app | ||
env: dev | ||
version: 0.1.0 | ||
debug: true | ||
modules: | ||
log: | ||
level: info | ||
output: stdout | ||
trace: | ||
processor: | ||
type: stdout | ||
http: | ||
client: | ||
timeout: 30 # in seconds, 30 by default | ||
transport: | ||
max_idle_connections: 100 # 100 by default | ||
max_connections_per_host: 100 # 100 by default | ||
max_idle_connections_per_host: 100 # 100 by default | ||
log: | ||
request: | ||
enabled: true # to log request details, disabled by default | ||
body: true # to add request body to request details, disabled by default | ||
level: info # log level for request logging | ||
response: | ||
enabled: true # to log response details, disabled by default | ||
body: true # to add response body to request details, disabled by default | ||
level: info # log level for response logging | ||
level_from_response: true # to use response code for response logging | ||
trace: | ||
enabled: true # to trace http calls, disabled by default | ||
``` | ||
If `http.client.log.response.level_from_response=true`, the response code will be used to determinate the log level: | ||
|
||
- `code < 400`: log level configured in `http.client.log.response.level` | ||
- `400 <= code < 500`: log level `warn` | ||
- `code >= 500`: log level `error` | ||
|
||
Notes: | ||
|
||
- the http client logging will be based on the [fxlog](https://github.com/ankorstore/yokai/tree/main/fxlog) module | ||
configuration | ||
- the http client tracing will be based on the [fxtrace](https://github.com/ankorstore/yokai/tree/main/fxtrace) module | ||
configuration | ||
|
||
### Override | ||
|
||
By default, the `http.Client` is created by | ||
the [DefaultHttpClientFactory](https://github.com/ankorstore/yokai/blob/main/httpclient/factory.go). | ||
|
||
If needed, you can provide your own factory and override the module: | ||
|
||
```go | ||
package main | ||
import ( | ||
"net/http" | ||
"github.com/ankorstore/yokai/fxconfig" | ||
"github.com/ankorstore/yokai/fxhttpclient" | ||
"github.com/ankorstore/yokai/fxlog" | ||
"github.com/ankorstore/yokai/fxtrace" | ||
"github.com/ankorstore/yokai/httpclient" | ||
"go.uber.org/fx" | ||
) | ||
type CustomHttpClientFactory struct{} | ||
func NewCustomHttpClientFactory() httpclient.HttpClientFactory { | ||
return &CustomHttpClientFactory{} | ||
} | ||
func (f *CustomHttpClientFactory) Create(options ...httpclient.HttpClientOption) (*http.Client, error) { | ||
return http.DefaultClient, nil | ||
} | ||
func main() { | ||
fx.New( | ||
fxconfig.FxConfigModule, // load the module dependencies | ||
fxlog.FxLogModule, | ||
fxtrace.FxTraceModule, | ||
fxhttpclient.FxHttpClientModule, // load the module | ||
fx.Decorate(NewCustomHttpClientFactory), // override the module with a custom factory | ||
fx.Invoke(func(httpClient *http.Client) { // invoke the custom client | ||
// ... | ||
}), | ||
).Run() | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module github.com/ankorstore/yokai/fxhttpclient | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/ankorstore/yokai/config v1.1.0 | ||
github.com/ankorstore/yokai/fxconfig v1.0.0 | ||
github.com/ankorstore/yokai/fxlog v1.0.0 | ||
github.com/ankorstore/yokai/fxtrace v1.1.0 | ||
github.com/ankorstore/yokai/httpclient v1.0.0 | ||
github.com/ankorstore/yokai/log v1.0.0 | ||
github.com/ankorstore/yokai/trace v1.0.0 | ||
github.com/stretchr/testify v1.8.4 | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 | ||
go.opentelemetry.io/otel v1.16.0 | ||
go.opentelemetry.io/otel/trace v1.16.0 | ||
go.uber.org/fx v1.20.1 | ||
) | ||
|
||
require ( | ||
github.com/cenkalti/backoff/v4 v4.2.1 // indirect | ||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect | ||
github.com/felixge/httpsnoop v1.0.3 // indirect | ||
github.com/fsnotify/fsnotify v1.7.0 // indirect | ||
github.com/go-logr/logr v1.2.4 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/rs/zerolog v1.31.0 // indirect | ||
github.com/sagikazarmark/locafero v0.4.0 // indirect | ||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect | ||
github.com/sourcegraph/conc v0.3.0 // indirect | ||
github.com/spf13/afero v1.11.0 // indirect | ||
github.com/spf13/cast v1.6.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/spf13/viper v1.18.2 // indirect | ||
github.com/subosito/gotenv v1.6.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect | ||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect | ||
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.16.0 // indirect | ||
go.opentelemetry.io/otel/sdk v1.16.0 // indirect | ||
go.opentelemetry.io/proto/otlp v0.19.0 // indirect | ||
go.uber.org/dig v1.17.1 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
go.uber.org/zap v1.26.0 // indirect | ||
golang.org/x/exp v0.0.0-20240110193028-0dcbfd608b1e // indirect | ||
golang.org/x/net v0.19.0 // indirect | ||
golang.org/x/sys v0.16.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17 // indirect | ||
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect | ||
google.golang.org/grpc v1.59.0 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.