Skip to content

Commit e6d1d23

Browse files
committed
refactor(tests): use a separate database webhookx_test for test
1 parent ac0993e commit e6d1d23

36 files changed

+339
-252
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ FROM alpine:3.15
1414
COPY --from=build-env /go/src/webhookx-io/webhookx/webhookx /usr/local/bin
1515

1616
EXPOSE 9600
17+
EXPOSE 9601
18+
EXPOSE 9602
1719

1820

1921
CMD ["webhookx", "start"]

app/app.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,11 @@ func (app *Application) initialize() error {
218218
Dispatcher: dispatcher,
219219
EventBus: app.bus,
220220
}
221-
if cfg.AccessLog.Enabled() {
221+
if cfg.AccessLog.Enabled {
222222
accessLogger, err := accesslog.NewAccessLogger("admin", accesslog.Options{
223-
File: cfg.AccessLog.File,
224-
Format: string(cfg.AccessLog.Format),
223+
File: cfg.AccessLog.File,
224+
Format: string(cfg.AccessLog.Format),
225+
Colored: cfg.AccessLog.Colored,
225226
})
226227
if err != nil {
227228
return err
@@ -247,10 +248,11 @@ func (app *Application) initialize() error {
247248
Srv: app.srv,
248249
RateLimiter: ratelimiter.NewRedisLimiter(client),
249250
}
250-
if cfg.AccessLog.Enabled() {
251+
if cfg.AccessLog.Enabled {
251252
accessLogger, err := accesslog.NewAccessLogger("proxy", accesslog.Options{
252-
File: cfg.AccessLog.File,
253-
Format: string(cfg.AccessLog.Format),
253+
File: cfg.AccessLog.File,
254+
Format: string(cfg.AccessLog.Format),
255+
Colored: cfg.AccessLog.Colored,
254256
})
255257
if err != nil {
256258
return err
@@ -268,10 +270,11 @@ func (app *Application) initialize() error {
268270

269271
if cfg.Status.IsEnabled() {
270272
var accessLogger accesslog.AccessLogger
271-
if cfg.AccessLog.Enabled() {
273+
if cfg.AccessLog.Enabled {
272274
accessLogger, err = accesslog.NewAccessLogger("status", accesslog.Options{
273-
File: cfg.AccessLog.File,
274-
Format: string(cfg.AccessLog.Format),
275+
File: cfg.AccessLog.File,
276+
Format: string(cfg.AccessLog.Format),
277+
Colored: cfg.AccessLog.Colored,
275278
})
276279
if err != nil {
277280
return err

cmd/admin_dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func newAdminDumpCmd() *cobra.Command {
7070
}
7171

7272
dump.Flags().StringVarP(&workspace, "workspace", "", "default", "Set a specific workspace.")
73-
dump.Flags().StringVarP(&addr, "addr", "", defaultAdminURL, "HTTP address of WebhookX's Admin API.")
73+
dump.Flags().StringVarP(&addr, "addr", "", AdminURL, "HTTP address of WebhookX's Admin API.")
7474
dump.Flags().IntVarP(&timeout, "timeout", "", 10, "Set the request timeout for the client to connect with WebhookX (in seconds).")
7575

7676
return dump

cmd/admin_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func newAdminSyncCmd() *cobra.Command {
8080
}
8181

8282
sync.Flags().StringVarP(&workspace, "workspace", "", "default", "Set a specific workspace.")
83-
sync.Flags().StringVarP(&addr, "addr", "", defaultAdminURL, "HTTP address of WebhookX's Admin API.")
83+
sync.Flags().StringVarP(&addr, "addr", "", AdminURL, "HTTP address of WebhookX's Admin API.")
8484
sync.Flags().IntVarP(&timeout, "timeout", "", 10, "Set the request timeout for the client to connect with WebhookX (in seconds).")
8585

8686
return sync

cmd/root.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"os"
77
)
88

9-
const (
10-
defaultAdminURL = "http://localhost:9601"
9+
var (
10+
AdminURL = "http://localhost:9601"
1111
)
1212

1313
var (
@@ -18,11 +18,15 @@ var (
1818

1919
func initConfig() {
2020
var err error
21+
22+
var options config.Options
2123
if configurationFile != "" {
22-
cfg, err = config.InitWithFile(configurationFile)
23-
} else {
24-
cfg, err = config.Init()
24+
buf, err := os.ReadFile(configurationFile)
25+
cobra.CheckErr(err)
26+
options.YAML = buf
2527
}
28+
29+
cfg, err = config.New(&options)
2630
cobra.CheckErr(err)
2731

2832
err = cfg.Validate()

config.yml

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
# ------------------------------------
2-
# WebhookX Configuration YAML file
2+
# WebhookX Configuration YAML File
33
# ------------------------------------
4+
45
log:
5-
file: /dev/stdout # Specifies the log file.
66
level: info # Supported values: debug, info, warn, and error.
77
format: text # Supported values: text, json.
8+
colored: true # Enables colorized logs when using text format.
9+
file: '' # By default, logs are written to stdout.
810

9-
access_log: # Access Log
10-
file: /dev/stdout # Specifies the log file.
11+
access_log:
12+
enabled: true # Enables access log.
1113
format: text # Supported values: text, json.
14+
colored: true # Enables colorized logs when using text format.
15+
file: '' # By default, logs are written to stdout.
1216

1317
database:
1418
host: localhost
@@ -20,24 +24,28 @@ database:
2024
# Connection uri parameters.
2125
# See https://www.postgresql.org/docs/current/libpq-connect.html for more information.
2226
max_pool_size: 40 # Specifies the maximum number of connections.
23-
max_lifetime: 1800 # Specifies the maximum lifetime (in seconds) for a connection.
27+
max_lifetime: 1800 # Specifies the maximum lifetime (in seconds) of a connection.
2428

2529
redis:
2630
host: localhost
2731
port: 6379
2832
password:
2933
database: 0
34+
max_pool_size: 0 # Specifies the maximum number of connections.
35+
# Default is 10 connections per available CPU.
36+
3037

3138
#------------------------------------------------------------------------------
32-
# PROXY
39+
# Proxy
40+
# The Proxy ingests events.
3341
#------------------------------------------------------------------------------
3442
proxy:
3543
listen: 0.0.0.0:9600
36-
tls: # TLS configuration
37-
cert: '' # The path to TLS certificate. Example: /path/to/server.cert
38-
key: '' # The path to TLS certificate key. Example: /path/to/server.key
39-
timeout_read: 10 # Specifies the maximum time (in seconds) for reading request. 0 disables timeout.
40-
timeout_write: 10 # Specifies the maximum time (in seconds) for writing response. 0 disables timeout.
44+
#tls: # TLS configuration
45+
# cert: /path/to/server.crt # The path to TLS certificate.
46+
# key: /path/to/server.key # The path to TLS certificate key.
47+
timeout_read: 10 # Specifies the maximum time (in seconds) for reading the request. 0 disables timeout.
48+
timeout_write: 10 # Specifies the maximum time (in seconds) for writing the response. 0 disables timeout.
4149
max_request_body_size: 1048576 # Specifies the maximum request body size. Default is 1048576.
4250
response: # Default HTTP response
4351
code: 200
@@ -50,17 +58,20 @@ proxy:
5058
port: 6379
5159
password:
5260
database: 0
61+
max_pool_size: 0 # Specifies the maximum number of connections.
62+
# Default value is 10 connections per every available CPU.
63+
5364

5465
#------------------------------------------------------------------------------
5566
# Admin API
5667
# The Admin API provides RESTful APIs for managing entities.
5768
#------------------------------------------------------------------------------
5869
admin:
5970
listen: 127.0.0.1:9601
60-
debug_endpoints: false # Whether to expose debugging and profiling endpoints.
71+
debug_endpoints: false # Whether to expose debugging and profiling endpoints (via pprof).
6172
# See https://pkg.go.dev/net/http/pprof for more information.
6273
#tls: # TLS configuration
63-
# cert: /path/to/server.crt # The Path to TLS certificate.
74+
# cert: /path/to/server.crt # The path to TLS certificate.
6475
# key: /path/to/server.key # The path to TLS certificate key.
6576

6677
#------------------------------------------------------------------------------
@@ -69,7 +80,7 @@ admin:
6980
#------------------------------------------------------------------------------
7081
status:
7182
listen: 127.0.0.1:9602
72-
debug_endpoints: true # Whether to expose debugging and profiling endpoints.
83+
debug_endpoints: true # Whether to expose debugging and profiling endpoints (via pprof).
7384
# See https://pkg.go.dev/net/http/pprof for more information.
7485

7586
#------------------------------------------------------------------------------
@@ -79,7 +90,7 @@ status:
7990
worker:
8091
enabled: true # Whether to enable the Worker.
8192
deliverer:
82-
timeout: 60000 # Sets the request timeout (in milliseconds) for delivery requests.
93+
timeout: 60000 # Specifies the request timeout (in milliseconds) for delivery requests.
8394
acl: # Access Control List (ACL) defines rules to control outbound network access.
8495
# A rule is a string of IPv4, IPv6, CIDR, hostname, or pre-configured group.
8596
# A hostname can contain a wildcard prefix (*.) represents its subdomains, and Unicode
@@ -105,12 +116,14 @@ worker:
105116
# Example of HTTPS: https://<host>:<port>
106117
#proxy_tls_cert: # Path to the client certificate file used for mTLS proxy authentication.
107118
#proxy_tls_key: # Path to the client private key file used for mTLS proxy authentication.
108-
#proxy_tls_ca_cert: # Path to the CA certificate file used to verify the HTTPS proxys certificate.
119+
#proxy_tls_ca_cert: # Path to the CA certificate file used to verify the HTTPS proxy's certificate.
109120
#proxy_tls_verify: true # Whether to verify the proxy server's TLS certificate.
110121

111-
pool:
112-
size: 10000 # pool size, default to 10000.
113-
concurrency: 0 # pool concurrency, default to 100 * CPUs
122+
pool: # Pool defines an in-memory (goroutine) pool used for event delivery.
123+
size: 10000 # Specifies the size of pool.
124+
# Defaults to 10000.
125+
concurrency: 0 # Specifies the maximum number of concurrent deliveries.
126+
# Default value is 100 per every available CPU.
114127

115128
#------------------------------------------------------------------------------
116129
# Cluster
@@ -119,37 +132,51 @@ worker:
119132
# This allows some nodes in the cluster to run as the control plane
120133
# and others to run as the data plane.
121134
#
122-
# supported values are:
135+
# Supported values:
123136
#
124137
# - `standalone`: disable cluster mode.
125138
# - `cp`: this node runs as the control plane.
126139
# It connects to the database to provide entity management.
127140
# - `dp_proxy`: this node runs as the Proxy data plane.
128141
# - `dp_worker`: this node runs as the Worker data plane.
129142

130-
anonymous_reports: true # sends anonymous data such as software version to WebhookX.
143+
anonymous_reports: true # Sends anonymous data to help improve WebhookX.
131144

132145

133146
#------------------------------------------------------------------------------
134-
# METRICS
147+
# Metrics
135148
#------------------------------------------------------------------------------
136149
metrics:
137-
attributes: # global attributes for each metric
138-
env: prod
139-
#exports: [ opentelemetry ] # list of enabled vendor exports. supported value are opentelemetry
140-
push_interval: 10 # interval(in seconds) at which metrics are sent to the OpenTelemetry Collector
150+
#attributes: # Specifies the custom attributes in key-value pair for each metric.
151+
# foo: bar
152+
#exports: # The list of enabled vendor exports. Supported value: opentelemetry.
153+
# - opentelemetry
154+
push_interval: 10 # Specifies the interval (in seconds) at which metrics are sent to the OpenTelemetry Collector
141155
opentelemetry:
142156
protocol: http/protobuf # Supported values: http/protobuf, grpc.
143-
endpoint: http://localhost:4318/v1/metrics # http/protobuf(http://localhost:4318/v1/metrics), grpc(localhost:4317)
157+
endpoint: http://localhost:4318/v1/metrics #
158+
# Example of http/protobuf:
159+
# protocol: http/protobuf
160+
# endpoint: http://localhost:4318/v1/metrics
161+
# Example of grpc:
162+
# protocol: grpc
163+
# endpoint: localhost:4317
144164

145165
#------------------------------------------------------------------------------
146-
# TRACING
166+
# Tracing
147167
#------------------------------------------------------------------------------
148168
tracing:
149169
enabled: false
150-
attributes: # global attributes for each trace
151-
env: prod
152-
sampling_rate: 1.0
170+
#attributes: # Specifies the custom attributes in key-value pair for each trace.
171+
# foo: bar
172+
sampling_rate: 1 # Specifies the trace sampling rate.
173+
# Example: `0.01`, samples 1% of traces.
153174
opentelemetry:
154-
protocol: http/protobuf # Supported value: http/protobuf, grpc.
155-
endpoint: http://localhost:4318/v1/traces # http/protobuf(http://localhost:4318/v1/traces), grpc(localhost:4317)
175+
protocol: http/protobuf # Supported values: http/protobuf, grpc.
176+
endpoint: http://localhost:4318/v1/traces #
177+
# Example of http/protobuf:
178+
# protocol: http/protobuf
179+
# endpoint: http://localhost:4318/v1/traces
180+
# Example of grpc:
181+
# protocol: grpc
182+
# endpoint: localhost:4317

config/access_log.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
)
77

88
type AccessLogConfig struct {
9-
File string `yaml:"file" json:"file" default:"/dev/stdout"`
10-
Format LogFormat `yaml:"format" json:"format" default:"text"`
9+
Enabled bool `yaml:"enabled" json:"enabled" default:"true"`
10+
Format LogFormat `yaml:"format" json:"format" default:"text"`
11+
Colored bool `yaml:"colored" json:"colored" default:"true"`
12+
File string `yaml:"file" json:"file"`
1113
}
1214

1315
func (cfg AccessLogConfig) Validate() error {
@@ -16,7 +18,3 @@ func (cfg AccessLogConfig) Validate() error {
1618
}
1719
return nil
1820
}
19-
20-
func (cfg AccessLogConfig) Enabled() bool {
21-
return cfg.File != ""
22-
}

config/config.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"github.com/creasty/defaults"
77
"github.com/webhookx-io/webhookx/pkg/envconfig"
88
"gopkg.in/yaml.v3"
9-
"io"
10-
"os"
119
"slices"
1210
)
1311

@@ -86,35 +84,23 @@ func (cfg Config) Validate() error {
8684
return nil
8785
}
8886

89-
func Init() (*Config, error) {
90-
var cfg Config
91-
if err := defaults.Set(&cfg); err != nil {
92-
return nil, err
93-
}
94-
95-
err := envconfig.Process("WEBHOOKX", &cfg)
96-
if err != nil {
97-
return nil, err
98-
}
99-
100-
return &cfg, nil
87+
type Options struct {
88+
YAML []byte
10189
}
10290

103-
func InitWithFile(filename string) (*Config, error) {
91+
func New(opts *Options) (*Config, error) {
10492
var cfg Config
105-
if err := defaults.Set(&cfg); err != nil {
106-
return nil, err
107-
}
108-
109-
f, err := os.Open(filename)
93+
err := defaults.Set(&cfg)
11094
if err != nil {
11195
return nil, err
11296
}
113-
defer f.Close()
114-
decoder := yaml.NewDecoder(f)
115-
err = decoder.Decode(&cfg)
116-
if err != nil && err != io.EOF {
117-
return nil, err
97+
98+
if opts != nil {
99+
if len(opts.YAML) > 0 {
100+
if err := yaml.Unmarshal(opts.YAML, &cfg); err != nil {
101+
return nil, err
102+
}
103+
}
118104
}
119105

120106
err = envconfig.Process("WEBHOOKX", &cfg)

0 commit comments

Comments
 (0)