componego

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2024 License: Apache-2.0 Imports: 2 Imported by: 4

README

ComponeGo Framework

Go Report Card Tests codecov Go Reference Mentioned in Awesome Go

screenshot

It is a framework for building applications based on components. These components can be used in multiple applications and are interchangeable. This framework is used solely to initialize the application and does NOT affect the main loop of your application. You can still use your favorite frameworks and libraries. We allow you to wrap them in components.

Components may depend on other components. They can be expanded or reduced based on your requirements. Components are not microservices; they are folders that contain different functionalities.

The framework has very low coupling within its code. All entities are optional.

We provide the ability to use dependency injection, configuration, and error handling. However, one of the framework's main features is that you can modify entities without changing the application code. This allows you to create mocks for any part of your application without changing the code.

If your application is divided into components (modules), it further separates your code into different services and allows you to reuse it in other applications. Of course, you don’t need to make components too small.

Documentation

Introduction: medium.com/@konstanchuk/25bfd16a97a9.

Visit our website to learn more: componego.github.io.

The documentation is up-to-date with the latest version of the framework. Please update your version to the latest.

Examples

You can find some examples here.

A typical application of this framework looks like this.

Skeleton

You can quickly create a basic application in several ways:

curl -sSL https://raw.githubusercontent.com/componego/componego/master/tools/create-basic-app.sh | sh

or

wget -O - https://raw.githubusercontent.com/componego/componego/master/tools/create-basic-app.sh | sh

On Windows, you can run the commands above with Git Bash, which comes with Git for Windows.

Contributing

We are open to improvements and suggestions. Pull requests are welcome.

License

The source code of the repository is licensed under the Apache 2.0 license. The core of the framework does not depend on other packages.

Documentation

Index

Constants

View Source
const (
	// SuccessExitCode is the exit code that the application should return
	// if there were no errors and the application executed successfully.
	SuccessExitCode int = 0
	// ErrorExitCode is the exit code that the application should return if the application fails.
	// It can also be any number that is greater than 0 and less than 256.
	ErrorExitCode int = 1
)

List of default application exit codes.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application interface {
	// ApplicationName returns the name of the application.
	ApplicationName() string
	// ApplicationAction describes the main action of the current application.
	// This function is called last when the application is fully initialized
	ApplicationAction(env Environment, options any) (int, error)
}

Application is a general interface that application should describe.

type ApplicationComponents

type ApplicationComponents interface {
	// Application belongs to the application.
	Application
	// ApplicationComponents return a list of components that the application depends on.
	// They will be sorted based on the dependent components.
	ApplicationComponents() ([]Component, error)
}

ApplicationComponents is an interface that describes the components of the application.

type ApplicationConfigInit

type ApplicationConfigInit interface {
	// Application belongs to the application.
	Application
	// ApplicationConfigInit returns configuration for all application entities.
	// This function is called only once.
	ApplicationConfigInit(appMode ApplicationMode, options any) (map[string]any, error)
}

ApplicationConfigInit is an interface that describes the process of obtaining configuration for the application.

type ApplicationDependencies

type ApplicationDependencies interface {
	// Application belongs to the application.
	Application
	// ApplicationDependencies returns a list of dependencies that the application provides.
	// This function must return an array of objects or functions that returns objects.
	ApplicationDependencies() ([]Dependency, error)
}

ApplicationDependencies is an interface that describes the dependencies of the application.

type ApplicationErrorHandler

type ApplicationErrorHandler interface {
	// Application belongs to the application.
	Application
	// ApplicationErrorHandler catches all application errors that are not handled in the main application code.
	ApplicationErrorHandler(err error, appIO ApplicationIO, appMode ApplicationMode) error
}

ApplicationErrorHandler is an interface that describes a function that catches all application errors.

type ApplicationIO

type ApplicationIO interface {
	// InputReader returns a pointer to read data into the application.
	InputReader() io.Reader
	// OutputWriter returns a pointer to output data from the application.
	OutputWriter() io.Writer
	// ErrorOutputWriter returns a pointer to output errors from the application.
	ErrorOutputWriter() io.Writer
}

