diff --git a/dtu/channel.go b/dtu/channel.go index 263ac0c57f8a3b9eec4ace6272a69cd8112316ac..5680dbdbfe33cdede3eb0bc631f1d0179ff6c29d 100644 --- a/dtu/channel.go +++ b/dtu/channel.go @@ -32,7 +32,7 @@ func NewChannel(channel *types.Channel) *Channel { } func (c *Channel) Open() error { - if c.IsServer { + if c.Net.IsServer { return c.Listen() } else { return c.Dial() @@ -40,7 +40,7 @@ func (c *Channel) Open() error { } func (c *Channel) Dial() error { - conn, err := net.Dial(c.Net, c.Addr) + conn, err := net.Dial(c.Net.Type, c.Net.Addr) if err != nil { return err } @@ -54,16 +54,16 @@ func (c *Channel) Dial() error { func (c *Channel) Listen() error { var err error - switch c.Net { + switch c.Net.Type { case "tcp", "tcp4", "tcp6", "unix": - c.listener, err = net.Listen(c.Net, c.Addr) + c.listener, err = net.Listen(c.Net.Type, c.Net.Addr) if err != nil { return err } go c.accept() case "udp", "udp4", "udp6", "unixgram": - c.packetConn, err = net.ListenPacket(c.Net, c.Addr) + c.packetConn, err = net.ListenPacket(c.Net.Type, c.Net.Addr) if err != nil { return err diff --git a/dtu/misc.go b/dtu/misc.go index 54634db9e753b463b1499f7223d501276485aaf8..82d67401032b0b820997be8ff04cd1f1ab61927c 100644 --- a/dtu/misc.go +++ b/dtu/misc.go @@ -55,7 +55,7 @@ func CreateChannel(c *types.Channel) (*Channel, error) { return startChannel(c) } -func GetChannel(id int64) (*Channel, error) { +func GetChannel(id int) (*Channel, error) { v, ok := channels.Load(id) if !ok { return nil, errors.New("通道不存在") @@ -63,7 +63,7 @@ func GetChannel(id int64) (*Channel, error) { return v.(*Channel), nil } -func DeleteChannel(id int64) error { +func DeleteChannel(id int) error { v, ok := channels.Load(id) if !ok { return errors.New("通道不存在") diff --git a/portal/src/app/main/channel-edit/channel-edit.component.html b/portal/src/app/main/channel-edit/channel-edit.component.html index 5283adeab331ea98d06bfea6a17273e35c85eeca..8a9004acb224c1d6401e484d6a3487c16b171888 100644 --- a/portal/src/app/main/channel-edit/channel-edit.component.html +++ b/portal/src/app/main/channel-edit/channel-edit.component.html @@ -1,53 +1,93 @@ -
- - 名称 - - - - - - 序列号 - - - - - - 网络类型 - - - - - - 地址 - - - - - - 服务端 - - - - - - 心跳 - - - - - - 注册包 - - - - - - - - - -
+
+
+ 名称 +
+
+ +
+
+ +
+
+ 网络 +
+
+ + + + + + + + + + + +
+
+ +
+
+ 禁用 +
+
+ +
+
+ +
+
+ 注册包 +
+
+ +
+
+ +
+
+
+ 正则表达式 +
+
+ +
+
+ +
+ + +
+
+ 心跳包 +
+
+ +
+
+ +
+
+
+ 间隔(秒) +
+
+ +
+
+ +
+
+ 内容(十六进制) +
+
+ +
+
+ +
+ + + + diff --git a/portal/src/app/main/channel-edit/channel-edit.component.scss b/portal/src/app/main/channel-edit/channel-edit.component.scss index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b12fb00960dc2bfb75c28da6583e63012ab6d434 100644 --- a/portal/src/app/main/channel-edit/channel-edit.component.scss +++ b/portal/src/app/main/channel-edit/channel-edit.component.scss @@ -0,0 +1,4 @@ + +.item{ + margin-bottom: 20px; +} diff --git a/portal/src/app/main/channel-edit/channel-edit.component.ts b/portal/src/app/main/channel-edit/channel-edit.component.ts index 4ca4c6cf359a3525a76a957df1c38bceb9e67423..72c782988b95e3f549720e6c5abb9433a74c21fb 100644 --- a/portal/src/app/main/channel-edit/channel-edit.component.ts +++ b/portal/src/app/main/channel-edit/channel-edit.component.ts @@ -12,40 +12,29 @@ export class ChannelEditComponent implements OnInit { @Input() channel: any = {}; - validateForm!: FormGroup; - - constructor(private as: ApiService, private fb: FormBuilder, private drawerRef: NzDrawerRef) { + constructor(private as: ApiService, private drawerRef: NzDrawerRef) { } ngOnInit(): void { - this.initForm({}); + this.initChannel({}); if (this.channel.id) { this.as.get('channel/' + this.channel.id).subscribe(res => { this.channel = res.data; - this.initForm(this.channel); + this.initChannel(this.channel); }); } - } - submit(): void { - for (const i in this.validateForm.controls) { - this.validateForm.controls[i].markAsDirty(); - this.validateForm.controls[i].updateValueAndValidity(); - } - if (!this.validateForm.valid) { - return; - } if (this.channel.id) { - this.as.put('channel/' + this.channel.id, this.validateForm.value).subscribe(res => { + this.as.put('channel/' + this.channel.id, this.channel).subscribe(res => { console.log(res); // TODO 修改成功 this.drawerRef.close(res.data); }); } else { - this.as.post('channel', this.validateForm.value).subscribe(res => { + this.as.post('channel', this.channel).subscribe(res => { console.log(res); // TODO 保存成功 this.drawerRef.close(res.data); @@ -53,25 +42,11 @@ export class ChannelEditComponent implements OnInit { } } - initForm(item): void { - if (!item.register) - item.register = {}; - if (!item.heart_beat) - item.heart_beat = {}; + initChannel(item): void { + item.net = item.net || { is_server: true, type: 'tcp', addr: ':1843'}; + item.register = item.register || {}; + item.heart_beat = item.heart_beat || {}; - this.validateForm = this.fb.group({ - name: [item.name, [Validators.required]], - // tags: [item.tags], - serial: [item.serial], - net: [item.net, [Validators.required]], - addr: [item.addr, [Validators.required]], - is_server: [item.is_server], - disabled: [item.disabled], - 'register.enable': [item.register.enable], - 'register.regex': [item.register.regex], - 'heart_beat.enable': [item.heart_beat.enable], - 'heart_beat.interval': [item.heart_beat.interval], - 'heart_beat.content': [item.heart_beat.content], - }); + this.channel = item; } } diff --git a/portal/src/app/main/channel/channel.component.html b/portal/src/app/main/channel/channel.component.html index e17f9e2bafdaf4c1d8068fdcaf0f0649f63b2d19..a0aff4f2eb77dcd4d8e8d5331a783fb7855dc03c 100644 --- a/portal/src/app/main/channel/channel.component.html +++ b/portal/src/app/main/channel/channel.component.html @@ -1,24 +1,25 @@ - - + + + + - - + + ID 名称 标签 - 序号 网络 - 地址 + 状态 创建时间 @@ -28,13 +29,13 @@ {{ data.id }} {{ data.name }} {{ data.tags }} - {{ data.serial }} - {{ data.net }} - {{ data.addr }} - {{ data.created }} + {{ data.net.is_server ? '服务端':'客户端' }} {{data.net.type}} {{data.net.addr}} + {{data.disabled ? '禁用' : ''}} 启动/停止 - + + {{ data.created | amDateFormat:'YYYY-MM-DD HH:mm:ss' }} + diff --git a/portal/src/app/main/channel/channel.component.ts b/portal/src/app/main/channel/channel.component.ts index b108c09eb6e3a0048a3a2ddcbfe2b03ba503281f..45b2dfed0d934584d33c60954b4a08571f83a0f7 100644 --- a/portal/src/app/main/channel/channel.component.ts +++ b/portal/src/app/main/channel/channel.component.ts @@ -32,7 +32,7 @@ export class ChannelComponent implements OnInit { this.drawer.create({ nzTitle: c ? '编辑' : '创建', nzMaskClosable: false, - nzWidth: 400, + nzWidth: 500, nzContent: ChannelEditComponent, nzContentParams: { channel: c || {} diff --git a/portal/src/app/main/main.module.ts b/portal/src/app/main/main.module.ts index c49dee20e3b7929030b2c09c9d28ff79e6cf5e0a..0c37017c1e6614a75bf6f8e17c40839a3cc8e735 100644 --- a/portal/src/app/main/main.module.ts +++ b/portal/src/app/main/main.module.ts @@ -14,8 +14,8 @@ import { NzCheckboxModule, NzDividerModule, NzDrawerModule, NzFormModule, NzIconModule, - NzInputModule, - NzModalModule, NzPopconfirmModule, NzSwitchModule, + NzInputModule, NzInputNumberModule, + NzModalModule, NzPopconfirmModule, NzSelectModule, NzSwitchModule, NzTableModule, NzToolTipModule } from 'ng-zorro-antd'; @@ -31,6 +31,7 @@ import {ChannelDetailComponent} from './channel-detail/channel-detail.component' import {LinkDetailComponent} from './link-detail/link-detail.component'; import {UserEditComponent} from './user-edit/user-edit.component'; import {LinkMonitorComponent} from './link-monitor/link-monitor.component'; +import {NzSpaceModule} from "ng-zorro-antd/space"; @NgModule({ @@ -64,6 +65,9 @@ import {LinkMonitorComponent} from './link-monitor/link-monitor.component'; IconsProviderModule, NzDividerModule, NzDrawerModule, + NzSelectModule, + NzSpaceModule, + NzInputNumberModule, ], bootstrap: [MainComponent] }) diff --git a/types/channel.go b/types/channel.go index 92f292bcb936ec0e78345637df26435a4fd986fe..3be02a943a1b71d961950220eb8c3ffffa8c8807 100644 --- a/types/channel.go +++ b/types/channel.go @@ -2,15 +2,23 @@ package types import "time" +type NetConf struct { + Type string `json:"type"` + Addr string `json:"addr"` + IsServer bool `json:"is_server"` + Timeout int `json:"timeout"` //TODO 改为秒 +} + type RegisterConf struct { - Enable bool - Length int - Regex string + Enable bool `json:"enable"` + Length int `json:"length"` + Regex string `json:"regex"` } type HeartBeatConf struct { - Enable bool - Content []byte + Enable bool `json:"enable"` + Interval int `json:"interval"` //TODO 改为秒 + Content []byte `json:"content"` } type Channel struct { @@ -18,24 +26,9 @@ type Channel struct { Name string `json:"name"` Tags []string `json:"tags"` - Serial string `storm:"index" json:"serial"` - - Net string `json:"net"` - Addr string `json:"addr"` - IsServer bool `json:"is_server"` - Timeout int `json:"timeout"` //TODO 改为秒 - - Register struct { - Enable bool `json:"enable"` - Length int `json:"length"` - Regex string `json:"regex"` - } `json:"register"` - - HeartBeat struct { - Enable bool `json:"enable"` - Interval int `json:"interval"` //TODO 改为秒 - Content []byte `json:"content"` - } `json:"heart_beat"` + Net NetConf `json:"net"` + Register RegisterConf `json:"register"` + HeartBeat HeartBeatConf `json:"heart_beat"` Disabled bool `json:"disabled"` Created time.Time `json:"created"` diff --git a/web/api/channel.go b/web/api/channel.go index 0d387d9f59b405db732241351395d9571dd54d98..7efd76740fe494b23fbedadac32f5063e9b4b9b5 100644 --- a/web/api/channel.go +++ b/web/api/channel.go @@ -7,7 +7,8 @@ import ( "github.com/zgwit/dtu-admin/types" ) -func channelAll(ctx *gin.Context) { + +func channels(ctx *gin.Context) { var cs []types.Channel err := storage.DB("channel").All(&cs) if err != nil { @@ -17,11 +18,6 @@ func channelAll(ctx *gin.Context) { replyOk(ctx, cs) } -func channels(ctx *gin.Context) { - cs := dtu.Channels() - replyOk(ctx, cs) -} - func channelCreate(ctx *gin.Context) { var channel types.Channel if err := ctx.ShouldBindJSON(&channel); err != nil { @@ -29,14 +25,15 @@ func channelCreate(ctx *gin.Context) { return } - //创建并启动 - c, err := dtu.CreateChannel(&channel) + err := storage.DB("channel").Save(&channel) if err != nil { replyError(ctx, err) return } - replyOk(ctx, c) + //TODO 启动服务 + + replyOk(ctx, channel) } func channelDelete(ctx *gin.Context) { @@ -45,17 +42,36 @@ func channelDelete(ctx *gin.Context) { replyError(ctx, err) return } - err := dtu.DeleteChannel(pid.Id) + + err := storage.DB("channel").DeleteStruct(&types.Channel{ID: pid.Id}) if err != nil { replyError(ctx, err) return } + //TODO 删除服务 + replyOk(ctx, nil) } + func channelModify(ctx *gin.Context) { + var channel types.Channel + if err := ctx.ShouldBindJSON(&channel); err != nil { + replyError(ctx, err) + return + } + + //TODO 不能全部字段更新,应该先取值,修改,再存入 + err := storage.DB("channel").Update(&channel) + if err != nil { + replyError(ctx, err) + return + } + + //TODO 重新启动服务 + replyOk(ctx, channel) } func channelGet(ctx *gin.Context) { @@ -64,13 +80,15 @@ func channelGet(ctx *gin.Context) { replyError(ctx, err) return } - c, err := dtu.GetChannel(pid.Id) + + var channel types.Channel + err := storage.DB("channel").One("ID", pid.Id, &channel) if err != nil { replyError(ctx, err) return } - replyOk(ctx, c) + replyOk(ctx, channel) } func channelStart(ctx *gin.Context) { diff --git a/web/api/router.go b/web/api/router.go index 2a0725893dac615d17059db56f0d3066fa8d0910..0a046f12dced5013cbd5fb5f5d0f7b92beb2c860 100644 --- a/web/api/router.go +++ b/web/api/router.go @@ -17,12 +17,12 @@ type paramSearch struct { } type paramId struct { - Id int64 `uri:"id"` + Id int `uri:"id"` } type paramId2 struct { - Id int64 `uri:"id"` - Id2 int64 `uri:"id2"` + Id int `uri:"id"` + Id2 int `uri:"id2"` } func RegisterRoutes(app *gin.RouterGroup) {