application

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2021 License: BSD-3-Clause Imports: 18 Imported by: 4

README

Application as service

Base config file

config.yaml

env: dev
level: 3 
log: /var/log/simple.log
pig: /var/run/simple.pid

level:

  • 0 - error only
  • 1 - + warning
  • 2 - + info
  • 3 - + debug

Example

package main

import (
	"fmt"

	"github.com/deweppro/go-app"
	"github.com/deweppro/go-logger"
)

var _ app.Servicer = (*Simple)(nil)

type (
	//Simple model
	Simple struct{}
	//SimpleConfig config model
	SimpleConfig struct {
		Env string `yaml:"env"`
	}
)

//NewSimple init Simple
func NewSimple(_ *SimpleConfig) *Simple {
	fmt.Println("call NewSimple")
	return &Simple{}
}

//Up  method for start Simple in DI container
func (s *Simple) Up() error {
	fmt.Println("call *Simple.Up")
	return nil
}

//Down  method for stop Simple in DI container
func (s *Simple) Down() error {
	fmt.Println("call *Simple.Down")
	return nil
}

func main() {
	app.New().
		Logger(logger.Default()).
		ConfigFile(
			"./config.yaml",
			&SimpleConfig{},
		).
		Modules(
			NewSimple,
		).
		Run()
}

HowTo

Run the app

app.New()
    .ConfigFile(<path to config file: string>, <config objects separate by comma: ...interface{}>)
    .Modules(<config objects separate by comma: ...interface{}>)
    .Run()

Supported types for initialization

  • Function that returns an object or interface

All incoming dependencies will be injected automatically

type Simple1 struct{}
func NewSimple1(_ *logger.Logger) *Simple1 { return &Simple1{} }

Returns the interface

type Simple2 struct{}
type Simple2Interface interface{
    Get() string
}
func NewSimple2() Simple2Interface { return &Simple2{} }
func (s2 *Simple2) Get() string { 
    return "Hello world"
}

If the object has the Up() error and Down() error methods, they will be called Up() error when the app starts, and Down() error when it finishes. This allows you to automatically start and stop routine processes inside the module

var _ app.Servicer = (*Simple3)(nil)
type Simple3 struct{}
func NewSimple3(_ *Simple4) *Simple3 { return &Simple3{} }
func (s3 *Simple3) Up() error { return nil }
func (s3 *Simple3) Down() error { return nil }
  • Named type
type HelloWorld string
  • Object structure
type Simple4 struct{
    S1 *Simple1
    S2 Simple2Interface
    HW HelloWorld
}
  • Object reference or type
s1 := &Simple1{}
hw := HelloWorld("Hello!!")

Example of initialization of all types

func main() {
	
    s1 := &Simple1{}
    hw := HelloWorld("Hello!!")

    app.New().
        Logger(logger.Default()).
        ConfigFile(
            "config.yaml",
            &debug.ConfigDebug{},
        ).
        Modules(
            debug.New,
            NewSimple2,
            NewSimple3,
            Simple4{},
            s1, hw,
        ).
        Run()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDepRunning    = errors.New("dependencies is already running")
	ErrDepNotRunning = errors.New("dependencies are not running yet")
	ErrDepEmpty      = errors.New("dependencies is empty")
	ErrDepUnknown    = errors.New("unknown dependency")
	ErrBadAction     = errors.New("is not a supported action")
	ErrBadFileFormat = errors.New("is not a supported file format")
)

Functions

func OnSyscallStop

func OnSyscallStop(callFunc func())

OnSyscallStop calling a function if you send a system event stop

func OnSyscallUp

func OnSyscallUp(callFunc func())

OnSyscallUp calling a function if you send a system event SIGHUP

func WrapErrors

func WrapErrors(err1, err2 error, message string) error

WrapErrors combining multiple errors

Types

type App

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

App application model

func New

func New() *App

New create application

func (*App) ConfigFile

func (a *App) ConfigFile(filename string, configs ...interface{}) *App

ConfigFile set config file path and configs models

func (*App) Logger

func (a *App) Logger(log logger.Logger) *App

Logger setup logger

func (*App) Modules

func (a *App) Modules(modules ...interface{}) *App

Modules append object to modules list

func (*App) Run

func (a *App) Run()

Run run application

type BaseConfig

type BaseConfig struct {
	Env     string `yaml:"env" json:"env" toml:"env"`
	PidFile string `yaml:"pid" json:"pid" toml:"pid"`
	//logger
	Level   uint32 `yaml:"level" json:"level" toml:"level"`
	LogFile string `yaml:"log" json:"log" toml:"log"`
}

BaseConfig config model

func (BaseConfig) MarshalEasyJSON

func (v BaseConfig) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (BaseConfig) MarshalJSON

func (v BaseConfig) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*BaseConfig) UnmarshalEasyJSON

func (v *BaseConfig) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*BaseConfig) UnmarshalJSON

func (v *BaseConfig) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type DI

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

DI - managing dependencies

func NewDI

func NewDI() *DI

NewDI - create new dependency manager

func (*DI) Build

func (d *DI) Build() error

Build - initialize dependencies

func (*DI) Down

func (d *DI) Down() error

Down - stop all services in dependencies

func (*DI) Inject

func (d *DI) Inject(item interface{}) error

Inject - obtained dependence

func (*DI) Register

func (d *DI) Register(items ...interface{}) error

Register - register a new dependency

func (*DI) Up

func (d *DI) Up() error

Up - start all services in dependencies

type ENV

type ENV string

ENV type for enviremants (prod, dev, stage, etc)

type ForceClose

type ForceClose struct {
	C     context.Context
	Close context.CancelFunc
}

ForceClose model for force close application

type Log

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

Log model

func NewLogger

func NewLogger(conf *BaseConfig) *Log

NewLogger init logger

func (*Log) Close

func (l *Log) Close() error

Close log file

func (*Log) Handler

func (l *Log) Handler(log logger.Logger)

Handler set custom logger for application

type Modules

type Modules []interface{}

Modules DI container

func (Modules) Add

func (m Modules) Add(v ...interface{}) Modules

Add object to container

type Servicer

type Servicer interface {
	Up() error
	Down() error
}

Servicer interface for services

type Sources

type Sources string

Sources model

func (Sources) Decode

func (s Sources) Decode(configs ...interface{}) error

Decode unmarshal file to model

Jump to

Keyboard shortcuts

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