approll

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

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 2 Imported by: 0

README

AppRoll

Go Report Card codecov

What is AppRoll?

AppRoll is a simple modular framework with dependency injection based on reflection. It is designed to be used as a base for a all kinds of applications, from simple CLI tools to complex web applications.

How to use it?

AppRoll is designed to be used as a base for your application.

First, it is worth learning about the presence of 2 abstractions, Module and Provider.

A module is a structure that follows simple rules:

  • It has a name ending with "Module"
  • It has a function called "RollStart" with any dependencies (modules or provider) as arguments and returns an error
  • Modules can only be initialized in AppRoll in a single instance
  • Modules cannot create circular dependencies between each other (this is checked at runtime, and will return an error if a circular dependency is detected)
  • Modules can have dependencies on other modules

A provider is a structure that follows simple rules:

  • Is a pointer to a structure, cannot be a primitive (int, uint, float, etc...) or a function.

Simple example

type UserRepositoryModule struct {
    DB *gorm.DB
}

func (m *UserRepositoryModule) RollStart(db *gorm.DB) error {
    // db *gorm.DB is a dependency of this module and added as provider to AppRoll
    m.DB = db
	
    return nil
}

type UserServiceModule struct {
    UserRepository *UserRepositoryModule
}

func (m *UserServiceModule) RollStart(repo *UserRepositoryModule) error {
    m.UserRepository = repo
    
    return nil
}

func main() {
    app := approll.NewApp(approll.Config{})
	
    // It can be in any order, but it is recommended to add providers first
    err := app.AddProvider(func() *gorm.DB {
        db, _ := gorm.Open("sqlite3", "test.db")
        return db
    }())
    if err != nil {
        panic(err)
    }
	
    // It can be in any order
    err = app.AddModule(&UserRepositoryModule{}, &UserServiceModule{})
    if err != nil {
        panic(err)
    }
	
    if err := app.Start(); err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// Config is the application configuration.
	Config Config

	// Modules is a list of modules that are loaded.
	Modules map[string]interface{}

	// Providers is a list of providers that are loaded.
	Providers map[string]interface{}
}

func NewApp

func NewApp(config Config) *App

NewApp creates a new application.

func (*App) AddModule

func (app *App) AddModule(module ...interface{}) error

AddModule registers a module with the application.

func (*App) AddProvider

func (app *App) AddProvider(provider ...interface{}) error

func (*App) GetModule

func (app *App) GetModule(module interface{}) error

GetModule retrieves a module from the application.

func (*App) GetProvider

func (app *App) GetProvider(provider interface{}) error

func (*App) Start

func (app *App) Start() error

Start initializes all modules and starts. If any module fails to start, the application will not start.

type Config

type Config struct {
	// OverwriteModule will overwrite an existing module with the same name.
	OverwriteModule bool

	// OverwriteProvider will overwrite an existing provider with the same name.
	OverwriteProvider bool
}

Directories

Path Synopsis
modules
env

Jump to

Keyboard shortcuts

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