This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.go
128 lines (121 loc) · 3.28 KB
/
http.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
"github.com/maxsupermanhd/lac/v2"
)
func routineWebServer(closechan <-chan struct{}) {
m := http.NewServeMux()
m.HandleFunc("/instances", webHandleInstances)
m.HandleFunc("/reload", webHandleReload)
m.HandleFunc("/alive", webHandleAlive)
m.HandleFunc("/request", webHandleRequestRoom)
var wg sync.WaitGroup
wg.Add(1)
srv := http.Server{
Addr: cfg.GetDSString("127.0.0.1:9271", "listenAddr"),
Handler: m,
ReadTimeout: time.Second * 2,
ReadHeaderTimeout: time.Second * 2,
WriteTimeout: time.Second * 2,
IdleTimeout: time.Second * 2,
}
go func() {
err := srv.ListenAndServe()
if err != nil {
if !errors.Is(err, http.ErrServerClosed) {
log.Printf("HTTP server error: %s", err)
}
}
wg.Done()
}()
<-closechan
log.Println("Shutting down http server...")
srv.Close()
log.Println("Waiting for http server to exit...")
wg.Wait()
}
func webHandleInstances(w http.ResponseWriter, r *http.Request) {
ret := map[int64]any{}
instancesLock.Lock()
for _, v := range instances {
cfgs := []any{}
for _, c := range v.cfgs {
a, _ := c.Get()
cfgs = append(cfgs, a)
}
inst := map[string]any{
"state": v.state.Load(),
"pid": v.Pid,
"game id": v.GameId,
"lobby id": v.LobbyId,
"settings": v.Settings,
"cfgs": cfgs,
}
ret[v.Id] = inst
}
instancesLock.Unlock()
b, err := json.MarshalIndent(ret, "", "\t")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
w.Write([]byte("\n"))
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
w.Write([]byte("\n"))
}
func webHandleReload(w http.ResponseWriter, r *http.Request) {
err := cfg.SetFromFileJSON("config.json")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Config reloaded"))
w.Write([]byte("\n"))
}
func webHandleAlive(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Autohoster backend online, room creation allowed: " + fmt.Sprint(!disallowInstanceCreation.Load())))
w.Write([]byte("\n"))
}
func webHandleRequestRoom(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("HTTP room request: failed to read body: %s", err.Error())
discordPostError("HTTP room request: failed to read body: %s", err.Error())
return
}
c, err := lac.FromBytesJSON(b)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("HTTP room request: failed to unmarshal json: %s", err.Error())
discordPostError("HTTP room request: failed to unmarshal json: %s", err.Error())
return
}
gi, err := generateInstance(c)
if err != nil {
log.Printf("HTTP room request: failed to generate instance: %s", err.Error())
discordPostError("HTTP room request: failed to generate instance: %s", err.Error())
if gi != nil {
releaseInstance(gi)
}
w.WriteHeader(http.StatusInternalServerError)
return
}
gi.QueueName = ""
go spawnRunner(gi)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("Room created, join with host.wz2100-autohost.net:%d", gi.Settings.GamePort)))
w.Write([]byte("\n"))
}