core

module
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2020 License: GPL-3.0

README

core

core for go services

Release Go Report Card

simple

config.yaml

env: dev
log: /tmp/log

debug:
  addr: localhost:8888

main.go

package main

import (
	"github.com/deweppro/core/pkg/app"
	"github.com/deweppro/core/pkg/provider/debug"
)

type Simple struct{}

func NewSimple(_ *debug.Debug) *Simple { return &Simple{} }
func (s *Simple) Up() error            { return nil }
func (s *Simple) Down() error          { return nil }

func main() {
    app.New("config.yaml").
        ConfigModels(
            &debug.ConfigDebug{},
        ).
        Modules(
            NewSimple,
            debug.New,
        ).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()
}

Jump to

Keyboard shortcuts

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