go_echo_wol/main.go

102 lines
2.6 KiB
Go

/*
* @Author: giaogiao giaogiao
* @Date: 2023-08-28 10:24:38
* @LastEditors: giaogiao giaogiao
* @LastEditTime: 2023-09-01 16:40:52
* @FilePath: \goweb\main.go
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
package main
import (
"embed"
"io/fs"
"log"
"net/http"
"os"
"goweb/controller"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
//go:embed app
var embededFiles embed.FS
func getFileSystem(useOS bool) http.FileSystem {
if useOS {
log.Print("using live mode")
return http.FS(os.DirFS("app"))
}
log.Print("using embed mode")
fsys, err := fs.Sub(embededFiles, "app")
if err != nil {
panic(err)
}
return http.FS(fsys)
}
// 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)
// // case wsValue.Method == "del":
// // controller.Del(&ws, wsValue.Uuid, wsValue.Data)
// 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"
assetHandler := http.FileServer(getFileSystem(useOS))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}}))
e.GET("/", echo.WrapHandler(assetHandler))
e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/ws", controller.Hello)
e.GET("/mac/:mac", controller.Get_send_start)
e.Logger.Fatal(e.Start(":1323"))
}