-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update CI setup, kebab-case rule, and refactor modules
- Refactored `ci.yml` to update go and python setup steps. - Modified pre-commit to exclude additional README files. - Introduced new documentation for `configfx` and `logfx`. - Renamed various `mod.go` to `mod-fx.go` and updated related functions. - Improved `ConfigLoader` and added methods `FromEnvFileDirect`, `FromJsonFileDirect`. - Updated `logfx` with new logging configuration and fx integration. - Optimized result handling by commenting out unused payload attributes.
- Loading branch information
Showing
16 changed files
with
316 additions
and
61 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
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,123 @@ | ||
# bliss/configfx | ||
|
||
## Overview | ||
|
||
The **configfx** package provides a flexible and powerful configuration loader for Go applications. It supports loading configuration from various sources, including environment files, JSON files, and system environment variables. The package is designed to work seamlessly with the `go.uber.org/fx` framework. | ||
|
||
The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests. | ||
|
||
|
||
## Fx | ||
|
||
The `configfx` package provides an `FxModule` that can be used to integrate with the `fx` framework. | ||
|
||
```go | ||
import ( | ||
... | ||
"github.com/eser/go-service/pkg/bliss/configfx" | ||
"go.uber.org/fx" | ||
... | ||
) | ||
|
||
app := fx.New( | ||
configfx.FxModule, // registers configfx.ConfigLoader | ||
... | ||
) | ||
|
||
app.Run() | ||
``` | ||
|
||
|
||
## API | ||
|
||
### ConfigLoader interface | ||
Defines methods for loading configuration. | ||
|
||
``` | ||
type ConfigLoader interface { | ||
LoadMeta(i any) (ConfigItemMeta, error) | ||
LoadMap(resources ...ConfigResource) (*map[string]any, error) | ||
Load(i any, resources ...ConfigResource) error | ||
FromEnvFileDirect(filename string) ConfigResource | ||
FromEnvFile(filename string) ConfigResource | ||
FromSystemEnv() ConfigResource | ||
FromJsonFileDirect(filename string) ConfigResource | ||
FromJsonFile(filename string) ConfigResource | ||
} | ||
``` | ||
|
||
|
||
### New function | ||
|
||
Creates a new `slog.Logger` object based on the provided configuration. | ||
|
||
```go | ||
// func NewLogger(config *Config) (*slog.Logger, error) | ||
|
||
logger, err := logfx.NewLogger(config) | ||
``` | ||
|
||
|
||
### NewLoggerAsDefault function | ||
|
||
Creates a new `slog.Logger` object based on the provided configuration and makes it default slog instance. | ||
|
||
```go | ||
// func NewLoggerAsDefault(config *Config) (*slog.Logger, error) | ||
|
||
logger, err := logfx.NewLoggerAsDefault(config) | ||
``` | ||
|
||
|
||
### Load function | ||
|
||
The `Load` method loads configuration from multiple resources. | ||
|
||
Example: | ||
```go | ||
type AppConfig struct { | ||
AppName string `conf:"NAME" default:"go-service"` | ||
|
||
Postgres struct { | ||
Dsn string `conf:"DSN" default:"postgres://localhost:5432"` | ||
} `conf:"POSTGRES"` | ||
} | ||
|
||
func loadConfig() (*AppConfig, error) { | ||
conf := &AppConfig{} | ||
|
||
err := cl.Load( | ||
conf, | ||
// load order: | ||
cl.FromJsonFile("config.json"), // - attempts to read from config.json, | ||
// config.local.json, | ||
// config.[env].json, | ||
// config.[env].local.json | ||
cl.FromEnvFile(".env"), // - attempts to read from .env | ||
// .env.local | ||
// .env.[env] | ||
// .env.[env].local | ||
cl.FromSystemEnv(), // - attempts to read from system environment variables | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load config: %w", err) | ||
} | ||
|
||
return conf, nil | ||
} | ||
|
||
func main() { | ||
appConfig, err := loadConfig() | ||
if err != nil { | ||
log.Fatalf("Error loading config: %v", err) | ||
|
||
return | ||
} | ||
|
||
// Searches JSON files first, then checks the POSTGRES__DSN among environment variables. | ||
// If the config variable is not specified, it falls back to the default value "postgres://localhost:5432". | ||
fmt.Println(appConfig.Postgres.Dsn) | ||
} | ||
``` |
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
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,87 @@ | ||
# bliss/logfx | ||
|
||
## Overview | ||
|
||
The **logfx** package is a configurable logging solution leverages the `log/slog` of the standard library for structured logging. It includes pretty-printing options and a fx module for the `go.uber.org/fx` framework. The package also has extensive tests to ensure reliability and correctness, covering configuration parsing, handler behavior and the custom error formatting logic. | ||
|
||
The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests. | ||
|
||
|
||
## Configuration | ||
|
||
Configuration struct for the logger: | ||
|
||
``` | ||
type Config struct { | ||
Level string `conf:"LEVEL" default:"INFO"` | ||
PrettyMode bool `conf:"PRETTY" default:"true"` | ||
AddSource bool `conf:"ADD_SOURCE" default:"false"` | ||
} | ||
``` | ||
|
||
|
||
## Fx | ||
|
||
The `logfx` package provides an `FxModule` and `GetFxLogger` that can be used to integrate with the `fx` framework. | ||
|
||
```go | ||
import ( | ||
... | ||
"github.com/eser/go-service/pkg/bliss/logfx" | ||
"go.uber.org/fx" | ||
... | ||
) | ||
|
||
app := fx.New( | ||
fx.WithLogger(logfx.GetFxLogger), // handles fx lifecycle events | ||
logfx.FxModule, // registers slog.Logger | ||
... | ||
) | ||
|
||
app.Run() | ||
``` | ||
|
||
|
||
## API | ||
|
||
### New function | ||
|
||
Creates a new `slog.Logger` object based on the provided configuration. | ||
|
||
```go | ||
// func New(config *Config) (*slog.Logger, error) | ||
|
||
logger, err := logfx.New(config) | ||
``` | ||
|
||
|
||
### NewDefaultLogger function | ||
|
||
Creates a new `slog.Logger` object based on the provided configuration and makes it default slog instance. | ||
|
||
```go | ||
// func NewDefaultLogger(config *Config) (*slog.Logger, error) | ||
|
||
logger, err := logfx.NewDefaultLogger(config) | ||
``` | ||
|
||
|
||
### Colored function | ||
|
||
Returns a ANSI-colored string for terminal output. | ||
|
||
```go | ||
// func Colored(color Color, message string) string | ||
|
||
// available colors: | ||
// ColorReset ColorDimGray | ||
// ColorRed ColorLightRed | ||
// ColorGreen ColorLightGreen | ||
// ColorYellow ColorLightYellow | ||
// ColorBlue ColorLightBlue | ||
// ColorMagenta ColorLightMagenta | ||
// ColorCyan ColorLightCyan | ||
// ColorGray ColorLightGray | ||
|
||
fmt.Println(logfx.Colored(logfx.ColorRed, "test")) | ||
``` |
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,26 @@ | ||
package logfx | ||
|
||
import ( | ||
"log/slog" | ||
"os" | ||
) | ||
|
||
func NewLogger(config *Config) (*slog.Logger, error) { | ||
handler, err := NewHandler(os.Stderr, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return slog.New(handler), nil | ||
} | ||
|
||
func NewLoggerAsDefault(config *Config) (*slog.Logger, error) { | ||
logger, err := NewLogger(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
slog.SetDefault(logger) | ||
|
||
return logger, nil | ||
} |
Oops, something went wrong.