Created
May 26, 2026 19:58
-
-
Save copyleftdev/2b753d2a816670e9c8c64eb5ef8f8ee0 to your computer and use it in GitHub Desktop.
micro-containers: Go HTTP server — runtime metadata at GET /health
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
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "net/http" | |
| "os" | |
| "runtime" | |
| "time" | |
| ) | |
| type Health struct { | |
| Status string `json:"status"` | |
| Runtime string `json:"runtime"` | |
| GoVersion string `json:"go_version"` | |
| Arch string `json:"arch"` | |
| PID int `json:"pid"` | |
| Uptime string `json:"uptime"` | |
| } | |
| var boot = time.Now() | |
| func main() { | |
| name := getenv("RUNTIME_NAME", "unknown") | |
| port := getenv("PORT", "8080") | |
| mux := http.NewServeMux() | |
| mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { | |
| w.Header().Set("Content-Type", "application/json") | |
| json.NewEncoder(w).Encode(Health{ | |
| Status: "ok", | |
| Runtime: name, | |
| GoVersion: runtime.Version(), | |
| Arch: runtime.GOARCH, | |
| PID: os.Getpid(), | |
| Uptime: time.Since(boot).Round(time.Millisecond).String(), | |
| }) | |
| }) | |
| mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintln(w, "micro-containers showcase — GET /health") | |
| }) | |
| fmt.Printf("[%s] listening on :%s\n", name, port) | |
| if err := http.ListenAndServe(":"+port, mux); err != nil { | |
| fmt.Fprintln(os.Stderr, err) | |
| os.Exit(1) | |
| } | |
| } | |
| func getenv(key, fallback string) string { | |
| if v := os.Getenv(key); v != "" { | |
| return v | |
| } | |
| return fallback | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment