Small web server to send Wake-on-LAN requests to its local network
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.3 KiB

package web
import (
"errors"
"net/http"
"strconv"
"strings"
"git.ulra.eu/adro/miniwol/config"
"git.ulra.eu/adro/miniwol/lib"
"github.com/labstack/echo/v4"
)
func add(c echo.Context) error {
device := config.Device{}
err := c.Bind(&device)
if err != nil {
return err
}
config.Config.Devices = append(config.Config.Devices, device)
config.Save()
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
func wake(c echo.Context) error {
index, err := strconv.Atoi(c.FormValue("Index"))
if err != nil {
return err
}
for i, device := range config.Config.Devices {
if i == index {
if !strings.Contains(device.IP, ":") {
device.IP += ":9"
}
err := lib.SendPacket(":0", device.IP, device.MAC)
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
}
return errors.New("device not found")
}
func remove(c echo.Context) error {
index, err := strconv.Atoi(c.FormValue("Index"))
if err != nil {
return err
}
for i := range config.Config.Devices {
if i == index {
config.Config.Devices = append(config.Config.Devices[:i], config.Config.Devices[i+1:]...)
err := config.Save()
if err != nil {
return err
}
return c.Redirect(http.StatusSeeOther, "/")
}
}
return errors.New("device not found")
}