Skip to content

Commit dcbb971

Browse files
committed
refactor
1 parent 2cebb7a commit dcbb971

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+863
-551
lines changed

admin/admin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import (
77
"os"
88
"time"
99

10-
"github.com/webhookx-io/webhookx/config"
10+
"github.com/webhookx-io/webhookx/config/modules"
1111
"go.uber.org/zap"
1212
)
1313

1414
// Admin is an HTTP Server
1515
type Admin struct {
16-
cfg *config.AdminConfig
16+
cfg *modules.AdminConfig
1717
s *http.Server
1818
log *zap.SugaredLogger
1919
}
2020

21-
func NewAdmin(cfg config.AdminConfig, handler http.Handler) *Admin {
21+
func NewAdmin(cfg modules.AdminConfig, handler http.Handler) *Admin {
2222
s := &http.Server{
2323
Handler: handler,
2424
Addr: cfg.Listen,

app/app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func New(cfg *config.Config) (*Application, error) {
9393

9494
func (app *Application) initialize() error {
9595
cfg := app.cfg
96-
cfg.OverrideByRole(cfg.Role)
9796

9897
log, err := log.NewZapLogger(&cfg.Log)
9998
if err != nil {

cmd/db.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"context"
45
"errors"
56

67
"github.com/golang-migrate/migrate/v4"
78
"github.com/spf13/cobra"
9+
"github.com/webhookx-io/webhookx/config"
810
"github.com/webhookx-io/webhookx/db"
911
"github.com/webhookx-io/webhookx/db/migrator"
1012
)
@@ -25,6 +27,7 @@ func newDatabaseResetCmd() *cobra.Command {
2527
return errors.New("canceled")
2628
}
2729
}
30+
cfg := cmd.Context().Value("config").(*config.Config)
2831
db, err := db.NewSqlDB(cfg.Database)
2932
if err != nil {
3033
return err
@@ -47,11 +50,18 @@ func newDatabaseResetCmd() *cobra.Command {
4750
}
4851

4952
func newDatabaseCmd() *cobra.Command {
50-
5153
database := &cobra.Command{
5254
Use: "db",
5355
Short: "Database commands",
5456
Long: ``,
57+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
58+
cfg, err := initConfig(configurationFile)
59+
if err != nil {
60+
return err
61+
}
62+
cmd.SetContext(context.WithValue(cmd.Context(), "config", cfg))
63+
return nil
64+
},
5565
}
5666

5767
database.PersistentFlags().StringVarP(&configurationFile, "config", "", "", "The configuration filename")
@@ -62,6 +72,7 @@ func newDatabaseCmd() *cobra.Command {
6272
Short: "Print the migration status",
6373
Long: ``,
6474
RunE: func(cmd *cobra.Command, args []string) error {
75+
cfg := cmd.Context().Value("config").(*config.Config)
6576
db, err := db.NewSqlDB(cfg.Database)
6677
if err != nil {
6778
return err
@@ -81,6 +92,7 @@ func newDatabaseCmd() *cobra.Command {
8192
Short: "Run any new migrations",
8293
Long: ``,
8394
RunE: func(cmd *cobra.Command, args []string) error {
95+
cfg := cmd.Context().Value("config").(*config.Config)
8496
db, err := db.NewSqlDB(cfg.Database)
8597
if err != nil {
8698
return err

cmd/root.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ var (
1414
var (
1515
configurationFile string
1616
verbose bool
17-
cfg *config.Config
1817
)
1918

20-
func initConfig() {
21-
var err error
22-
23-
var options config.Options
24-
if configurationFile != "" {
25-
buf, err := os.ReadFile(configurationFile)
26-
cobra.CheckErr(err)
27-
options.YAML = buf
19+
func initConfig(filename string) (*config.Config, error) {
20+
cfg := config.New()
21+
err := config.Load(filename, cfg)
22+
if err != nil {
23+
return nil, err
2824
}
2925

30-
cfg, err = config.New(&options)
31-
cobra.CheckErr(err)
32-
3326
err = cfg.Validate()
34-
cobra.CheckErr(err)
27+
if err != nil {
28+
return nil, err
29+
}
30+
return cfg, nil
3531
}
3632

3733
func NewRootCmd() *cobra.Command {
@@ -41,7 +37,6 @@ func NewRootCmd() *cobra.Command {
4137
Long: ``,
4238
SilenceUsage: true,
4339
}
44-
cobra.OnInitialize(initConfig)
4540

4641
cmd.SetOut(os.Stdout)
4742
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Verbose logging.")

cmd/start.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func newStartCmd() *cobra.Command {
1616
Short: "Start server",
1717
Long: ``,
1818
RunE: func(cmd *cobra.Command, args []string) error {
19+
cfg, err := initConfig(configurationFile)
20+
if err != nil {
21+
return err
22+
}
23+
1924
app, err := app.New(cfg)
2025
if err != nil {
2126
return err

config.yml

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,48 @@ tracing:
183183

184184

185185
#------------------------------------------------------------------------------
186-
# Secret (remote secret)
187-
# reference: {secret://<provider>/<name>[?<properties>][#/jsonpointer]}
188-
# Example: {secret://aws/path/to/secret#/password?ttl=1}
186+
# Secret Reference (External Secret Reference)
187+
# Secret Reference allows fetching values from external secret providers.
188+
#
189+
# Syntax:
190+
# {secret://<provider>/<name>[.<jsonpath>][?<parameters>]}
191+
#
192+
# Components:
193+
# - <provider> The provider name (e.g.`aws`, `vault`).
194+
# - <name> The secret name.
195+
# - <jsonpath> A optional JSON Path to extract value from a JSON.
196+
# JSON Path is a series of keys separated by a `.` character.
197+
# Examples: `database.username`, `credentials.1.username`.
198+
# - <parameters> The optional parameters.
199+
#
200+
# Examples:
201+
# {secret://aws/path/to/mysecret}
202+
# {secret://aws/path/to/mysecret.password}
189203
#------------------------------------------------------------------------------
190204
secret:
191205
providers: # Specifies enabled providers.
192-
- '@default'
193-
- '@all'
194-
ttl: 10s
195-
aws: # AWS SecretManager provider
196-
region: us-west-1
197-
url: http://localhost:4566 # aws_url?
198-
199-
vault: # HashiCorp Vault provider
200-
address: http://localhost:8200
201-
mount_path: secret
202-
namespace:
203-
auth_method: approle
204-
authn:
206+
- '@default' # Supported values:
207+
# - `@default`: an alias of built-in providers.
208+
# - `aws`
209+
# - `vault`
210+
211+
aws: # AWS SecretsManager provider
212+
region: # AWS region.
213+
url: # Optional custom endpoint.
214+
# If unset, uses AWS default endpoint resolution.
215+
216+
vault: # HashiCorp Vault provider (KV v2)
217+
address: http://127.0.0.1:8200 # Vault server address.
218+
mount_path: secret # The mount path for KV secrets engine.
219+
namespace: # Vault namespace (for Vault Enterprise).
220+
auth_method: token # Authentication method. Supported values: `token`, `approle`, `kubernetes`.
221+
authn: # Authentication configuration.
205222
token:
206-
token:
223+
token: # The token used to making requests to Vault.
207224
approle:
208-
role_id:
209-
secret_id:
210-
response_wrapping: false
225+
role_id: # RoleID used for login.
226+
secret_id: # SecretID used for login.
227+
response_wrapping: false # Whether to use response-wrapping. Defaults to false.
211228
kubernetes:
212-
role:
213-
token_path:
229+
role: # Vault role bound to the Kubernetes service account.
230+
token_path: # Path to JWT token file.

0 commit comments

Comments
 (0)