compogo

package module
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: Apache-2.0 Imports: 17 Imported by: 4

README ΒΆ

Compogo βš›οΈ

Compogo β€” это лСгковСсный ΠΈ ΠΏΡ€Π°Π³ΠΌΠ°Ρ‚ΠΈΡ‡Π½Ρ‹ΠΉ способ сборки Go-сСрвисов ΠΈΠ· Π΄Π΅ΠΊΠ»Π°Ρ€Π°Ρ‚ΠΈΠ²Π½Ρ‹Ρ… ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ΠΎΠ². Π—Π°Π±ΡƒΠ΄ΡŒΡ‚Π΅ ΠΏΡ€ΠΎ многостраничный main.go β€” просто ΠΎΠΏΠΈΡˆΠΈΡ‚Π΅, ΠΎΡ‚ Ρ‡Π΅Π³ΠΎ зависит ваш ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π΄Π΅Π»Π°Π΅Ρ‚ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ этапС ΠΆΠΈΠ·Π½ΠΈ, ΠΈ Π΄ΠΎΠ²Π΅Ρ€ΡŒΡ‚Π΅ ядру сборку всСго прилоТСния Π² Ρ†Π΅Π»ΡŒΠ½ΡƒΡŽ, Ρ€Π°Π±ΠΎΡ‚Π°ΡŽΡ‰ΡƒΡŽ систСму.

πŸš€ Быстрый старт

package main

import (
    "github.com/Compogo/compogo"
    "github.com/Compogo/compogo/logger"
    "github.com/Compogo/compogo/container"
    "github.com/Compogo/compogo/configurator"
    "github.com/Compogo/myapp/http"
    "github.com/Compogo/myapp/service"
)

func main() {
    app := compogo.NewApp("myapp",
        compogo.WithOsSignalCloser(),           // graceful shutdown ΠΏΠΎ Ctrl+C
        compogo.WithLogger(logger.NewSlog(), logger.Component),
        compogo.WithContainer(container.NewDig(), container.Component),
        compogo.WithConfigurator(configurator.NewViper(), configurator.Component),
        compogo.WithComponents(
            http.Component,
            service.Component,
        ),
    )

    if err := app.Serve(); err != nil {
        panic(err)
    }
}

✨ ΠšΠ»ΡŽΡ‡Π΅Π²Ρ‹Π΅ возмоТности

🧩 Π”Π΅ΠΊΠ»Π°Ρ€Π°Ρ‚ΠΈΠ²Π½Ρ‹Π΅ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹

ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚ сам описываСт свои зависимости ΠΈ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Π½Π° ΠΊΠ°ΠΆΠ΄ΠΎΠΌ этапС ΠΆΠΈΠ·Π½ΠΈ:

var Component = &component.Component{
    Dependencies: app.Components{logger.Component, config.Component},
    
    Init: func(c container.Container) error {
        return c.Provide(NewMyService)
    },
    
    PostRun: func(c container.Container) error {
        return c.Invoke(func(s *MyService, r runner.Runner) {
            r.RunProcess(s)
        })
    },
}
πŸ“‹ 10-ΡˆΠ°Π³ΠΎΠ²Ρ‹ΠΉ ΠΆΠΈΠ·Π½Π΅Π½Π½Ρ‹ΠΉ Ρ†ΠΈΠΊΠ»

Init β†’ PreRun β†’ Run β†’ PostRun β†’ PreWait β†’ Wait β†’ PostWait β†’ PreStop β†’ Stop β†’ PostStop

⏱️ Π’Π°ΠΉΠΌΠ°ΡƒΡ‚Ρ‹ Π½Π° ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ шаг

ΠšΠ°ΠΆΠ΄Ρ‹ΠΉ этап ΠΈΠΌΠ΅Π΅Ρ‚ свой Ρ‚Π°ΠΉΠΌΠ°ΡƒΡ‚.

