Files
api-gateway/main.go
2026-04-09 16:53:42 +03:00

96 lines
2.4 KiB
Go

package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
func env(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
type status struct {
Service string `json:"service"`
OK bool `json:"ok"`
Data string `json:"data"`
}
func fetchUsers(usersURL string) status {
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Get(usersURL + "/users")
if err != nil {
return status{"users-api", false, fmt.Sprintf("error: %v", err)}
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return status{"users-api", false, fmt.Sprintf("status %d: %s", resp.StatusCode, body)}
}
return status{"users-api", true, string(body)}
}
func main() {
usersURL := env("USERS_API_URL", "http://users-api:8080")
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthz" {
w.WriteHeader(200)
return
}
users := fetchUsers(usersURL)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<head>
<title>API Gateway</title>
<style>
body { font-family: 'JetBrains Mono', monospace; background: #0a0a0a; color: #e5e5e5; padding: 40px; }
h1 { color: #60a5fa; }
.svc { margin: 16px 0; padding: 16px; border-radius: 8px; border: 1px solid #333; }
.ok { border-color: #166534; background: #052e16; }
.fail { border-color: #7f1d1d; background: #1c0a0a; }
.label { font-size: 14px; color: #9ca3af; }
.url { font-size: 11px; color: #6b7280; margin-top: 2px; }
.data { font-size: 13px; margin-top: 8px; color: #d1d5db; }
.ok .data { color: #4ade80; }
.fail .data { color: #f87171; }
.time { color: #6b7280; font-size: 12px; margin-top: 24px; }
</style>
</head>
<body>
<h1>API Gateway</h1>
<p style="color:#9ca3af">Service-to-service communication test</p>
`)
cls := "ok"
if !users.OK {
cls = "fail"
}
fmt.Fprintf(w, ` <div class="svc %s">
<div class="label">%s</div>
<div class="url">→ %s</div>
<div class="data">%s</div>
</div>
`, cls, users.Service, usersURL, users.Data)
fmt.Fprintf(w, ` <div class="time">Checked at %s</div>
</body></html>`, time.Now().Format(time.RFC3339))
})
port := env("PORT", "8080")
log.Printf("api-gateway listening on :%s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}