app

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2020 License: BSD-3-Clause Imports: 17 Imported by: 0

README

go-app

Coverage Status Release Go Report Card Build Status

simple

config.yaml

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

main.go

package main

import (
	"fmt"

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

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

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

func NewSimple(_ *SimpleConfig) *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().
		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.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().
        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 New

func New() *application

func NewLogger added in v1.3.0

func NewLogger(filename string) *log

func NewModules added in v1.3.0

func NewModules() *modules

func NewSources added in v1.3.0

func NewSources(filename string) (*sources, error)

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"`
	PidFile string `yaml:"pid" json:"pid"`
}

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 (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 added in v1.3.0

type ENV string

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