Compare commits

...

2 Commits

Author SHA1 Message Date
giaogiao f2218df084 增加删除功能 2023-09-08 11:21:13 +08:00
giaogiao e488ed7adf 结构化 2023-09-08 10:34:03 +08:00
6 changed files with 119 additions and 70 deletions

28
controller/ajax.go Normal file
View File

@ -0,0 +1,28 @@
package controller
import (
"fmt"
"goweb/wol"
"net"
"net/http"
"github.com/labstack/echo/v4"
)
func Get_send_start(c echo.Context) error {
mac := c.Param("mac")
println("开始发送wol 地址为" + mac)
a, err := net.Interfaces()
if err != nil {
return err
}
for i := range a {
if int(a[i].Flags) > 50 {
fmt.Printf("正在往 %s 接口发送\n", a[i].Name)
wol.WakespecifyInterfac(mac, "255.255.255.255", "9", a[i].Name)
}
}
return c.String(http.StatusOK, "已经发送请求MAC :"+mac)
}

View File

@ -51,3 +51,12 @@ func Getall(ws **websocket.Conn, uuid string) {
return
}
}
func Del(ws **websocket.Conn, uuid string, id int) {
database.Del(id)
err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, id))
if err != nil {
// c.Logger().Error(err)
return
}
}

73
controller/wsmain.go Normal file
View File

@ -0,0 +1,73 @@
package controller
import (
"encoding/json"
"fmt"
"github.com/labstack/echo/v4"
"golang.org/x/net/websocket"
)
type MacData struct {
Mac string `json:"mac"`
}
type DelData struct {
ID int `json:"id"`
}
type TooLTT struct {
Method string `json:"method"`
Data interface{} `json:"data"`
Uuid string `json:"uuid"`
}
/*
ws 分发
*/
func Hello(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
for {
msg := ""
err := websocket.Message.Receive(ws, &msg)
if err != nil {
c.Logger().Error(err)
break
}
//暂时不解析的json
var json_data json.RawMessage
wsValue := TooLTT{
Data: &json_data,
}
json.Unmarshal([]byte(msg), &wsValue)
if err := json.Unmarshal([]byte(msg), &wsValue); err != nil {
fmt.Println(err)
}
switch wsValue.Method {
case "start":
var startdata MacData
if err := json.Unmarshal(json_data, &startdata); err != nil {
c.Logger().Error(err)
return
}
Send_start(startdata.Mac, &ws, wsValue.Uuid)
case "getall":
Getall(&ws, wsValue.Uuid)
case "del":
var DelData DelData
if err := json.Unmarshal(json_data, &DelData); err != nil {
c.Logger().Error(err)
return
}
Del(&ws, wsValue.Uuid, DelData.ID)
default:
c.Logger().Error("没有找到方法")
}
fmt.Printf("请求方法:%s uuid:%s \n", wsValue.Method, wsValue.Uuid)
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}

View File

@ -31,7 +31,6 @@ func CreateTable() int {
return 0
}
func Getallwol(everyone *[]Wol) {
// everyone := make([]wol, 0)
err := engine.Find(&(*everyone))
if err != nil {
println("Getallwol err", err)
@ -54,3 +53,10 @@ func Insert(wol *Wol) {
}
println(affected)
}
func Del(id int) {
affected, err := engine.Where("i_d = ?", id).Delete(&Wol{})
if err != nil {
println(err)
}
println(affected, "ok")
}

71
main.go
View File

@ -10,20 +10,15 @@ package main
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"goweb/controller"
"goweb/wol"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/net/websocket"
)
//go:embed app
@ -44,68 +39,6 @@ func getFileSystem(useOS bool) http.FileSystem {
return http.FS(fsys)
}
func get_send_start(c echo.Context) error {
mac := c.Param("mac")
println("开始发送wol 地址为" + mac)
a, err := net.Interfaces()
if err != nil {
return err
}
for i := range a {
if int(a[i].Flags) > 50 {
fmt.Printf("正在往 %s 接口发送\n", a[i].Name)
wol.WakespecifyInterfac(mac, "255.255.255.255", "9", a[i].Name)
}
}
return c.String(http.StatusOK, "已经发送请求MAC :"+mac)
}
func hello(c echo.Context) error {
type Data struct {
Mac string `json:"mac"`
}
type TooLTT struct {
Method string `json:"method"`
Data Data `json:"data"`
Uuid string `json:"uuid"`
}
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
var wsValue TooLTT
for {
// Write
// timeUnix := time.Now().Unix()
// output, _ := json.Marshal(&wsValue)
// err := websocket.Message.Send(ws, "Hello, Client!"+string(output)+" time is :"+strconv.FormatInt(timeUnix, 10))
// if err != nil {
// c.Logger().Error(err)
// }
// Read
msg := ""
err := websocket.Message.Receive(ws, &msg)
if err != nil {
c.Logger().Error(err)
break
}
json.Unmarshal([]byte(msg), &wsValue)
switch {
case wsValue.Method == "start":
controller.Send_start(wsValue.Data.Mac, &ws, wsValue.Uuid)
case wsValue.Method == "getall":
controller.Getall(&ws, wsValue.Uuid)
default:
fmt.Printf("error")
}
fmt.Printf("%s\n", msg)
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}
func main() {
e := echo.New()
useOS := len(os.Args) > 1 && os.Args[1] == "live"
@ -116,7 +49,7 @@ func main() {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/ws", hello)
e.GET("/mac/:mac", get_send_start)
e.GET("/ws", controller.Hello)
e.GET("/mac/:mac", controller.Get_send_start)
e.Logger.Fatal(e.Start(":1323"))
}

BIN
wol.db

Binary file not shown.