结构化

This commit is contained in:
giaogiao 2023-09-08 10:34:03 +08:00
parent 1c0ceb29e7
commit e488ed7adf
6 changed files with 171 additions and 67 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,18 @@ func Getall(ws **websocket.Conn, uuid string) {
return return
} }
} }
func Del(ws **websocket.Conn, uuid string, id int) {
wol := new(database.Wol)
wol.ID = id
database.Del()
// if err != nil {
// fmt.Println("marshal failed!", err)
// return
// }
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 return 0
} }
func Getallwol(everyone *[]Wol) { func Getallwol(everyone *[]Wol) {
// everyone := make([]wol, 0)
err := engine.Find(&(*everyone)) err := engine.Find(&(*everyone))
if err != nil { if err != nil {
println("Getallwol err", err) println("Getallwol err", err)
@ -54,3 +53,13 @@ func Insert(wol *Wol) {
} }
println(affected) println(affected)
} }
func Del() {
println("del orm")
wol1 := new(Wol)
wol1.Name = "00:70:0a:f5:9a:a3"
affected, err := engine.Delete(&wol1)
if err != nil {
println(err)
}
println(affected, "ok")
}

111
main.go
View File

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

BIN
wol.db

Binary file not shown.