go-app

module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2020 License: GPL-3.0

README

go-app

Release Go Report Card

simple

config.yaml

env: dev
log: /tmp/log

main.go

package main

import (
	"fmt"

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

type Simple struct{}

func NewSimple(_ *logger.ConfigLog) *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(
			&logger.ConfigLog{},
		).
		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

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()
}

Directories

Path Synopsis
examples
simple command
pkg
app

Jump to

Keyboard shortcuts

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