diff --git a/controller/wol_item.go b/controller/wol_item.go index cd50d78..ecd0027 100644 --- a/controller/wol_item.go +++ b/controller/wol_item.go @@ -3,6 +3,7 @@ package controller import ( "encoding/json" "fmt" + "goweb/database" "goweb/res" "goweb/wol" "net" @@ -36,21 +37,16 @@ func Send_start(mac string, ws **websocket.Conn) { } func Getall(ws **websocket.Conn) { - type wol struct { - ID int - Name string - Address string - } - var wolarray []wol - wol_item := new(wol) - wol_item.ID = 1 - wol_item.Name = "电脑" - wol_item.Address = "10:7B:44:80:F4:6A" - wol_item2 := new(wol) - wol_item2.ID = 2 - wol_item2.Name = "aa" - wol_item2.Address = "10:7B:44:80:F4:6A" - wolarray = append(wolarray, *wol_item, *wol_item2) + wolarray := make([]database.Wol, 0) + // wol_item := new(wol) + // wol_item.ID = 1 + // wol_item.Name = "电脑" + // wol_item.Address = "10:7B:44:80:F4:6A" + // wol_item2 := new(wol) + // wol_item2.ID = 2 + // wol_item2.Name = "aa" + // wol_item2.Address = "10:7B:44:80:F4:6A" + // wolarray = append(wolarray, *wol_item, *wol_item2) v, err := json.Marshal(wolarray) if err != nil { fmt.Println("marshal failed!", err) diff --git a/database/database.go b/database/database.go index 54f1de6..123e91b 100644 --- a/database/database.go +++ b/database/database.go @@ -1,12 +1,18 @@ package database import ( - _ "modernc.org/sqlite" + _ "github.com/mattn/go-sqlite3" "xorm.io/xorm" ) var engine *xorm.Engine +type Wol struct { + ID int + Name string `xorm:"varchar(64)"` + Mac string `xorm:"varchar(64)"` +} + func init() { var err error file := "./wol.db" @@ -15,4 +21,19 @@ func init() { panic(err) } + +} +func CreateTable() int { + err := engine.Sync2(new(Wol)) + if err != nil { + panic(err) + } + return 0 +} +func Getallwol(everyone *[]Wol) { + // everyone := make([]wol, 0) + err := engine.Find(&everyone) + if err != nil { + panic(err) + } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..3df7dae --- /dev/null +++ b/main_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "goweb/database" + "testing" +) + +func TestAdd(t *testing.T) { + if ans := database.CreateTable(); ans == 0 { + t.Errorf("1 + 2 expected be 3, but %d got", ans) + } + +} diff --git a/wol.db b/wol.db index 5ae2447..c518437 100644 Binary files a/wol.db and b/wol.db differ