Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# GoZix Echo

[documentation-img]: https://img.shields.io/badge/godoc-reference-blue.svg?color=24B898&style=for-the-badge&logo=go&logoColor=ffffff
[documentation-url]: https://pkg.go.dev/github.com/gozix/echo/v3
[documentation-url]: https://pkg.go.dev/github.com/gozix/echo/v4
[license-img]: https://img.shields.io/github/license/gozix/echo.svg?style=for-the-badge
[license-url]: https://github.com/gozix/echo/blob/master/LICENSE
[release-img]: https://img.shields.io/github/tag/gozix/echo.svg?label=release&color=24B898&logo=github&style=for-the-badge
Expand All @@ -26,7 +26,7 @@ The bundle provide a Echo integration to GoZix application.
## Installation

```shell
go get github.com/gozix/echo/v3
go get github.com/gozix/echo/v4
```

## Dependencies
Expand All @@ -35,30 +35,43 @@ go get github.com/gozix/echo/v3
* [viper](https://github.com/gozix/viper)
* [zap](https://github.com/gozix/zap)

## Configuration
## Configuration 3 or more servers

```json
{
"echo": {
"debug": false,
"level": "debug",
"static": {
"prefix": "/",
"root": ""
"admin": {
"host": "0.0.0.0",
"port": 8080,
"level": "debug",
"debug": false,
"hide_banner": true,
"hide_port": false
},
"public": {
"host": "0.0.0.0",
"port": 8081,
"static": {
"prefix": "/",
"root": ""
},
"level": "info",
"debug": false,
"hide_banner": true,
"hide_port": false
},
"hide_banner": false,
"hide_port": false
"private": {
"port": 8082,
"...": "..."
},
"...": {}
}
}
```

## Built-in Tags

| Symbol | Value | Description |
| --------------------- | ----------------------------- | ------------------------- |
| TagController | echo.controller | Add a controller |
| TagConfigurator | echo.configurator | Add a configurator |
| TagMiddleware | echo.middleware | Add a middleware |
```golang
gzEcho.NewBundle("admin", "public", "private")
```

## Documentation

Expand Down
61 changes: 49 additions & 12 deletions di.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,68 @@ import (
)

const (
// tagConfigurator is tag to mark middleware's.
// tagServerName is tag to mark a server name value.
tagServerName = "echo.server_name"

// tagEcho is tag to mark a echo instance.
tagEcho = "echo.echo"

// tagConfigurator is tag to mark configurators.
tagConfigurator = "echo.configurator"

// tagMiddleware is tag to mark middleware's.
// tagController is tag to mark controllers.
tagController = "echo.controller"

// tagMiddleware is tag to mark middlewares.
tagMiddleware = "echo.middleware"

// argMiddlewarePriority is name of priority argument.
argMiddlewarePriority = "priority"
)

// AsConfigurator is syntax sugar for the di container.
func AsConfigurator() di.ProvideOption {
func AsConfigurator(srvName string) di.ProvideOption {
return di.Tags{{
Name: tagConfigurator,
Name: tagConfigurator + "." + srvName,
}}
}

// AsController is syntax sugar for the di container.
func AsController() di.ProvideOption {
return di.As(new(Controller))
func AsController(srvName string) di.ProvideOption {
return di.ProvideOptions(di.Tags{{
Name: tagController + "." + srvName,
}}, di.As(new(Controller)))
}

// AsMiddleware is syntax sugar for the di container.
func AsMiddleware(priority int64) di.ProvideOption {
func AsMiddleware(srvName string, priority int64) di.ProvideOption {
return di.Tags{{
Name: tagMiddleware,
Name: tagMiddleware + "." + srvName,
Args: di.Args{{
Key: argMiddlewarePriority,
Value: strconv.FormatInt(priority, 10),
}},
}}
}

func asServerName(srvName string) di.AddOption {
return di.Tags{{
Name: tagServerName,
Args: di.Args{{
Key: "name",
Value: srvName,
}},
}, {
Name: tagServerName + "." + srvName,
}}
}

func asEcho(srvName string) di.ProvideOption {
return di.Tags{{
Name: tagEcho + "." + srvName,
}}
}

func sortByPriority() di.Modifier {
return di.Sort(func(x, y di.Definition) bool {
var xp int64
Expand All @@ -64,10 +93,18 @@ func sortByPriority() di.Modifier {
})
}

func withConfigurator() di.Modifier {
return di.WithTags(tagConfigurator)
func withServerName(srvName string) di.Modifier {
return di.WithTags(tagServerName + "." + srvName)
}

func withConfigurator(srvName string) di.Modifier {
return di.WithTags(tagConfigurator + "." + srvName)
}

func withController(srvName string) di.Modifier {
return di.WithTags(tagController + "." + srvName)
}

func withMiddleware() di.Modifier {
return di.WithTags(tagMiddleware)
func withMiddleware(srvName string) di.Modifier {
return di.WithTags(tagMiddleware + "." + srvName)
}
63 changes: 42 additions & 21 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (
gzViper "github.com/gozix/viper/v3"
gzZap "github.com/gozix/zap/v3"

"github.com/gozix/echo/v3/internal/command"
"github.com/gozix/echo/v3/internal/configurator"
"github.com/gozix/echo/v3/internal/echo"
"github.com/gozix/echo/v4/internal/command"
"github.com/gozix/echo/v4/internal/configurator"
"github.com/gozix/echo/v4/internal/echo"
)

type (
// Bundle implements the glue.Bundle interface.
Bundle struct{}
Bundle struct {
svrNames []string
}

// Configurator is type alias of configurator.Configurator.
Configurator = configurator.Configurator
Expand All @@ -33,8 +35,10 @@ const BundleName = "echo"
var _ glue.Bundle = (*Bundle)(nil)

// NewBundle create bundle instance.
func NewBundle() *Bundle {
return new(Bundle)
func NewBundle(svrNames ...string) *Bundle {
return &Bundle{
svrNames: svrNames,
}
}

// Name implements the glue.Bundle interface.
Expand All @@ -44,23 +48,40 @@ func (b *Bundle) Name() string {

// Build implements the glue.Bundle interface.
func (b *Bundle) Build(builder di.Builder) error {
return builder.Apply(
// echo
di.Provide(echo.New, di.Constraint(0, di.Optional(true), withConfigurator())),

// command's
var opt = []di.BuilderOption{
di.Provide(command.NewHTTPServer, glue.AsCliCommand()),
}

for _, srvName := range b.svrNames {
opt = append(opt, di.BuilderOptions(
// server name
di.Add(srvName, asServerName(srvName)),

// echo
di.Provide(
echo.New, asEcho(srvName),
di.Constraint(0, withConfigurator(srvName)),
),

// configurators
di.Provide(
configurator.NewController, AsConfigurator(srvName),
di.Constraint(0, withController(srvName), di.Optional(true)),
),
di.Provide(
configurator.NewMiddleware, AsConfigurator(srvName),
di.Constraint(0, withMiddleware(srvName), di.Optional(true), sortByPriority()),
),
di.Provide(
configurator.NewEcho, AsConfigurator(srvName),
di.Constraint(0, withServerName(srvName)),
),
di.Provide(configurator.NewErrHandler, AsConfigurator(srvName)),
di.Provide(configurator.NewValidator, AsConfigurator(srvName)),
))
}

// configurator's
di.Provide(configurator.NewController, di.Constraint(0, di.Optional(true)), AsConfigurator()),
di.Provide(configurator.NewEcho, AsConfigurator()),
di.Provide(configurator.NewErrHandler, AsConfigurator()),
di.Provide(
configurator.NewMiddleware, AsConfigurator(),
di.Constraint(0, withMiddleware(), sortByPriority()),
),
di.Provide(configurator.NewValidator, AsConfigurator()),
)
return builder.Apply(opt...)
}

// DependsOn implements the glue.DependsOn interface.
Expand Down
67 changes: 33 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
module github.com/gozix/echo/v3
module github.com/gozix/echo/v4

go 1.18
go 1.23.0

require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.11.2
github.com/gozix/di v1.0.0
github.com/go-playground/validator/v10 v10.26.0
github.com/gobwas/glob v0.2.3
github.com/gozix/di v1.0.3
github.com/gozix/glue/v3 v3.0.0
github.com/gozix/validator/v3 v3.0.0
github.com/gozix/viper/v3 v3.0.0
github.com/gozix/viper/v3 v3.0.3
github.com/gozix/zap/v3 v3.0.0
github.com/labstack/echo/v4 v4.10.1
github.com/labstack/gommon v0.4.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0
github.com/labstack/echo/v4 v4.13.4
github.com/labstack/gommon v0.4.2
github.com/spf13/cobra v1.9.1
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.14.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gozix/universal-translator/v3 v3.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/leodido/go-urn v1.2.1 // 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.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
golang.org/x/time v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading