Skip to content

Commit 59bde61

Browse files
committed
Add middleware
1 parent e187bd9 commit 59bde61

File tree

17 files changed

+137
-182
lines changed

17 files changed

+137
-182
lines changed

exemple/go.mod

Lines changed: 0 additions & 19 deletions
This file was deleted.

exemple/go.sum

Lines changed: 0 additions & 14 deletions
This file was deleted.

exemple/internal/app/app.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

exemple/internal/app/handler/handle_home.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

exemple/main.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

xfoundation/app.go

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package xfoundation
22

33
import (
44
"context"
5-
"fmt"
65
"go.uber.org/zap"
76
"os"
87
"os/signal"
@@ -17,21 +16,12 @@ type (
1716

1817
App struct {
1918
Container
20-
Env string
19+
Env AppEnv
2120
Logger *zap.Logger
22-
Providers []Provider
21+
Providers []any
2322
startHooks []func(ctx context.Context) error
2423
stopHooks []func(ctx context.Context) error
2524
}
26-
27-
Container struct {
28-
values map[reflect.Type]reflect.Value
29-
}
30-
)
31-
32-
const (
33-
AppEnvProduction = "production"
34-
AppEnvDevelopment = "development"
3525
)
3626

3727
func (app *App) Run() {
@@ -41,7 +31,7 @@ func (app *App) Run() {
4131
}
4232

4333
if app.Logger == nil {
44-
if app.Env == AppEnvProduction {
34+
if app.Env.Production() {
4535
app.Logger = panicOnError(zap.NewProduction())
4636
} else {
4737
app.Logger = panicOnError(zap.NewDevelopment())
@@ -51,8 +41,11 @@ func (app *App) Run() {
5141

5242
for _, provider := range app.Providers {
5343
log := app.Logger.With(zap.Any("provider", reflect.TypeOf(provider)))
54-
if err := provider.Register(app); err != nil {
55-
log.Fatal("failed to register provider", zap.Error(err))
44+
app.Provide(provider)
45+
if p, ok := provider.(Provider); ok {
46+
if err := p.Register(app); err != nil {
47+
log.Fatal("failed to register provider", zap.Error(err))
48+
}
5649
}
5750
log.Info("provider registered")
5851
}
@@ -83,37 +76,6 @@ func (app *App) OnStop(hook func(ctx context.Context) error) {
8376
app.stopHooks = append(app.stopHooks, hook)
8477
}
8578

86-
func (c *Container) Provide(v any) {
87-
c.values[reflect.TypeOf(v)] = reflect.ValueOf(v)
88-
}
89-
90-
func (c *Container) Invoke(f any) ([]reflect.Value, error) {
91-
fType := reflect.TypeOf(f)
92-
if fType.Kind() != reflect.Func {
93-
return nil, fmt.Errorf("app.Invoke: invalid func type %v", fType)
94-
}
95-
var dependencies []reflect.Value
96-
for i := 0; i < fType.NumIn(); i++ {
97-
depType := fType.In(i)
98-
value, ok := c.values[depType]
99-
if !ok {
100-
return nil, fmt.Errorf("app.Invoke: cannot find dependency %v", depType)
101-
}
102-
dependencies = append(dependencies, value)
103-
}
104-
105-
returnValues := reflect.ValueOf(f).Call(dependencies)
106-
if returnValuesLen := len(returnValues); returnValuesLen > 0 {
107-
if err, ok := returnValues[returnValuesLen-1].Interface().(error); ok {
108-
if err != nil {
109-
return nil, err
110-
}
111-
returnValues = returnValues[:returnValuesLen-1]
112-
}
113-
}
114-
return returnValues, nil
115-
}
116-
11779
func panicOnError[T any](value T, err error) T {
11880
if err != nil {
11981
panic(err)

xfoundation/container.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package xfoundation
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
type Container struct {
9+
values map[reflect.Type]reflect.Value
10+
}
11+
12+
func (c *Container) Provide(v any) {
13+
c.values[reflect.TypeOf(v)] = reflect.ValueOf(v)
14+
}
15+
16+
func (c *Container) Invoke(f any) ([]reflect.Value, error) {
17+
fType := reflect.TypeOf(f)
18+
if fType.Kind() != reflect.Func {
19+
return nil, fmt.Errorf("app.Invoke: invalid func type %v", fType)
20+
}
21+
var dependencies []reflect.Value
22+
for i := 0; i < fType.NumIn(); i++ {
23+
depType := fType.In(i)
24+
value, ok := c.values[depType]
25+
if !ok {
26+
return nil, fmt.Errorf("app.Invoke: cannot find dependency %v", depType)
27+
}
28+
dependencies = append(dependencies, value)
29+
}
30+
31+
returnValues := reflect.ValueOf(f).Call(dependencies)
32+
if returnValuesLen := len(returnValues); returnValuesLen > 0 {
33+
if err, ok := returnValues[returnValuesLen-1].Interface().(error); ok {
34+
if err != nil {
35+
return nil, err
36+
}
37+
returnValues = returnValues[:returnValuesLen-1]
38+
}
39+
}
40+
return returnValues, nil
41+
}

xfoundation/env.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package xfoundation
2+
3+
type AppEnv string
4+
5+
const (
6+
AppEnvProduction AppEnv = "production"
7+
AppEnvDevelopment AppEnv = "development"
8+
)
9+
10+
func (e AppEnv) Production() bool {
11+
return e == "production"
12+
}

xfoundation/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module github.com/caumette-co/x/xfoundation
33
go 1.21.0
44

55
require (
6+
github.com/jinzhu/inflection v1.0.0 // indirect
7+
github.com/jinzhu/now v1.1.5 // indirect
68
go.uber.org/dig v1.17.0 // indirect
79
go.uber.org/multierr v1.11.0 // indirect
810
go.uber.org/zap v1.25.0 // indirect
11+
gorm.io/gorm v1.25.4 // indirect
912
)

xfoundation/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
2+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
3+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
4+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
15
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
26
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU=
37
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
48
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
59
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
610
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
11+
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
12+
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

0 commit comments

Comments
 (0)