godi

package module
v0.0.0-...-e8d9f39 Latest Latest
Warning

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

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

README

godi

godi is a Go package that extends the Uber Fx framework, providing a simpler and more concise way to manage dependency injection in Go applications. It introduces syntactic sugar to make your code cleaner and more maintainable.

Features

  • Service Registration: Easily register services with dependencies.
  • Interface Binding: Bind implementations to interfaces for flexible dependency management.
  • Lifecycle Hooks: Run functions after the application starts.
  • Simplified API: Use a straightforward API to manage your application's dependencies.

Installation

To install godi, use the following command:

go get github.com/Rustixir/godi

Getting Started

Here's a quick example to get you started with godi.

Define Your Services
package main

import (
	"log/slog"
	"github.com/Rustixir/godi"
)

func NewServiceA(logger *slog.Logger) serviceA {
	return serviceA{logger: logger}
}

type serviceA struct {
	logger *slog.Logger
}

func (s *serviceA) Print() {
	s.logger.Info("serviceA Print: Hello")
}

func NewServiceB(logger *slog.Logger) serviceB {
	return serviceB{logger: logger}
}

type serviceB struct {
	logger *slog.Logger
}

func (s *serviceB) Print() {
	s.logger.Info("serviceB Print: Hello")
}

func NewServiceC(a serviceA, b serviceB, logger *slog.Logger) serviceC {
	return serviceC{ServiceA: a, ServiceB: b, logger: logger}
}

type serviceC struct {
	ServiceA serviceA
	ServiceB serviceB
	logger   *slog.Logger
}

func (c serviceC) Run() {
	c.logger.Info("Works fine")
	c.ServiceA.Print()
	c.ServiceB.Print()
}
Register and Run Services
func main() {
	godi.AddSlog()
	godi.AddService("serviceA", NewServiceA)
	godi.AddService("serviceB", NewServiceB)
	godi.AddService("serviceC", NewServiceC)

	godi.OnAfterStart(func(service serviceC) error {
		service.Run()
		return nil
	})

	godi.Run()
}

API Reference

  • AddService(name string, constructor interface{}, dependencies ...interface{}): Register a service with optional dependencies.
  • AddIService(name string, constructor interface{}, Interface interface{}): Register a service and bind it to an interface.
  • OnAfterStart(funcs ...interface{}): Register functions to execute after the application starts.
  • Run(): Start the application and execute all registered lifecycle hooks.

License

This project is licensed under the Apache License 2.0.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHook

func AddHook(OnStart Hook, OnStop Hook)

func AddIService

func AddIService(name string, constructor interface{}, Interface interface{})

func AddService

func AddService(name string, constructor interface{}, dependencies ...Option)

func AddSlog

func AddSlog()

func AddSlogWithHandler

func AddSlogWithHandler(handler slog.Handler)

func AddZapDevelopment

func AddZapDevelopment(options ...zap.Option)

func AddZapProduction

func AddZapProduction(options ...zap.Option)

func OnAfterStart

func OnAfterStart(funcs ...interface{})

func Run

func Run()

Types

type Hook

type Hook func(context.Context) error

type In

type In = fx.In

type Lifecycle

type Lifecycle = fx.Lifecycle

type Option

type Option = fx.Option

func Provide

func Provide(constructors ...interface{}) Option

func Supply

func Supply(values ...interface{}) Option

type Out

type Out = fx.Out

Jump to

Keyboard shortcuts

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