ygg

package module
v0.0.0-...-8ee6274 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 18 Imported by: 0

README

Yggdrasil

Yggdrasil the base of all life (backend services).

Usage

// Any http.Handler compatible struct
mux := http.NewServerMux()
// Create http component
http := application.NewHttpComponent(router)
// Compose new application
app := application.NewApplications(http)
// Run blocks until application exits
err :=  app.Run()
if err ! nil{
    return err
}

Structure

Each compoonent is composed of 3 functions, that will be invoked by the Application

  • Startup
  • Run
  • Close
Lifecycle
---
title: Lifecycle
---
stateDiagram-v2
    state "Application starting" as AS
    state "Application running" as AR
    state is_c_running_state <<choice>>
    state is_a_running_state <<choice>>
    
    [*] --> AS: Application.Run()
    AS --> Component
    state Component {
        [*] --> Starting: Component.Startup()
        Starting --> Running: Component.Run()
        Running --> is_c_running_state
        is_c_running_state --> Running
        is_c_running_state --> Closing: Error
        Closing --> Done: Component.Close()
    }
    AS --> AR: All components has started
    AR --> is_a_running_state
    is_a_running_state --> AR
    is_a_running_state --> Closing: Signal to shutdown
    Done --> [*]
TODO
  • Test
  • Move hardcoded strings into config
  • Document metricz and healthz components
  • Document how to create new components
  • Better documentation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Liveness

func Liveness() http.HandlerFunc

func NewCronComponent

func NewCronComponent(cron *gocron.Scheduler) *cronComponent

func NewHealthzComponent

func NewHealthzComponent(providers []Provider, config config.HealthzConfig) *healthzComponent

func NewLifecycleComponent

func NewLifecycleComponent(startups []LifeCycleFunc, run LifeCycleFunc, cleanups []LifeCycleFunc) *lifecycleComponent

func NewMetriczComponent

func NewMetriczComponent(registry *prometheus.Registry, config config.MetricszConfig) *metriczComponent

func WithHttpServer

func WithHttpServer(httpServer *http.Server) httpComponentOption

func WithListener

func WithListener(listener net.Listener) httpComponentOption

func WithServerConfig

func WithServerConfig(serverConfig *config.ServerConfig) httpComponentOption

Types

type Application

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

func NewApplications

func NewApplications(components ...Component) *Application

func (*Application) AddComponent

func (app *Application) AddComponent(c Component)

func (*Application) Run

func (app *Application) Run() error

func (Application) WithLogger

func (app Application) WithLogger(logger Logger) Application

func (Application) WithTimeout

func (app Application) WithTimeout(timeout time.Duration) Application

type Checkable

type Checkable interface {
	Healthz(ctx context.Context) error
}

Checkable Makes sure the object has the ) function

type CheckableFunc

type CheckableFunc func(ctx context.Context) error

func (CheckableFunc) Healthz

func (f CheckableFunc) Healthz(ctx context.Context) error

type Component

type Component interface {
	// Synchronous startup, container runs this one by one
	Startup() error
	// Asynchronous run,  container runs this in parrallel
	// Has to block until it's done
	// When one of componenst exits this func, container will start shutting down
	Run() error
	// Cleanup function
	Close(ctx context.Context) error
	// Retrieve hook functions
	Hooks() []Hook
}

type Error

type Error struct {
	Name    string `json:"name"`
	Message string `json:"message"`
}

Error the structure of the Error object

type GrpcComponent

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

TODO separate to separate package to not depend on grpc in main package

func NewGrpcComponent

func NewGrpcComponent(server *grpc.Server, lis net.Listener) *GrpcComponent

func (*GrpcComponent) Close

func (gc *GrpcComponent) Close(ctx context.Context) error

func (*GrpcComponent) Hooks

func (hc *GrpcComponent) Hooks() []Hook

func (*GrpcComponent) Run

func (gc *GrpcComponent) Run() error

func (*GrpcComponent) Startup

func (gc *GrpcComponent) Startup() error

type HealthzChecker

type HealthzChecker struct {
	Providers []Provider
}

HealthzChecker contains the instance

func NewHealthChecker

func NewHealthChecker(providers []Provider) *HealthzChecker

func (*HealthzChecker) Healthz

func (h *HealthzChecker) Healthz() http.HandlerFunc

returns a http.HandlerFunc for the healthz service

func (*HealthzChecker) Serve

func (h *HealthzChecker) Serve(config config.HealthzConfig) *http.Server

type HealthzResponse

type HealthzResponse struct {
	Services []Service `json:"services,omitempty"`
	Healthy  bool      `json:"healthy"`
}

Response type, we return a json object with {healthy:bool, errors:[]}

type Hook

type Hook interface {
	Type() HookType
	Run() error
}

func AfterRunHook

func AfterRunHook(f HookFunc) Hook

func AfterStartupHook

func AfterStartupHook(f HookFunc) Hook

func BeforRunHook

func BeforRunHook(f HookFunc) Hook

func BeforeCloseHook

func BeforeCloseHook(f HookFunc) Hook

func BeforeStartupHook

func BeforeStartupHook(f HookFunc) Hook

type HookFunc

type HookFunc func() error

type HookType

type HookType = uint
const (
	HookTypeBeforeStartup HookType = iota
	HookTypeAfterStartup

	HookTypeBeforeRun
	HookTypeAfterRun

	HookTypeBeforeClose
)

type HttpComponent

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

func NewHttpComponent

func NewHttpComponent(handler http.Handler, options ...httpComponentOption) *HttpComponent

func (*HttpComponent) Close

func (hc *HttpComponent) Close(ctx context.Context) error

func (*HttpComponent) GetHandler

func (hc *HttpComponent) GetHandler(t testing.TB) http.Handler

This is just for testing

func (*HttpComponent) GetRunningServer

func (hc *HttpComponent) GetRunningServer(t testing.TB) *http.Server

func (*HttpComponent) Hooks

func (hc *HttpComponent) Hooks() []Hook

func (*HttpComponent) Run

func (hc *HttpComponent) Run() error

func (*HttpComponent) Startup

func (hc *HttpComponent) Startup() error

type LifeCycleFunc

type LifeCycleFunc func(ctx context.Context) error

type Logger

type Logger interface {
	Debug(mgs string, params ...any)
	Info(mgs string, params ...any)
}

type Provider

type Provider struct {
	Handle Checkable
	Name   string
}

Provider is a provder we can check for healthz

type Service

type Service struct {
	Name         string `json:"name"`
	Healthy      bool   `json:"healthy"`
	ErrorMessage string `json:"error_message,omitempty"`
}

Service struct reprecenting a healthz provider and it's status

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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