-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: restructure application context and service initialization
This commit introduces several key changes: - Created a new AppContext package to centralize application initialization - Simplified main service entry points in cmd/samplesvc and cmd/migrate - Moved configuration and initialization logic into a reusable AppContext - Updated dependency versions (ajan, goose, kin-openapi) - Removed legacy main package implementation
- Loading branch information
Showing
10 changed files
with
217 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/eser/ajan" | ||
"github.com/eser/go-service/pkg/samplesvc/adapters/appcontext" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
var ( | ||
ErrCommandRequired = errors.New("command is required") | ||
ErrAppContextNotInitialized = errors.New("app context is not initialized") | ||
ErrDatabaseNotInitialized = errors.New("database is not initialized") | ||
ErrDatabaseNotSqlDb = errors.New("database is not an instance of *sql.DB") | ||
ErrFailedToRunGoose = errors.New("failed to run goose") | ||
) | ||
|
||
type AppConfig ajan.BaseConfig | ||
|
||
func run(ctx context.Context, args []string) error { | ||
if len(args) < 1 { | ||
return ErrCommandRequired | ||
} | ||
|
||
appContext, err := appcontext.NewAppContext(ctx) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", ErrAppContextNotInitialized, err) | ||
} | ||
|
||
defaultSql := appContext.Data.GetDefaultSql() | ||
if defaultSql == nil { | ||
return ErrDatabaseNotInitialized | ||
} | ||
|
||
db, dbOk := defaultSql.GetConnection().(*sql.DB) //nolint:varnamelen | ||
if !dbOk { | ||
return ErrDatabaseNotSqlDb | ||
} | ||
|
||
command := args[0] | ||
rest := args[1:] | ||
|
||
err = goose.RunContext(ctx, command, db, "./etc/data/migrations", rest...) | ||
if err != nil { | ||
return fmt.Errorf("%w: %w", ErrFailedToRunGoose, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
err := run(ctx, os.Args[1:]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package appcontext | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/eser/ajan/configfx" | ||
"github.com/eser/ajan/datafx" | ||
"github.com/eser/ajan/logfx" | ||
"github.com/eser/ajan/metricsfx" | ||
) | ||
|
||
var ErrInitFailed = errors.New("failed to initialize app context") | ||
|
||
type AppContext struct { | ||
Config *AppConfig | ||
Logger *slog.Logger | ||
Metrics *metricsfx.MetricsProvider | ||
Data *datafx.Registry | ||
} | ||
|
||
func NewAppContext(ctx context.Context) (*AppContext, error) { | ||
appContext := &AppContext{} //nolint:exhaustruct | ||
|
||
// config | ||
cl := configfx.NewConfigManager() | ||
|
||
appContext.Config = &AppConfig{} //nolint:exhaustruct | ||
|
||
err := cl.LoadDefaults(appContext.Config) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %w", ErrInitFailed, err) | ||
} | ||
|
||
// logger | ||
appContext.Logger, err = logfx.NewLoggerAsDefault(os.Stdout, &appContext.Config.Log) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %w", ErrInitFailed, err) | ||
} | ||
|
||
// metrics | ||
appContext.Metrics = metricsfx.NewMetricsProvider() | ||
|
||
err = appContext.Metrics.RegisterNativeCollectors() | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %w", ErrInitFailed, err) | ||
} | ||
|
||
// data | ||
appContext.Data = datafx.NewRegistry(appContext.Logger) | ||
|
||
err = appContext.Data.LoadFromConfig(ctx, &appContext.Config.Data) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: %w", ErrInitFailed, err) | ||
} | ||
|
||
return appContext, nil | ||
} |
3 changes: 1 addition & 2 deletions
3
pkg/samplesvc/adapters/config/config.go → pkg/samplesvc/adapters/appcontext/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package config | ||
package appcontext | ||
|
||
import ( | ||
"github.com/eser/ajan" | ||
) | ||
|
||
type AppConfig struct { | ||
AppName string `conf:"NAME" default:"samplesvc"` | ||
ajan.BaseConfig | ||
} |
Oops, something went wrong.