Fabriktor Infrastructure Package 📦



Overview
This repository, gitlab.com/fabriktor/pkg, contains the infrastructure package for Fabriktor's internal systems. It is designed exclusively for Fabriktor's operational needs and is not intended for public usage. Use outside of Fabriktor's infrastructure is at your own risk, as its functionality and structure may change without notice.
The package provides utilities, middleware, and configurations to support Fabriktor's backend infrastructure, integrating with internal services such as MongoDB, Redis, and Google Cloud Run.
Refer to the Go Reference for detailed API documentation and examples.
Package Structure
This Go package follows a modular and extensible design, making it easy to scale and adapt.
Folder Structure
Each package can contain, at most, the following five files, though not all packages necessarily include all of them.
For example, if a package is not related to any specific structure, it may omit the {package_name}.go file. Here's a breakdown of the potential contents of a package:
- client.go: Defines an interface for core methods.
- tool.go: Provides a
Tool struct, the main entry point for using package methods.
- option.go: Contains functional options.
- {package_name}.go: The core file, named after the package, defining the main struct and its logic. This struct is a composed structure embedding
{package_name}Meta and {package_name}Comp.
- util.go: Contains utility functions, helper structs, error types, and other supporting elements.
Each package may include any combination of these files, depending on the specific needs of the package.
Details
- Client: Abstracts package logic in an interface.
- Tool: The main access point, exposing operations through the client.
- Functional Options: Used to set fields within {package_name}Meta and {package_name}Comp. If not provided, these fields will fall back to default values.
- Core Struct: The main structure (named after the package) is composed of two embedded structs:
- {package_name}Meta: Contains metadata fields such as
CreatedAt. These fields must not be sent from the frontend and are set using functional options.
- {package_name}Comp: Contains all implicit, computed fields that are populated by the backend. These fields must not be sent from the frontend and are set using functional options.
- The main struct contains all required fields that must be sent from the frontend.
Functional Options
Functional options allow dynamic modification of computed and metadata fields without changing the core struct.
- Options can be set in two ways:
- At the time of struct initialization in the constructor.
- Later, using the
WithOpts(opts ...Option) method.
- Options only affect fields within
{package_name}Meta and {package_name}Comp. Required fields in the main struct are not modified through options.
- If functional options are omitted during construction, default values will be assigned automatically.
- If the struct is initialized by decoding a JSON payload, all fields in
{package_name}Meta and {package_name}Comp will remain unset. To populate them, WithDefault() or WithOpts(opts ...Option) must be called after decoding, ensuring these fields are properly initialized when the constructor is not used.
To call methods within a package, an instance of Tool must first be created using NewTool[C any](c C, opts ...ToolOption[C]) *Tool[C].
The Tool acts as the primary access point for invoking package functionalities. Once instantiated, it provides direct access to the available methods.
Special Packages
config
- Holds constants and global variables used throughout the project.
- Standardizes configurations across packages.
- Reduces hardcoded values in different files.
log
- Used in all packages for structured logging.
- Provides
Wrap(ctx context.Context, fn func() (any, error)) error for logging function execution.
- Handles error logging, execution time tracking, and monitoring.
- Eliminates duplicated logging logic.
- Defines the generic
Tool struct, embedded in all packages.
- Acts as a base that all tools inherit, reducing code duplication.
- Uses Go generics (
Tool[C any]) for flexibility across different clients.
License
This project is licensed under the MIT License.