-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
92 lines (83 loc) · 2.15 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"github.com/Buco7854/http-manager/errors"
"github.com/Buco7854/http-manager/router"
"github.com/Buco7854/http-manager/serializers"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"time"
)
const shutdownTimeout = 10 * time.Second
func main() {
r := new(router.Router)
r.AddRoute("GET", "/shutdown", func(writer http.ResponseWriter, request *http.Request) {
if err := exec.Command("cmd", "/C", "shutdown", "/s", "/t", "10").Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exitStatus := exitError.ExitCode()
if exitStatus == 1190 {
errors.JSONError(
writer,
"Sorry, a system shutdown is already scheduled. Please wait until the system shuts down automatically.",
http.StatusConflict,
)
return
}
}
errors.JSONError(
writer,
"An error has occurred while trying to initiate the shutdown procedure",
http.StatusInternalServerError,
)
return
}
serializers.JsonResponse(
writer,
struct {
Detail string `json:"detail"`
}{
Detail: "Shutdown procedure successfully initialized",
},
)
})
r.AddRoute("GET", "/launch-apex-legends", func(writer http.ResponseWriter, request *http.Request) {
if err := exec.Command("cmd", "/C", "start", "steam://rungameid/1172470").Run(); err != nil {
errors.JSONError(
writer,
"An error has occurred while trying to launch the game",
http.StatusInternalServerError,
)
return
}
serializers.JsonResponse(
writer,
struct {
Detail string `json:"detail"`
}{
Detail: "Successfully Launched Apex Legends",
},
)
})
server := &http.Server{
Addr: ":8080",
Handler: r,
}
log.Printf("Server listening on %v", server.Addr)
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ERROR on ListenAndServe(): %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Println("ERROR on Shutdown()", err)
}
log.Println("Server Shutdown Complete")
}