diff --git a/pkg/middle/Data.go b/pkg/middle/Data.go new file mode 100644 index 0000000..18dcc80 --- /dev/null +++ b/pkg/middle/Data.go @@ -0,0 +1,24 @@ +package middle + +// 数据类型 +type data struct { + m map[string]interface{} +} + +// 数据初始化,分配内存 +func NewData() *data { + return &data{ + make(map[string]interface{}), + } +} + +// 获取数据 +func (d *data) Get(key string) interface{} { + return d.m[key] +} + +// 推入数据 +func (d *data) Put(key string, val interface{}) error { + d.m[key] = val + return nil +} diff --git a/pkg/middle/Middle.go b/pkg/middle/Middle.go index c79d7d3..2c0e1a4 100644 --- a/pkg/middle/Middle.go +++ b/pkg/middle/Middle.go @@ -1,36 +1,11 @@ package middle -import "fmt" - // 获取数据的接口 type Msg interface { Put(key string, val interface{}) error Get(key string) interface{} } -// 数据类型 -type data struct { - m map[string]interface{} -} - -// 数据初始化,分配内存 -func NewData() *data { - return &data{ - make(map[string]interface{}), - } -} - -// 获取数据 -func (d *data) Get(key string) interface{} { - return d.m[key] -} - -// 推入数据 -func (d *data) Put(key string, val interface{}) error { - d.m[key] = val - return nil -} - type middleWare interface { Put(msg Msg) Out() Msg @@ -49,6 +24,7 @@ func NewDriver() *Driver { return new(Driver) } +// 驱动 func (d *Driver) Start(msg Msg) { for _, m := range d.conf { @@ -56,9 +32,9 @@ func (d *Driver) Start(msg Msg) { go m.Middle.Put(msg) msg = m.Middle.Out() - fmt.Println(msg.Get("1")) } + } func main() { @@ -67,6 +43,14 @@ func main() { Weight: 1, Middle: NewUpload(), }, + { + Weight: 2, + Middle: NewStorage(), + }, + { + Weight: 3, + Middle: NewSend(), + }, } da := NewDriver() diff --git a/pkg/middle/Send.go b/pkg/middle/Send.go new file mode 100644 index 0000000..ff93d2d --- /dev/null +++ b/pkg/middle/Send.go @@ -0,0 +1,24 @@ +package middle + +import "fmt" + +type Send struct { + ch chan *data +} + +func NewSend() *Send { + return &Send{ + make(chan *data), + } +} + +func (m *Send) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Send") + m.ch <- da +} + +func (m *Send) Out() Msg { + return <-m.ch +} diff --git a/pkg/middle/Storage.go b/pkg/middle/Storage.go new file mode 100644 index 0000000..5450ebd --- /dev/null +++ b/pkg/middle/Storage.go @@ -0,0 +1,24 @@ +package middle + +import "fmt" + +type Storage struct { + ch chan *data +} + +func NewStorage() *Storage { + return &Storage{ + make(chan *data), + } +} + +func (m *Storage) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Storage") + m.ch <- da +} + +func (m *Storage) Out() Msg { + return <-m.ch +} diff --git a/pkg/middle/upload.go b/pkg/middle/Upload.go similarity index 85% rename from pkg/middle/upload.go rename to pkg/middle/Upload.go index 9e0e0e0..0e160c2 100644 --- a/pkg/middle/upload.go +++ b/pkg/middle/Upload.go @@ -1,21 +1,25 @@ package middle +import "fmt" + type Upload struct { ch chan *data } -func (m *Upload) Put(msg Msg) { - da := NewData() - da.Put("1", msg) - m.ch <- da -} - -func (m *Upload) Out() Msg { - return <-m.ch -} - func NewUpload() *Upload { return &Upload{ make(chan *data), } } + +func (m *Upload) Put(msg Msg) { + da := NewData() + da.Put("1", msg) + fmt.Println("传入Upload") + + m.ch <- da +} + +func (m *Upload) Out() Msg { + return <-m.ch +}