go

module
v1.1.40 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0

README

go

Go Toolkit

Inject example

import (
	"flag"
	"fmt"
	"os"

	c "github.com/d0ngw/go/common"
	"github.com/d0ngw/go/inject"
	"github.com/d0ngw/go/orm"
)

// WorkerApp woker服务,适用于后台执行的任务
type WorkerApp struct {
	config  c.Configurer
	modules []*inject.Module
	env     string
}

// NewWorkerApp new http app
func NewWorkerApp(config c.Configurer, modules []*inject.Module) *WorkerApp {
	ret := &WorkerApp{
		config:  config,
		modules: modules,
	}
	return ret
}

// Usage flag.Usage
func (p *WorkerApp) Usage() {
	fmt.Fprintf(os.Stderr, "\n")
	fmt.Fprintf(os.Stderr, "usage:%s args...\n", os.Args[0])
	fmt.Fprintf(os.Stderr, "\n")
	flag.PrintDefaults()
	os.Exit(1)
}

// FlagInit flag的初始化
func (p *WorkerApp) FlagInit() {
	flag.Usage = p.Usage
	flag.StringVar(&p.env, "env", "dev", "the enviroment")
}

// Run 运行,直到推出
func (p *WorkerApp) Run() {
	flag.Parse()

	injector, err := inject.SetupInjector(p.config, "", p.env, p.modules...)
	if err != nil {
		c.Errorf("init injector fail,err:%s", err)
		p.Usage()
	}

	if err := injector.Start(nil); err != nil {
		c.Errorf("Start servcie fail,err:%s", err)
		os.Exit(1)
	}

	shutdownHook := c.NewShutdownhook(defaultShutdownHooks...)
	shutdownHook.AddHook(func() {
		injector.Stop(nil)
	})

	shutdownHook.WaitShutdown()
}

var app *WorkerApp

func init(){
    var module = inject.NewModule()
    module.Bind(orm.NewSimpleShardDBService(orm.NewMySQLDBPool))

    app = NewWorkerApp(xxxconfig,[]*inject.Module{module})
    app.FlagInit()
}

func main(){
    app.Run()
}

Directories

Path Synopsis
Package cache 提供缓冲相关的服务
Package cache 提供缓冲相关的服务
counter
Package counter supply counter service
Package counter supply counter service
list
Package list has some list cache implemented by redis
Package list has some list cache implemented by redis
Package http 提供基本的http服务
Package http 提供基本的http服务
Package inject 提供类似Guice Injector的依赖注入功能,可以用于简化服务的组装
Package inject 提供类似Guice Injector的依赖注入功能,可以用于简化服务的组装
Package orm 简单的DAL 封装
Package orm 简单的DAL 封装

Jump to

Keyboard shortcuts

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