app

package module
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

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"

	"go.osspkg.com/goppy/app"
	"go.osspkg.com/goppy/xlog"
)

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

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

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

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

func main() {
	app.New().
		Logger(log.Default()).
		ConfigFile(
			"./config.yaml",
			Config{},
		).
		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(_ *log.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(xc.Context) error and Down() error methods, they will be called Up(xc.Context) 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 _ service.IServiceCtx = (*Simple3)(nil)
type Simple3 struct{}
func NewSimple3(_ *Simple4) *Simple3 { return &Simple3{} }
func (s3 *Simple3) Up(_ xc.Context) error { return nil }
func (s3 *Simple3) Down(_ xc.Context) 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!!")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	Logger(log xlog.Logger) App
	Modules(modules ...interface{}) App
	ConfigResolvers(res ...config.Resolver) App
	ConfigFile(filename string) App
	ConfigModels(configs ...interface{}) App
	PidFile(filename string) App
	Run()
	Invoke(call interface{})
	Call(call interface{})
	ExitFunc(call func(code int)) App
}

func New

func New() App

New create application

type Config

type Config struct {
	Env string    `yaml:"env"`
	Log LogConfig `yaml:"log"`
}

Config config model

type Container added in v0.1.7

type Container interface {
	Start() error
	Register(items ...interface{}) error
	Invoke(item interface{}) error
	BreakPoint(item interface{}) error
	Stop() error
}

func NewContainer added in v0.1.7

func NewContainer(ctx xc.Context) Container

type LogConfig added in v0.3.3

type LogConfig struct {
	Level    uint32 `yaml:"level"`
	FilePath string `yaml:"file_path"`
	Format   string `yaml:"format"`
}

type Modules

type Modules []interface{}

Modules DI container

func (Modules) Add

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

Add object to container

type ServiceContextInterface

type ServiceContextInterface interface {
	Up(ctx context.Context) error
	Down() error
}

type ServiceInterface

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

type ServiceXContextInterface added in v0.1.7

type ServiceXContextInterface interface {
	Up(ctx xc.Context) error
	Down() error
}

Jump to

Keyboard shortcuts

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