From aa3e66a37c10d239941f5f4cbc88f7b63cf47d48 Mon Sep 17 00:00:00 2001 From: giaogiao Date: Wed, 13 Sep 2023 14:43:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=89=93=E5=8C=85=E7=9A=84?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=95=B0=E9=87=8F=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 1 + .gitignore | 3 +- bulid.sh | 68 +++++++++++++++++++++++-------------------- controller/ws.go | 13 ++++++++- controller/wsmain.go | 13 ++++++++- database/database.go | 16 +++++----- main_test.go | 26 ++++++++++++++--- wol.db | Bin 16384 -> 16384 bytes 8 files changed, 93 insertions(+), 47 deletions(-) diff --git a/.drone.yml b/.drone.yml index a7250ce..349fef8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -37,6 +37,7 @@ steps: - name: outdir path: /root/web commands: + - rm -rf /root/web/* - rm -rf ./app/* - cp -r /go/web/build/* ./app/ - sed -i 's?/static?/static/static?g' ./app/index.html diff --git a/.gitignore b/.gitignore index 1c2f433..db34eb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -tmp \ No newline at end of file +tmp +__debug_bin* \ No newline at end of file diff --git a/bulid.sh b/bulid.sh index 843cbfe..480c734 100644 --- a/bulid.sh +++ b/bulid.sh @@ -1,36 +1,40 @@ #!/bin/bash -CPU_architecture="darwin/amd64 -darwin/arm64 -freebsd/386 -freebsd/amd64 -freebsd/arm -freebsd/arm64 -linux/386 -linux/amd64 -linux/arm -linux/arm64 -linux/loong64 -linux/mips -linux/mips64 -linux/mips64le -linux/mipsle -linux/ppc64 -linux/ppc64le -linux/riscv64 -linux/s390x -netbsd/386 -netbsd/amd64 -netbsd/arm -netbsd/arm64 -openbsd/386 -openbsd/amd64 -openbsd/arm -openbsd/arm64 -openbsd/mips64 -windows/386 -windows/amd64 -windows/arm -windows/arm64" +# CPU_architecture="darwin/amd64 +# darwin/arm64 +# freebsd/386 +# freebsd/amd64 +# freebsd/arm +# freebsd/arm64 +# linux/386 +# linux/amd64 +# linux/arm +# linux/arm64 +# linux/loong64 +# linux/mips +# linux/mips64 +# linux/mips64le +# linux/mipsle +# linux/ppc64 +# linux/ppc64le +# linux/riscv64 +# linux/s390x +# netbsd/386 +# netbsd/amd64 +# netbsd/arm +# netbsd/arm64 +# openbsd/386 +# openbsd/amd64 +# openbsd/arm +# openbsd/arm64 +# openbsd/mips64 +# windows/386 +# windows/amd64 +# windows/arm +# windows/arm64" + +#只打包两个版本 +CPU_architecture="linux/amd64 +windows/amd64" for line in $CPU_architecture do diff --git a/controller/ws.go b/controller/ws.go index 0c29c48..571a4b3 100644 --- a/controller/ws.go +++ b/controller/ws.go @@ -52,7 +52,7 @@ func Getall(ws **websocket.Conn, uuid string) { } } -func Del(ws **websocket.Conn, uuid string, id int) { +func Del(ws **websocket.Conn, uuid string, id int64) { database.Del(id) err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, id)) if err != nil { @@ -60,3 +60,14 @@ func Del(ws **websocket.Conn, uuid string, id int) { return } } +func Add(ws **websocket.Conn, uuid string, name string, mac string) { + wol := new(database.Wol) + wol.Mac = mac + wol.Name = name + database.Insert(wol) + err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, "")) + if err != nil { + // c.Logger().Error(err) + return + } +} diff --git a/controller/wsmain.go b/controller/wsmain.go index 26f581f..5e89767 100644 --- a/controller/wsmain.go +++ b/controller/wsmain.go @@ -12,7 +12,11 @@ type MacData struct { Mac string `json:"mac"` } type DelData struct { - ID int `json:"id"` + ID int64 `json:"id"` +} +type AddData struct { + Nmae string `json:"name"` + Mac string `json:"mac"` } type TooLTT struct { Method string `json:"method"` @@ -61,6 +65,13 @@ func Hello(c echo.Context) error { return } Del(&ws, wsValue.Uuid, DelData.ID) + case "add": + var AddData AddData + if err := json.Unmarshal(json_data, &AddData); err != nil { + c.Logger().Error(err) + return + } + Add(&ws, wsValue.Uuid, AddData.Nmae, AddData.Mac) default: c.Logger().Error("没有找到方法") diff --git a/database/database.go b/database/database.go index 9efaa9c..2db470a 100644 --- a/database/database.go +++ b/database/database.go @@ -8,7 +8,7 @@ import ( var engine *xorm.Engine type Wol struct { - ID int + ID int64 Name string `xorm:"varchar(64)"` Mac string `xorm:"varchar(64)"` } @@ -18,7 +18,7 @@ func init() { file := "./wol.db" engine, err = xorm.NewEngine("sqlite3", file) if err != nil { - println(err) + println(err.Error()) } @@ -26,14 +26,14 @@ func init() { func CreateTable() int { err := engine.Sync2(new(Wol)) if err != nil { - println(err) + println(err.Error()) } return 0 } func Getallwol(everyone *[]Wol) { err := engine.Find(&(*everyone)) if err != nil { - println("Getallwol err", err) + println("Getallwol err", err.Error()) } } @@ -41,19 +41,19 @@ func Getallwoltext() { everyone := make([]Wol, 0) err := engine.Find(&everyone) if err != nil { - println("Getallwoltext", err) + println("Getallwoltext", err.Error()) } println(len(everyone)) } func Insert(wol *Wol) { - affected, err := engine.Insert(&wol) + affected, err := engine.Insert(wol) if err != nil { - println(err) + println(err.Error()) } println(affected) } -func Del(id int) { +func Del(id int64) { affected, err := engine.Where("i_d = ?", id).Delete(&Wol{}) if err != nil { println(err) diff --git a/main_test.go b/main_test.go index 24692a6..efc73af 100644 --- a/main_test.go +++ b/main_test.go @@ -1,12 +1,30 @@ package main import ( + "goweb/database" "testing" ) -func TestAdd(t *testing.T) { - if ans := 3; ans == 0 { - t.Errorf("1 + 2 expected be 3, but %d got", ans) - } +// func TestAdd(t *testing.T) { +// if database.CreateTable() != 0 { +// t.Errorf("数据库同步失败") +// } +// } + +func TestAdd_Delwol(t *testing.T) { + wol := new(database.Wol) + wol.Mac = "8F:26:58:C1:85:83" + wol.Name = "text" + database.Insert(wol) + wolarray := make([]database.Wol, 0) + database.Getallwol(&wolarray) + + for _, item := range wolarray { + if item.Name == wol.Name { + database.Del(item.ID) + return + } + } + t.Errorf("并没有找到") } diff --git a/wol.db b/wol.db index b9f48cea8033deb1a8f3e2a9f0d123c809abc19f..306a7a9d552dd76024a6166fad2b84cf3fd406da 100644 GIT binary patch delta 283 zcmZo@U~Fh$oFFYI$H2hA0>n%}%m~C>6LpM%f_hmOc==-(nE1IE___F-Hx|h9u>kqC z6B{chbMc2Vs!wcGW~yeM+|Q?wo=~2jlc1oHkQtwnpy25j;_B`iq!19~>FXF2so?Dz zso)qI;_vC_9OUZj>KCHm=MTi8K0Z1M33-XRsR;@pt`TsN+{9!g0ZpKTV1Q+FBL5fu zNeV1%jQrmj_`d_WuldFK7}yz^g&Bb^VB+O}%D~8fih=(W|0$s4K7KPvCKd)!{gTv* b5(_sgBQq;g3oBn%}%m~D66LpM%f_hmOc==-(nD}}b___FcHx|h9F;%ln zZsk{qRw~cWQBu(0