ApplicationIO is an interface that describes input and output for the application environment.

type ApplicationMode

type ApplicationMode int

ApplicationMode is application mode type.

const (
	// ProductionMode is a constant for running the application in production mode.
	ProductionMode ApplicationMode = 0
	// DeveloperMode is a constant for running the application in developer mode.
	DeveloperMode ApplicationMode = 1
	// TestMode is a constant for running the application in test mode.
	TestMode ApplicationMode = 2
)

List of default application modes.

type Component

type Component interface {
	// ComponentIdentifier returns the component ID.
	// If the identifier in several components is the same, then the last component will be used.
	// This can be used to overwrite components.
	ComponentIdentifier() string
	// ComponentVersion returns the component version.
	ComponentVersion() string
}

Component is an interface that describes the component.

type ComponentComponents

type ComponentComponents interface {
	// Component belongs to the component.
	Component
	// ComponentComponents returns list of components that the component depends on.
	// They will be sorted based on the dependent components.
	ComponentComponents() ([]Component, error)
}

ComponentComponents is an interface that describes which components the current component depends on.

type ComponentDependencies

type ComponentDependencies interface {
	// Component belongs to the component.
	Component
	// ComponentDependencies returns a list of dependencies that the component provides
	// This function must return an array of objects or functions that returns objects.
	ComponentDependencies() ([]Dependency, error)
}

ComponentDependencies is an interface that describes the dependencies of the component.

type ComponentInit

type ComponentInit interface {
	// Component belongs to the component.
	Component
	// ComponentInit is called during component initialization.
	ComponentInit(env Environment) error
}

ComponentInit is an interface that describes the component initialization.

type ComponentProvider

type ComponentProvider interface {
	// Components returns a list of sorted application components.
	Components() []Component
}

ComponentProvider is an interface that describes a list of active application components. These components are sorted in order of dependencies between components.

type ComponentStop

type ComponentStop interface {
	// Component belongs to the component.
	Component
	// ComponentStop is called when the component stops.
	// You can handle previous error (return a new or old error).
	ComponentStop(env Environment, prevErr error) error
}

ComponentStop is an interface that describes stopping the component.

type ConfigProvider

type ConfigProvider interface {
	// ConfigValue returns the configuration by key.
	// The value can be modified by the Processor which validates or converts the value.
	ConfigValue(configKey string, processor Processor) (any, error)
}

ConfigProvider is an interface that describes configuration getter.

type Dependency

type Dependency any

type DependencyInvoker

type DependencyInvoker interface {
	// Invoke uses dependencies.
	// You can only pass a function as an argument.
	// If the passed function returns an error, then it will be returned.
	Invoke(function any) (any, error)
	// Populate is similar to Invoke, but it can only take objects that are a reference.
	// The passed object will be filled with dependencies.
	Populate(target any) error
	// PopulateFields fills the struct fields passed as an argument with dependencies.
	// Fields must have a special tag: `componego:"inject"`.
	// Fields without this tag are ignored.
	PopulateFields(target any) error
}

DependencyInvoker is an interface that describes the functions that invoke dependencies for the application.

type Environment

type Environment interface {
	// GetContext returns a current application context.
	GetContext() context.Context
	// SetContext sets a new application context.
	// The new context must inherit from the previous context.
	SetContext(ctx context.Context) error
	// Application returns a current application object.
	Application() Application
	// ApplicationIO returns an object for getting application input and output.
	ApplicationIO() ApplicationIO
	// ApplicationMode returns the mode in which the application is started.
	ApplicationMode() ApplicationMode
	// ConfigProvider returns an object for getting config.
	ConfigProvider() ConfigProvider
	// Components returns a sorted list of active application components.
	Components() []Component
	// DependencyInvoker returns an object to invoke dependencies.
	DependencyInvoker() DependencyInvoker
}

Environment is an interface that describes the application environment.

type Processor

type Processor interface {
	// ProcessData validates and converts data.
	// The method returns a new value or an error.
	ProcessData(value any) (any, error)
}

Processor is an interface that describes the validation or conversion of data to another format.

Directories

Path Synopsis
examples
impl
internal
libs
tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL