Auto Create Workers in Database

This commit is contained in:
Samuel Lorch 2024-05-09 04:42:24 +02:00
parent 0155c36af1
commit 4e0df234f1

View file

@ -36,6 +36,21 @@ func handleWorkerWebsocket(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented) w.WriteHeader(http.StatusNotImplemented)
return return
} }
// Connection ID
uuid := r.Header.Get(constants.UUID_HEADER)
if uuid == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
name := r.Header.Get(constants.NAME_HEADER)
_, err := db.Exec(r.Context(), "INSERT INTO workers (id, name) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET name = $2", uuid, name)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
slog.ErrorContext(r.Context(), "Error Upserting Worker", "err", err)
return
}
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
InsecureSkipVerify: true, InsecureSkipVerify: true,
}) })
@ -45,9 +60,6 @@ func handleWorkerWebsocket(w http.ResponseWriter, r *http.Request) {
} }
defer c.CloseNow() defer c.CloseNow()
// Connection ID
uuid := r.Header.Get(constants.UUID_HEADER)
// Track Connection // Track Connection
func() { func() {
WorkersMutex.Lock() WorkersMutex.Lock()
@ -70,7 +82,7 @@ func handleWorkerWebsocket(w http.ResponseWriter, r *http.Request) {
} }
Workers[uuid] = &Worker{ Workers[uuid] = &Worker{
Name: r.Header.Get(constants.NAME_HEADER), Name: name,
Address: r.RemoteAddr, Address: r.RemoteAddr,
Conn: c, Conn: c,
Connected: true, Connected: true,