forked from daBONDi/go-rest-wol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
59 lines (48 loc) · 1.61 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Rest API Implementations
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
)
//restWakeUpWithComputerName - REST Handler for Processing URLS /api/computer/<computerName>
func restWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
computerName := vars["computerName"]
var result WakeUpResponseObject
result.Success = false
// Ensure computerName is not empty
if computerName == "" {
result.Message = "Empty Computername is not allowed"
result.ErrorObject = nil
w.WriteHeader(http.StatusBadRequest)
// Computername is empty
} else {
// Get Computer from List
for _, c := range ComputerList {
if c.Name == computerName {
// We found the Computername
if err := SendMagicPacket(c.Mac, c.BroadcastIPAddress, ""); err != nil {
// We got an internal Error on SendMagicPacket
w.WriteHeader(http.StatusInternalServerError)
result.Success = false
result.Message = "Internal error on Sending the Magic Packet"
result.ErrorObject = err
} else {
// Horray we send the WOL Packet succesfully
result.Success = true
result.Message = fmt.Sprintf("Succesfully Wakeup Computer %s with Mac %s on Broadcast IP %s", c.Name, c.Mac, c.BroadcastIPAddress)
result.ErrorObject = nil
}
}
}
if result.Success == false && result.ErrorObject == nil {
// We could not find the Computername
w.WriteHeader(http.StatusNotFound)
result.Message = fmt.Sprintf("Computername %s could not be found", computerName)
}
}
json.NewEncoder(w).Encode(result)
}