Skip to content

Commit 43891e9

Browse files
authored
refactor: restructure backend code organization (#247)
* refactor: restructure backend code organization - Move handlers to handler/v1beta1/ (from internal/server/v1beta1/) - Move interceptors to internal/middleware/ (from pkg/server/interceptor/) - Move store to store/ (from internal/store/) - Move telemetry to internal/telemetry/ (from pkg/telemetry/) - Extract server config to internal/config/ package - Remove pkg/ directory entirely (metrics, statsd, telemetry) - Migrate proto generation from PGV to buf protovalidate - Switch buf.gen.yaml to source_relative with proto/gen/ output - Replace manual ValidateAll() calls with connectrpc/validate interceptor - Remove envoyproxy/protoc-gen-validate dependency * refactor: flatten handler package, remove v1beta1 subdirectory Move handler files from handler/v1beta1/ to handler/ and rename package from handlersv1beta1 to handler. * refactor: move proto/gen to top-level gen directory Remove the proto/ wrapper directory — generated code now lives directly at gen/ (gen/raystack/compass/v1beta1/, gen/buf/validate/). * refactor: rename handler server.go to handler.go, update references * refactor: move config example and flatten test directory - Move config.yaml to internal/config/config.example.yaml - Flatten test/e2e_test/ to test/ - Update Makefile reference
1 parent 8873112 commit 43891e9

File tree

149 files changed

+17086
-25428
lines changed

Some content is hidden

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

149 files changed

+17086
-25428
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ generate: ## Run go generate
4949
@go generate ./...
5050

5151
config: ## Generate sample config file
52-
@cp config/config.yaml config.yaml
52+
@cp internal/config/config.example.yaml config.yaml
5353

5454
proto: ## Generate protobuf files
5555
@rm -rf proto/

buf.gen.yaml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
version: v2
2+
# Proto source lives in the proton repo (../proton).
3+
# After running `buf generate`, remove unwanted generated files:
4+
# find gen/raystack -mindepth 1 -maxdepth 1 ! -name compass -exec rm -rf {} +
5+
inputs:
6+
- directory: ../proton
7+
- module: buf.build/bufbuild/protovalidate
28
managed:
39
enabled: true
410
override:
511
- file_option: go_package
612
path: raystack/compass/v1beta1/service.proto
7-
value: github.com/raystack/compass/proto/compassv1beta1
13+
value: github.com/raystack/compass/gen/raystack/compass/v1beta1;compassv1beta1
814
plugins:
915
- remote: buf.build/protocolbuffers/go:v1.36.11
10-
out: proto
11-
opt: module=github.com/raystack/compass/proto
12-
- remote: buf.build/bufbuild/validate-go:v1.0.2
13-
out: proto
14-
opt: module=github.com/raystack/compass/proto
16+
out: gen
17+
opt:
18+
- paths=source_relative
1519
- remote: buf.build/connectrpc/go:v1.18.1
16-
out: proto
17-
opt: module=github.com/raystack/compass/proto
20+
out: gen
21+
opt:
22+
- paths=source_relative

cli/assets.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/MakeNowJust/heredoc"
88
"github.com/raystack/compass/core/namespace"
99
"github.com/raystack/compass/internal/client"
10-
compassv1beta1 "github.com/raystack/compass/proto/compassv1beta1"
10+
compassv1beta1 "github.com/raystack/compass/gen/raystack/compass/v1beta1"
1111
"github.com/raystack/salt/cli/printer"
1212
"github.com/spf13/cobra"
1313
)
@@ -187,11 +187,6 @@ func editAssetCommand(cfg *Config) *cobra.Command {
187187
return err
188188
}
189189

