Skip to content

Commit 1f034ad

Browse files
authored
Merge branch 'Prod' into Backend-Modular
2 parents fd34880 + f2e5332 commit 1f034ad

12 files changed

Lines changed: 131 additions & 60 deletions

File tree

.github/workflows/go-ci-cd.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: Go CI/CD
33
on:
44
push:
55
branches:
6-
- main
6+
- Prod # Trigger pipeline only when code is pushed to the Prod branch
77
pull_request:
88
branches:
9-
- main
9+
- Prod # Also trigger pipeline for pull requests targeting the Prod branch
1010

1111
jobs:
1212
test:
@@ -20,7 +20,7 @@ jobs:
2020
- name: Set up Go
2121
uses: actions/setup-go@v4
2222
with:
23-
go-version: 1.20
23+
go-version: 1.23.4 # Use the same Go version as your local setup
2424

2525
- name: Install dependencies
2626
run: |

backend/monitor

304 Bytes
Binary file not shown.

backend/server.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
alertEnabled bool
3131
mu sync.Mutex
3232
defaultLimit = setDefaultLimit()
33+
shutdownChan = make(chan struct{})
3334
)
3435

3536
func setDefaultLimit() SetLimit {
@@ -105,6 +106,16 @@ func sendAlert(resource string, usage float64) {
105106
}
106107
}
107108

109+
func shutdownHandler(w http.ResponseWriter, r *http.Request) {
110+
if r.Method == http.MethodPost {
111+
log.Println("Received shutdown request")
112+
close(shutdownChan) // Trigger shutdown
113+
w.WriteHeader(http.StatusOK)
114+
} else {
115+
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
116+
}
117+
}
118+
108119
func toggleAlertHandler(w http.ResponseWriter, r *http.Request) {
109120
mu.Lock()
110121
defer mu.Unlock()
@@ -169,33 +180,41 @@ func monitorResources() {
169180
cpuUsage := getCPUUsage()
170181
memUsage := getMemoryUsage()
171182
diskUsage := getDiskUsage()
172-
173-
// Check if the resource usage exceeds thresholds and send notifications
174183
sendAlert("CPU", cpuUsage)
175184
sendAlert("Memory", memUsage)
176185
sendAlert("Disk", diskUsage)
177186

178187
time.Sleep(5 * time.Second)
179188
}
180189
}
181-
182190
func main() {
183-
// Initial alert state
184191
alertEnabled = true
185192
limit := setDefaultLimit()
186193
log.Println(limit)
194+
server := &http.Server{
195+
Addr: ":8080",
196+
}
187197

188-
// Set up routes
189198
http.HandleFunc("/toggle-alerts", toggleAlertHandler)
190199
http.HandleFunc("/resource-usage", resourceUsageHandler)
191200
http.HandleFunc("/limit-changer", toggleLimitHandler)
201+
http.HandleFunc("/shutdown", shutdownHandler)
192202

193-
// Start monitoring resources in the background
194203
go monitorResources()
195204

196-
// Start the backend server
197-
log.Println("Starting backend server on port 8080...")
198-
if err := http.ListenAndServe(":8080", nil); err != nil {
199-
log.Fatal("Error starting server: ", err)
205+
go func() {
206+
log.Println("Starting backend server on port 8080...")
207+
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
208+
log.Fatalf("Error starting server: %v", err)
209+
}
210+
}()
211+
212+
<-shutdownChan
213+
214+
log.Println("Shutting down the server...")
215+
if err := server.Close(); err != nil {
216+
log.Fatalf("Server shutdown failed: %v", err)
200217
}
218+
219+
log.Println("Server stopped.")
201220
}
Binary file not shown.

frontend/widget.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"net/http"
9+
"os"
910
"strconv"
1011
"time"
1112

@@ -84,6 +85,17 @@ func changeLimits(cpuThreshold, memoryThreshold, diskThreshold float64) error {
8485
return nil
8586
}
8687

88+
// Function to shut down the backend server
89+
func shutdownServer() error {
90+
resp, err := http.Post("http://localhost:8080/shutdown", "application/json", nil)
91+
if err != nil {
92+
return fmt.Errorf("error sending shutdown request: %v", err)
93+
}
94+
defer resp.Body.Close()
95+
96+
return nil
97+
}
98+
8799
func createGraphWindow(a fyne.App) fyne.Window {
88100
w := a.NewWindow("SysGuard")
89101

@@ -144,6 +156,17 @@ func createGraphWindow(a fyne.App) fyne.Window {
144156
}
145157
})
146158