πŸ”Œ Graceful shutdown ΠΈΠ· ΠΊΠΎΡ€ΠΎΠ±ΠΊΠΈ

Одна строка β€” ΠΈ вашС ΠΏΡ€ΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½ΠΎ Π·Π°Π²Π΅Ρ€ΡˆΠ°Π΅Ρ‚ΡΡ ΠΏΠΎ SIGINT/SIGTERM.

πŸ“¦ Установка
go get github.com/Compogo/compogo
πŸ“„ ЛицСнзия

Apache 2.0 β€” смотритС Ρ„Π°ΠΉΠ» LICENSE.

Compogo β€” создан с ❀️ для Go-Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ², ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ цСнят порядок ΠΈ ΠΏΡ€Π΅Π΄ΡΠΊΠ°Π·ΡƒΠ΅ΠΌΠΎΡΡ‚ΡŒ.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	ClusterFieldName       = "cluster"
	NamespaceFieldName     = "namespace"
	ContainerIdFieldName   = "container.id"
	ContainerNameFieldName = "container.name"
	HostnameFieldName      = "hostname"

	InitDurationFieldName          = "compogo.duration.init"
	ConfigurationDurationFieldName = "compogo.duration.configuration"

	PreExecuteDurationFieldName  = "compogo.duration.execute.pre"
	ExecuteDurationFieldName     = "compogo.duration.execute.run"
	PostExecuteDurationFieldName = "compogo.duration.execute.post"

	PreWaitDurationFieldName  = "compogo.duration.wait.pre"
	PostWaitDurationFieldName = "compogo.duration.wait.post"

	PreStopDurationFieldName  = "compogo.duration.stop.pre"
	StopDurationFieldName     = "compogo.duration.stop.stop"
	PostStopDurationFieldName = "compogo.duration.stop.post"

	ClusterDefault       = "unknown-cluster"
	NamespaceDefault     = "unknown-namespace"
	ContainerIdDefault   = "unknown-container_id"
	ContainerNameDefault = "unknown-container_name"
	HostnameDefault      = "unknown-hostname"

	InitDurationDefault          = 100 * time.Millisecond
	ConfigurationDurationDefault = 100 * time.Millisecond

	PreRunDurationDefault  = 100 * time.Millisecond
	RunDurationDefault     = 100 * time.Millisecond
	PostRunDurationDefault = 100 * time.Millisecond

	PreWaitDurationDefault  = 100 * time.Millisecond
	PostWaitDurationDefault = 100 * time.Millisecond

	PreStopDurationDefault  = 100 * time.Millisecond
	StopDurationDefault     = 100 * time.Millisecond
	PostStopDurationDefault = 100 * time.Millisecond
)
View Source
const (
	MetricNamePrefix       = "compogo_"
	MetricAppNameFieldName = "app"
)

Variables ΒΆ

View Source
var (
	ContainerUndefinedError    = errors.New("container is undefined")
	ConfiguratorUndefinedError = errors.New("configurator is undefined")
	CloserUndefinedError       = errors.New("closer is undefined")
	LoggerUndefinedError       = errors.New("logger is undefined")
	AppIsRunningError          = errors.New("app is running")
	ComponentStepTimeoutError  = errors.New("timeout execution of the component at the step")
)

Functions ΒΆ

This section is empty.

Types ΒΆ

type App ΒΆ

type App struct {
	// contains filtered or unexported fields
}

App represents the main application container. It manages component lifecycle, dependencies, and graceful shutdown.

func NewApp ΒΆ

func NewApp(name string, options ...Option) *App

NewApp creates a new application instance with the given name and options. The config component is automatically added to ensure basic configuration is always present.

func (*App) AddComponents ΒΆ

func (app *App) AddComponents(components ...*component.Component) (err error)

AddComponents registers one or more components and their dependencies in the application. Components are initialized immediately and cannot be added while the app is running. Returns an error if validation fails or the app is already running.

