-
Notifications
You must be signed in to change notification settings - Fork 0
Create stats endpoint #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
47fa41c
Add /stats endpoint and backend uptime tracking functionality
P4ST4S b3b23ce
Add memory usage tracking to Backend and update ServerPool stats retr…
P4ST4S 751882c
Add Dockerfile and main.go for backend server implementation with hea…
P4ST4S 627a8e2
Fix Dockerfile build command and implement backend stats update funct…
P4ST4S 660bc3b
Enhance documentation for GetUpTime and GetStats methods in ServerPool
P4ST4S 09b7b81
Fix stats endpoint registration and improve server list parsing
P4ST4S 0f4eb81
Refactor updateBackendStats to use a custom HTTP client with a timeout
P4ST4S 1a14f98
Refactor statsHandler to use json.Marshal for encoding response data
P4ST4S 475b9df
Implement worker pool for backend stats updates in healthCheck function
P4ST4S 17a17c5
Update README to include real-time stats monitoring instructions and …
P4ST4S 2c836e3
Add Worker Pool section to README for stats fetching optimization
P4ST4S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| FROM golang:1.25.4-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY go.mod ./ | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN CGO_ENABLED=0 GOOS=linux go build -o server cmd/server/main.go | ||
|
|
||
| FROM alpine:latest | ||
|
|
||
| WORKDIR /root/ | ||
|
|
||
| COPY --from=builder /app/server . | ||
|
|
||
| EXPOSE 80 | ||
|
|
||
| ENTRYPOINT ["./server"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| ) | ||
|
|
||
| // statsHandler returns the current status of the server pool | ||
| func statsHandler(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Content-Type", "application/json") | ||
|
|
||
| stats := serverPool.GetStats() | ||
|
|
||
| if err := json.NewEncoder(w).Encode(stats); err != nil { | ||
| http.Error(w, err.Error(), http.StatusInternalServerError) | ||
|
P4ST4S marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "runtime" | ||
| ) | ||
|
|
||
| type HealthResponse struct { | ||
| MemoryUsage uint64 `json:"memory_usage"` | ||
| } | ||
|
|
||
| func main() { | ||
| port := os.Getenv("PORT") | ||
| if port == "" { | ||
| port = "80" | ||
| } | ||
|
|
||
| http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
| fmt.Fprintf(w, "Hello from backend! I am running on %s\n", os.Getenv("HOSTNAME")) | ||
| }) | ||
|
|
||
| http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { | ||
| var m runtime.MemStats | ||
| runtime.ReadMemStats(&m) | ||
|
|
||
| resp := HealthResponse{ | ||
| MemoryUsage: m.Alloc, | ||
| } | ||
|
|
||
| w.Header().Set("Content-Type", "application/json") | ||
| json.NewEncoder(w).Encode(resp) | ||
| }) | ||
|
|
||
| log.Printf("Backend server starting on port %s...", port) | ||
| log.Fatal(http.ListenAndServe(":"+port, nil)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.