app

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: BSD-3-Clause Imports: 15 Imported by: 0

README

go-app

Coverage Status Release Go Report Card Build Status

simple

config.yaml

env: dev
log: /tmp/log

main.go

package main

import (
	"fmt"

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

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

type Simple struct{}

func NewSimple(_ *app.ConfigLogger) *Simple {
	fmt.Println("call NewSimple")
	return &Simple{}
}

func (s *Simple) Up() error {
	fmt.Println("call *Simple.Up")
	return nil
}

func (s *Simple) Down() error {
	fmt.Println("call *Simple.Down")
	return nil
}

func main() {
	app.
		New("config.yaml").
		ConfigModels(&app.ConfigLogger{}).
		Modules(NewSimple).
		PidFile("/tmp/app.pid").
		Run()
}

HowTo

Run the app

app.New(<path to config file: string>)
    .ConfigModels(<config objects separate by comma: ...interface{}>)
    .Modules(<config objects separate by comma: ...interface{}>)
    .PidFile(<process id file path: string>)
    .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.ServiceInterface = (*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("config.yaml").
        ConfigModels(
            &debug.ConfigDebug{},
        ).
        Modules(
            debug.New,
            NewSimple2,
            NewSimple3,
            Simple4{}
            s1, hw,
        ).PidFile("/tmp/app.pid").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")
)

Functions

func New

func New(fileconfig string) *application

func OnSyscallStop

func OnSyscallStop(callFunc func())

func OnSyscallUp

func OnSyscallUp(callFunc func())

func WrapErrors

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

Types

type ConfigLogger

type ConfigLogger struct {
	Env     string `yaml:"env" json:"env"`
	LogFile string `yaml:"log" json:"log"`
}

func (ConfigLogger) MarshalEasyJSON

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

MarshalEasyJSON supports easyjson.Marshaler interface

func (ConfigLogger) MarshalJSON

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

MarshalJSON supports json.Marshaler interface

func (*ConfigLogger) UnmarshalEasyJSON

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

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*ConfigLogger) UnmarshalJSON

func (v *ConfigLogger) 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 (_di *DI) Build() error

Build - initialize dependencies

func (*DI) Down

func (_di *DI) Down() error

Down - stop all services in dependencies

func (*DI) Inject

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

Inject - obtained dependence

func (*DI) Register

func (_di *DI) Register(items []interface{}) error

Register - register a new dependency

func (*DI) Up

func (_di *DI) Up() error

Up - start all services in dependencies

type ForceClose

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

type ServiceInterface

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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