159+
// Button to close the app
160+
closeAppButton := widget.NewButton("Close App", func() {
161+
if err := shutdownServer(); err != nil {
162+
log.Println("Error shutting down server:", err)
163+
} else {
164+
log.Println("Server shut down successfully. Exiting application...")
165+
a.Quit()
166+
os.Exit(0) // Exit the frontend application
167+
}
168+
})
169+
147170
go func() {
148171
for {
149172
usage, err := fetchResourceUsage()
@@ -174,6 +197,7 @@ func createGraphWindow(a fyne.App) fyne.Window {
174197
memEntry,
175198
diskEntry,
176199
changeLimitsButton,
200+
closeAppButton, // Add the "Close App" button
177201
)
178202

179203
w.SetContent(content)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ require (
3131
github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect
3232
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
3333
github.com/pmezard/go-difflib v1.0.0 // indirect
34-
github.com/progrium/darwinkit v0.5.0 // indirect
3534
github.com/rogpeppe/go-internal v1.13.1 // indirect
3635
github.com/rymdport/portal v0.3.0 // indirect
3736
github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
255255
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
256256
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
257257
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
258-
github.com/progrium/darwinkit v0.5.0 h1:SwchcMbTOG1py3CQsINmGlsRmYKdlFrbnv3dE4aXA0s=
259-
github.com/progrium/darwinkit v0.5.0/go.mod h1:PxQhZuftnALLkCVaR8LaHtUOfoo4pm8qUDG+3C/sXNs=
260258
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
261259
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
262260
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

main.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

main.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Get the script's directory
4+
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
5+
6+
# Start backend monitor
7+
echo "Starting backend monitor..."
8+
if [ -f "$SCRIPT_DIR/backend/monitor" ]; then
9+
echo "Found 'monitor' executable in $SCRIPT_DIR/backend."
10+
"$SCRIPT_DIR/backend/monitor" &
11+
backend_pid=$!
12+
echo "Backend monitor started with PID: $backend_pid"
13+
else
14+
echo "Error: 'monitor' file not found in $SCRIPT_DIR/backend."
15+
exit 1
16+
fi
17+
18+
# Start frontend SysGuard
19+
echo "Starting frontend SysGuard..."
20+
if [ -f "$SCRIPT_DIR/frontend/monitor" ]; then
21+
echo "Found 'sysguard' executable in $SCRIPT_DIR/frontend."
22+
"$SCRIPT_DIR/frontend/monitor" &
23+
frontend_pid=$!
24+
echo "Frontend SysGuard started with PID: $frontend_pid"
25+
else
26+
echo "Error: 'sysguard' file not found in $SCRIPT_DIR/frontend."
27+
echo "Stopping backend monitor..."
28+
kill $backend_pid
29+
exit 1
30+
fi
31+
32+
# Wait for both processes to complete
33+
echo "Both backend and frontend are running. Press Ctrl+C to stop."
34+
trap "echo 'Stopping processes...'; kill $backend_pid $frontend_pid" EXIT
35+
wait

makefile

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ GOFMT = gofmt
55

66
BACKEND_DIR = backend
77
FRONTEND_DIR = frontend
8+
BACKEND_LOG = backend.log
9+
BACKEND_PORT = 8080 # Update this if needed
810

911
deps:
1012
@echo "Installing Go dependencies..."
@@ -18,25 +20,39 @@ build-frontend:
1820
@echo "Building the frontend application..."
1921
$(GO) build -o $(FRONTEND_DIR)/$(BINARY_NAME) $(FRONTEND_DIR)/widget.go
2022

21-
run-backend: build-backend
23+
build: build-backend build-frontend
24+
25+
run-backend:
2226
@echo "Running the backend server..."
23-
$(BACKEND_DIR)/$(BINARY_NAME)
27+
@$(BACKEND_DIR)/$(BINARY_NAME) > $(BACKEND_LOG) 2>&1 & # Run in background
28+
@echo "Backend started in the background."
29+
30+
wait-backend:
31+
@echo "Waiting for backend to start..."
32+
@while ! nc -z localhost $(BACKEND_PORT); do sleep 1; done
33+
@echo "Backend is running!"
2434

25-
run-frontend: build-frontend
35+
run-frontend:
2636
@echo "Running the frontend application..."
27-
$(FRONTEND_DIR)/$(BINARY_NAME)
37+
@$(FRONTEND_DIR)/$(BINARY_NAME)
38+
39+
run-all: deps build run-backend wait-backend run-frontend
40+
41+
run-built: run-backend wait-backend run-frontend
2842

2943
clean:
3044
@echo "Cleaning up..."
31-
rm -f $(BACKEND_DIR)/$(BINARY_NAME) $(FRONTEND_DIR)/$(BINARY_NAME)
45+
rm -f $(BACKEND_DIR)/$(BINARY_NAME) $(FRONTEND_DIR)/$(BINARY_NAME) $(BACKEND_LOG)
3246

3347
help:
3448
@echo "Makefile for Go application"
3549
@echo ""
3650
@echo "Available targets:"
37-
@echo " build - Build both backend and frontend applications"
38-
@echo " run-backend - Build and run the backend server"
39-
@echo " run-frontend - Build and run the frontend application"
40-
@echo " clean - Remove the generated binaries"
41-
@echo " deps - Install Go dependencies and tidy up"
42-
@echo " help - Show this help message"
51+
@echo " build - Build both backend and frontend applications"
52+
@echo " run-backend - Build and run the backend server"
53+
@echo " wait-backend - Wait until the backend is ready using a real check"
54+
@echo " run-frontend - Build and run the frontend application"
55+
@echo " run-all - Install deps, build everything, then run backend & frontend"
56+
@echo " clean - Remove generated binaries and logs"
57+
@echo " deps - Install Go dependencies and tidy up"
58+
@echo " help - Show this help message"

0 commit comments

Comments
 (0)