1 Star 0 Fork 0

micro-community/x-edge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
edge.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
Edward 提交于 2020-06-04 00:08 . simple package using
package edge
import (
"strings"
nedge "github.com/micro-community/x-edge/edge"
"github.com/micro/cli/v2"
"github.com/micro/go-micro/v2"
log "github.com/micro/go-micro/v2/logger"
)
//Edge config locate in x-edge/cmd
//Service of edge srv
type Service interface {
Name() string
Init(opts ...Option) error
Run() error
String() string
MService() micro.Service
EService() nedge.Service
}
//edgeApp for edge process
type edgeApp struct {
opts Options
}
//NewService return a edge service application
func NewService(opts ...Option) Service {
options := newOptions(opts...)
s := &edgeApp{
opts: options,
}
return s
}
func (e *edgeApp) buildGoMicroOption() []micro.Option {
serviceOpts := []micro.Option{}
if len(e.opts.Flags) > 0 {
serviceOpts = append(serviceOpts, micro.Flags(e.opts.Flags...))
}
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
if name := ctx.String("server_name"); len(name) > 0 {
e.opts.Name = name
}
if ver := ctx.String("server_version"); len(ver) > 0 {
e.opts.Version = ver
}
if id := ctx.String("server_id"); len(id) > 0 {
e.opts.ID = id
}
if addr := ctx.String("server_address"); len(addr) > 0 {
e.opts.Address = addr
}
if adv := ctx.String("server_advertise"); len(adv) > 0 {
e.opts.Advertise = adv
}
return nil
}))
return serviceOpts
}
func (e *edgeApp) Init(opts ...Option) error {
for _, o := range opts {
o(&e.opts)
}
serviceOpts := e.buildGoMicroOption()
edgeOptions := []nedge.Option{}
if e.opts.EdgeTransport != nil {
//setup transport for edge
edgeOptions = append(edgeOptions, nedge.Transport(e.opts.EdgeTransport))
}
if strings.Trim(e.opts.EdgeHost, " ") != "" {
edgeOptions = append(edgeOptions, nedge.Host(e.opts.EdgeHost))
}
if e.opts.Extractor != nil {
edgeOptions = append(edgeOptions, nedge.WithExtractor(e.opts.Extractor))
} else {
edgeOptions = append(edgeOptions, nedge.WithExtractor(nedge.DefaultExtractor))
}
e.opts.Edge.Init(edgeOptions...)
serviceOpts = append(serviceOpts, micro.Action(func(ctx *cli.Context) error {
// execute edge service Action
if e.opts.Edge.Options().Action != nil {
e.opts.Edge.Options().Action(ctx)
}
if e.opts.Action != nil {
e.opts.Action(ctx)
}
return nil
}))
e.opts.MicroService.Init(serviceOpts...)
return nil
}
func (e *edgeApp) start() error {
return nil
}
func (e *edgeApp) stop() error {
return nil
}
//Run to launch edge server process
func (e *edgeApp) Run() error {
// init edge itself
log.Init(log.WithFields(map[string]interface{}{"Service": "[Edge]"}))
if err := e.opts.Edge.Start(); err != nil {
log.Fatal(err)
}
// Run go-micro servier
if err := e.opts.MicroService.Run(); err != nil {
log.Fatal(err)
}
if err := e.opts.Edge.Stop(); err != nil {
log.Fatal(err)
}
return nil
}
// return micro.service
func (e *edgeApp) MService() micro.Service {
return e.opts.MicroService
}
// return micro.service
func (e *edgeApp) EService() nedge.Service {
return e.opts.Edge
}
func (e *edgeApp) Name() string {
return e.opts.Name
}
func (e *edgeApp) String() string {
return "edge app"
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/micro-community/x-edge.git
[email protected]:micro-community/x-edge.git
micro-community
x-edge
x-edge
develop

搜索帮助