func (*App) BindFlags ΒΆ

func (app *App) BindFlags(flagSet flag.FlagSet) (err error)

BindFlags binds command-line flags for all registered components. Must be called before Serve() and cannot be called while the app is running.

func (*App) Clone ΒΆ

func (app *App) Clone(name string) *App

Clone creates a child application that inherits all core services (config, container, configurator, closer) but has its own logger and component set. Useful for creating isolated sub-applications (e.g., for testing or modules).

func (*App) IsRunning ΒΆ

func (app *App) IsRunning() bool

IsRunning returns true if the application is in the running state.

func (*App) Serve ΒΆ

func (app *App) Serve() (err error)

Serve starts the application and runs all components through their lifecycle: 1. Sequential execution of PreExecute, Execute, PostExecute, PreWait 2. Concurrent execution of all Wait components 3. Sequential execution of PostWait 4. Wait for shutdown signal or first error 5. Sequential execution of PreStop, Stop, PostStop 6. Wait for all goroutines to finish

type CloserOsSignal ΒΆ

type CloserOsSignal struct {
	// contains filtered or unexported fields
}

func NewCloserOsSignal ΒΆ

func NewCloserOsSignal() *CloserOsSignal

func (*CloserOsSignal) Close ΒΆ

func (closer *CloserOsSignal) Close() error

func (*CloserOsSignal) GetContext ΒΆ

func (closer *CloserOsSignal) GetContext() context.Context

func (*CloserOsSignal) IsClosed ΒΆ

func (closer *CloserOsSignal) IsClosed() bool

func (*CloserOsSignal) Serve ΒΆ

func (closer *CloserOsSignal) Serve()

type Config ΒΆ

type Config struct {
	Name string
	PID  uint64

	// k8s
	Cluster       string
	Namespace     string
	ContainerId   string
	ContainerName string
	Hostname      string

	// Duration
	InitDuration          time.Duration
	ConfigurationDuration time.Duration

	ExecuteRunDuration  time.Duration
	ExecuteDuration     time.Duration
	PostExecuteDuration time.Duration

	PreWaitDuration  time.Duration
	PostWaitDuration time.Duration

	PreStopDuration  time.Duration
	StopDuration     time.Duration
	PostStopDuration time.Duration
}

func Configuration ΒΆ

func Configuration(config *Config, configurator configurator.Configurator) *Config

func NewConfig ΒΆ

func NewConfig() *Config

type Option ΒΆ

type Option func(app *App)

Option configures the App during creation. Options are applied in the order they are provided to NewApp.

func WithCloser ΒΆ

func WithCloser(closer closer.Closer, cmp *component.Component) Option

WithCloser injects a closer implementation and its component. The closer component will participate in the application lifecycle.

func WithComponents ΒΆ

func WithComponents(components ...*component.Component) Option

WithComponents registers additional components during app creation. Panics if component registration fails (should only happen on programming errors).

func WithConfigurator ΒΆ

func WithConfigurator(configurator configurator.Configurator, cmp *component.Component) Option

WithConfigurator injects a configurator implementation and its component. The configurator component will participate in the application lifecycle.

func WithContainer ΒΆ

func WithContainer(container container.Container, cmp *component.Component) Option

WithContainer injects a DI container implementation and its component. The container component will participate in the application lifecycle.

func WithLogger ΒΆ

func WithLogger(logger logger.Logger, cmp *component.Component) Option

WithLogger injects a logger implementation and its component into the app. The logger component will participate in the application lifecycle.

func WithOsSignalCloser ΒΆ

func WithOsSignalCloser() Option

WithOsSignalCloser returns an Option that adds a default closer listening for OS signals. The closer handles SIGINT, SIGTERM, and SIGABRT, providing graceful shutdown out of the box. Perfect for standard applications - just add this option and your app stops gracefully on Ctrl+C.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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