-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
126 lines (108 loc) · 3.55 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package fxgomysqlserver
import (
"context"
"fmt"
"os"
"github.com/ankorstore/yokai-contrib/fxgomysqlserver/config"
"github.com/ankorstore/yokai-contrib/fxgomysqlserver/server"
yokaiconfig "github.com/ankorstore/yokai/config"
"github.com/ankorstore/yokai/log"
sqle "github.com/dolthub/go-mysql-server/server"
oteltrace "go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
)
// ModuleName is the module name.
const ModuleName = "gomysqlserver"
// FxGoMySQLServerModule is the [Fx] go-mysql-server module.
//
// [Fx]: https://github.com/uber-go/fx
var FxGoMySQLServerModule = fx.Module(
ModuleName,
fx.Provide(
fx.Annotate(
server.NewDefaultGoMySQLServerFactory,
fx.As(new(server.GoMySQLServerFactory)),
),
fx.Annotate(
NewFxGoMySQLServerModuleInfo,
fx.As(new(interface{})),
fx.ResultTags(`group:"core-module-infos"`),
),
NewFxGoMySQLServerConfig,
NewFxGoMySQLServer,
),
)
// FxGoMySQLServerConfigParam allows injection of the required dependencies in [NewGoMySQLServerConfig].
type FxGoMySQLServerConfigParam struct {
fx.In
Config *yokaiconfig.Config
}
// NewFxGoMySQLServerConfig returns a new [config.GoMySQLServerConfig] instance.
func NewFxGoMySQLServerConfig(p FxGoMySQLServerConfigParam) (*config.GoMySQLServerConfig, error) {
// server transport
transportConfig := p.Config.GetString("modules.gomysqlserver.config.transport")
transport := config.FetchTransport(transportConfig)
if transport == config.UnknownTransport {
return nil, fmt.Errorf("unknown transport: %s", transportConfig)
}
// server config
return config.NewGoMySQLServerConfig(
config.WithTransport(transport),
config.WithUser(p.Config.GetString("modules.gomysqlserver.config.user")),
config.WithPassword(p.Config.GetString("modules.gomysqlserver.config.password")),
config.WithSocket(p.Config.GetString("modules.gomysqlserver.config.socket")),
config.WithHost(p.Config.GetString("modules.gomysqlserver.config.host")),
config.WithPort(p.Config.GetInt("modules.gomysqlserver.config.port")),
config.WithDatabase(p.Config.GetString("modules.gomysqlserver.config.database")),
), nil
}
// FxGoMySQLServerParam allows injection of the required dependencies in [NewFxGoMySQLServer].
type FxGoMySQLServerParam struct {
fx.In
LifeCycle fx.Lifecycle
ServerFactory server.GoMySQLServerFactory
ServerConfig *config.GoMySQLServerConfig
Config *yokaiconfig.Config
Logger *log.Logger
TracerProvider oteltrace.TracerProvider
}
// NewFxGoMySQLServer returns a new [sqle.Server] instance.
func NewFxGoMySQLServer(p FxGoMySQLServerParam) (*sqle.Server, error) {
// server config
options := []server.GoMySQLServerOption{
server.WithConfig(p.ServerConfig),
}
// server logger
if p.Config.GetBool("modules.gomysqlserver.log.enabled") {
options = append(options, server.WithLogOutput(os.Stdout))
}
// server tracer
if p.Config.GetBool("modules.gomysqlserver.trace.enabled") {
options = append(options, server.WithTracer(p.TracerProvider.Tracer(ModuleName)))
}
// server creation
srv, err := p.ServerFactory.Create(options...)
if err != nil {
return nil, err
}
// server dsn
dsn, err := p.ServerConfig.DSN()
if err != nil {
return nil, err
}
// server start
p.Logger.Info().Msgf("starting go mysql server, accepting connections on %s", dsn)
//nolint:errcheck
go srv.Start()
// server lifecycle
p.LifeCycle.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
if p.ServerConfig.Transport() != config.MemoryTransport {
p.Logger.Info().Msg("stopping go mysql server")
return srv.Close()
}
return nil
},
})
return srv, nil
}