190-
err := reqBody.ValidateAll()
191-
if err != nil {
192-
return err
193-
}
194-
195190
clnt, err := client.Create(cmd.Context(), cfg.Client)
196191
if err != nil {
197192
return err

cli/config.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66

77
"github.com/MakeNowJust/heredoc"
88
"github.com/raystack/compass/internal/client"
9-
"github.com/raystack/compass/internal/server"
10-
esStore "github.com/raystack/compass/internal/store/elasticsearch"
11-
"github.com/raystack/compass/internal/store/postgres"
12-
"github.com/raystack/compass/pkg/metrics"
13-
"github.com/raystack/compass/pkg/telemetry"
9+
compassconfig "github.com/raystack/compass/internal/config"
10+
esStore "github.com/raystack/compass/store/elasticsearch"
11+
"github.com/raystack/compass/store/postgres"
12+
"github.com/raystack/compass/internal/telemetry"
1413
"github.com/raystack/salt/config"
1514
"github.com/spf13/cobra"
1615
"gopkg.in/yaml.v2"
@@ -77,9 +76,6 @@ type Config struct {
7776
// Log
7877
LogLevel string `yaml:"log_level" mapstructure:"log_level" default:"info"`
7978

80-
// NewRelic
81-
NewRelic metrics.NewRelicConfig `mapstructure:"newrelic"`
82-
8379
// Telemetry
8480
Telemetry telemetry.Config `mapstructure:"telemetry"`
8581

@@ -90,7 +86,7 @@ type Config struct {
9086
DB postgres.Config `mapstructure:"db"`
9187

9288
// Service
93-
Service server.Config `mapstructure:"service"`
89+
Service compassconfig.ServerConfig `mapstructure:"service"`
9490

9591
// Client
9692
Client client.Config `mapstructure:"client"`

cli/discussions.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/MakeNowJust/heredoc"
88
"github.com/raystack/compass/core/namespace"
99
"github.com/raystack/compass/internal/client"
10-
compassv1beta1 "github.com/raystack/compass/proto/compassv1beta1"
10+
compassv1beta1 "github.com/raystack/compass/gen/raystack/compass/v1beta1"
1111
"github.com/raystack/salt/cli/printer"
1212
"github.com/spf13/cobra"
1313
)
@@ -143,11 +143,6 @@ func postDiscussionCommand(cfg *Config) *cobra.Command {
143143
if err := parseFile(filePath, &reqBody); err != nil {
144144
return err
145145
}
146-
err := reqBody.ValidateAll()
147-
if err != nil {
148-
return err
149-
}
150-
151146
clnt, err := client.Create(cmd.Context(), cfg.Client)
152147
if err != nil {
153148
return err

cli/lineage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/MakeNowJust/heredoc"
77
"github.com/raystack/compass/core/namespace"
88
"github.com/raystack/compass/internal/client"
9-
compassv1beta1 "github.com/raystack/compass/proto/compassv1beta1"
9+
compassv1beta1 "github.com/raystack/compass/gen/raystack/compass/v1beta1"
1010
"github.com/raystack/salt/cli/printer"
1111
"github.com/spf13/cobra"
1212
)

cli/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/MakeNowJust/heredoc"
1010
"github.com/raystack/compass/core/namespace"
1111
"github.com/raystack/compass/internal/client"
12-
compassv1beta1 "github.com/raystack/compass/proto/compassv1beta1"
12+
compassv1beta1 "github.com/raystack/compass/gen/raystack/compass/v1beta1"
1313
"github.com/raystack/salt/cli/printer"
1414
"github.com/spf13/cobra"
1515
)

cli/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/MakeNowJust/heredoc"
77
"github.com/raystack/compass/core/namespace"
88
"github.com/raystack/compass/internal/client"
9-
compassv1beta1 "github.com/raystack/compass/proto/compassv1beta1"
9+
compassv1beta1 "github.com/raystack/compass/gen/raystack/compass/v1beta1"
1010
"github.com/raystack/salt/cli/printer"
1111
"github.com/spf13/cobra"
1212
)

cli/server.go

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import (
1010
"syscall"
1111

1212
"github.com/MakeNowJust/heredoc"
13-
"github.com/newrelic/go-agent/v3/newrelic"
1413
"github.com/raystack/compass/core/asset"
1514
"github.com/raystack/compass/core/discussion"
1615
"github.com/raystack/compass/core/star"
1716
"github.com/raystack/compass/core/tag"
1817
"github.com/raystack/compass/core/user"
1918
compassserver "github.com/raystack/compass/internal/server"
20-
esStore "github.com/raystack/compass/internal/store/elasticsearch"
21-
"github.com/raystack/compass/internal/store/postgres"
22-
"github.com/raystack/compass/pkg/telemetry"
19+
esStore "github.com/raystack/compass/store/elasticsearch"
20+
"github.com/raystack/compass/store/postgres"
21+
"github.com/raystack/compass/internal/telemetry"
2322
log "github.com/raystack/salt/observability/logger"
2423
"github.com/spf13/cobra"
2524
)
@@ -81,7 +80,7 @@ func serverMigrateCommand(cfg *Config) *cobra.Command {
8180
ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
8281
defer cancel()
8382
if down {
84-
return migrateDown(cfg)
83+
return migrateDown(ctx, cfg)
8584
}
8685
return runMigrations(ctx, cfg)
8786
},
@@ -97,11 +96,6 @@ func runServer(config *Config) error {
9796
logger := initLogger(config.LogLevel)
9897
logger.Info("compass starting", "version", Version)
9998

100-
nrApp, err := initNewRelicMonitor(config, logger)
101-
if err != nil {
102-
return err
103-
}
104-
10599
otelCleanup, err := telemetry.Init(ctx, config.Telemetry, logger)
106100
if err != nil {
107101
return fmt.Errorf("failed to initialize telemetry: %w", err)
@@ -117,6 +111,13 @@ func runServer(config *Config) error {
117111
if err != nil {
118112
return err
119113
}
114+
defer func() {
115+
logger.Warn("closing db...")
116+
if err := pgClient.Close(); err != nil {
117+
logger.Error("error when closing db", "err", err)
118+
}
119+
logger.Warn("db closed...")
120+
}()
120121

121122
// init tag
122123
tagRepository, err := postgres.NewTagRepository(pgClient)
@@ -169,8 +170,6 @@ func runServer(config *Config) error {
169170
ctx,
170171
config.Service,
171172
logger,
172-
pgClient,
173-
nrApp,
174173
namespaceService,
175174
assetService,
176175
starService,
@@ -212,41 +211,20 @@ func initPostgres(logger log.Logger, config *Config) (*postgres.Client, error) {
212211
return pgClient, nil
213212
}
214213

215-
func initNewRelicMonitor(config *Config, logger log.Logger) (*newrelic.Application, error) {
216-
if !config.NewRelic.Enabled {
217-
logger.Info("New Relic monitoring is disabled.")
218-
return nil, nil
219-
}
220-
app, err := newrelic.NewApplication(
221-
newrelic.ConfigAppName(config.NewRelic.AppName),
222-
newrelic.ConfigLicense(config.NewRelic.LicenseKey),
223-
)
224-
if err != nil {
225-
return nil, fmt.Errorf("unable to create New Relic Application: %w", err)
226-
}
227-
logger.Info("New Relic monitoring is enabled for", "config", config.NewRelic.AppName)
228-
229-
return app, nil
230-
}
231-
232214
func runMigrations(ctx context.Context, config *Config) error {
233-
fmt.Println("Preparing migration...")
234-
235215
logger := initLogger(config.LogLevel)
236216
logger.Info("compass is migrating", "version", Version)
237217

238-
logger.Info("Migrating Postgres & ElasticSearch...")
239218
esClient, err := initElasticsearch(logger, config.Elasticsearch)
240219
if err != nil {
241220
return err
242221
}
243222

244-
logger.Info("Initiating Postgres client...")
245-
pgClient, err := postgres.NewClient(config.DB)
223+
pgClient, err := initPostgres(logger, config)
246224
if err != nil {
247-
logger.Error("failed to prepare migration", "error", err)
248225
return err
249226
}
227+
defer pgClient.Close()
250228

251229
ver, err := pgClient.Migrate(config.DB)
252230
if err != nil {
@@ -265,27 +243,24 @@ func runMigrations(ctx context.Context, config *Config) error {
265243
} else if err != nil {
266244
return fmt.Errorf("problem with migration %w", err)
267245
}
268-
logger.Info(fmt.Sprintf("Migration finished. Version: %d", ver))
246+
logger.Info("migration finished", "version", ver)
269247
return nil
270248
}
271249

272-
func migrateDown(config *Config) error {
273-
fmt.Println("Preparing rolling back one step of migration...")
274-
250+
func migrateDown(ctx context.Context, config *Config) error {
275251
logger := initLogger(config.LogLevel)
276-
logger.Info("compass is migrating", "version", Version)
252+
logger.Info("compass is rolling back migration", "version", Version)
277253

278-
logger.Info("Initiating Postgres client...")
279-
pgClient, err := postgres.NewClient(config.DB)
254+
pgClient, err := initPostgres(logger, config)
280255
if err != nil {
281-
logger.Error("failed to prepare migration", "error", err)
282256
return err
283257
}
258+
defer pgClient.Close()
284259

285260
ver, err := pgClient.MigrateDown(config.DB)
286261
if err != nil {
287262
return fmt.Errorf("problem with migration %w", err)
288263
}
289-
logger.Info(fmt.Sprintf("Migration finished. Version: %d", ver))
264+
logger.Info("migration finished", "version", ver)
290265
return nil
291266
}

docs/docs/configuration.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,11 @@ app:
237237
config:
238238
COMPASS_SERVICE_PORT: 8080
239239
# COMPASS_SERVICE_HOST: 0.0.0.0
240-
# COMPASS_STATSD_ENABLED: false
241-
# COMPASS_STATSD_PREFIX: compass
242-
# COMPASS_NEWRELIC_ENABLED: false
243-
# COMPASS_NEWRELIC_APPNAME: compass
244240
# COMPASS_LOG_LEVEL: info
245241

246242
secretConfig:
247243
{}
248244
# COMPASS_ELASTICSEARCH_BROKERS: ~
249-
# COMPASS_STATSD_ADDRESS: ~
250-
# COMPASS_NEWRELIC_LICENSEKEY: ~
251245
# COMPASS_DB_HOST: ~
252246
# COMPASS_DB_PORT: 5432
253247
# COMPASS_DB_NAME: ~
@@ -284,8 +278,6 @@ If everything goes ok, you should see something like this:
284278
```bash
285279
time="2022-04-27T09:18:08Z" level=info msg="compass starting" version=v0.2.0
286280
time="2022-04-27T09:18:08Z" level=info msg="connected to elasticsearch cluster" config="\"docker-cluster\" (server version 7.6.1)"
287-
time="2022-04-27T09:18:08Z" level=info msg="New Relic monitoring is disabled."
288-
time="2022-04-27T09:18:08Z" level=info msg="statsd metrics monitoring is disabled."
289281
time="2022-04-27T09:18:08Z" level=info msg="connected to postgres server" host=postgres port=5432
290282
time="2022-04-27T09:18:08Z" level=info msg="server started"
291283
```

0 commit comments

Comments
 (0)