Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lab1c/app_go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# DevOps Info Service (Go)

## Overview
Compiled-language version of the DevOps info service. It exposes the same two endpoints as the Python app and keeps the JSON response structure consistent.

## Prerequisites
- Go 1.22+ installed

## Build and Run
Run directly:
```bash
go run main.go
```

Build a binary:
```bash
go build -o devops-info
./devops-info
```

Windows build/run:
```bash
go build -o devops-info.exe
.\devops-info.exe
```

Custom config examples:
```bash
PORT=8080 go run main.go
HOST=127.0.0.1 PORT=3000 go run main.go
```

## API Endpoints
- `GET /` - Service and system information
- `GET /health` - Health check

## Configuration
| Variable | Default | Description |
| --- | --- | --- |
| `HOST` | `0.0.0.0` | Bind address for the server |
| `PORT` | `5000` | Port to listen on |
Binary file added lab1c/app_go/devops-info.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions lab1c/app_go/docs/GO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Why Go

I picked Go for the compiled version because it builds fast, produces a single binary, and the standard library already has everything I need for a small HTTP service. It also starts quickly and is easy to run in a container later in the course.
79 changes: 79 additions & 0 deletions lab1c/app_go/docs/LAB01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# LAB01 - DevOps Info Service (Go)

## Implementation Notes
This version mirrors the Python API and keeps the same JSON shape. The `python_version` field is kept for compatibility, and I fill it with the Go runtime version so the field is still informative.

## Build and Run
```bash
go run main.go
```

Build a binary:
```bash
go build -o devops-info
./devops-info
```

## Binary Size Comparison
- Go binary: `<size>`
- Python app (source only): `<size>`

Quick size commands:
```bash
ls -lh devops-info
dir devops-info.exe
```

## API Endpoints
### `GET /`
Returns service, system, runtime, and request details.

Example:
```json
{
"service": {
"name": "devops-info-service",
"version": "1.0.0",
"description": "DevOps course info service",
"framework": "Go net/http"
},
"system": {
"hostname": "my-machine",
"platform": "windows",
"platform_version": "Windows_NT",
"architecture": "amd64",
"cpu_count": 12,
"python_version": "go1.22.1"
},
"runtime": {
"uptime_seconds": 42,
"uptime_human": "0 hours, 0 minutes",
"current_time": "2026-01-27T10:15:00Z",
"timezone": "UTC"
},
"request": {
"client_ip": "127.0.0.1",
"user_agent": "curl/8.5.0",
"method": "GET",
"path": "/"
},
"endpoints": [
{"path": "/", "method": "GET", "description": "Service information"},
{"path": "/health", "method": "GET", "description": "Health check"}
]
}
```

### `GET /health`
Example:
```json
{
"status": "healthy",
"timestamp": "2026-01-27T10:15:05Z",
"uptime_seconds": 47
}
```

## Challenges & Solutions
- The Go mux treats `/` as a catch-all, so I added explicit path checks to return a JSON 404 for unknown routes.
- `RemoteAddr` includes the port, so I split host/port to get a clean client IP.
Binary file added lab1c/app_go/docs/screenshots/build.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab1c/app_go/docs/screenshots/health-check.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lab1c/app_go/docs/screenshots/root.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lab1c/app_go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module devops-info-service

go 1.22
257 changes: 257 additions & 0 deletions lab1c/app_go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package main

import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"runtime"
"strings"
"time"
)

type Service struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Framework string `json:"framework"`
}

type System struct {
Hostname string `json:"hostname"`
Platform string `json:"platform"`
PlatformVersion string `json:"platform_version"`
Architecture string `json:"architecture"`
CPUCount int `json:"cpu_count"`
PythonVersion string `json:"python_version"`
}

type Runtime struct {
UptimeSeconds int `json:"uptime_seconds"`
UptimeHuman string `json:"uptime_human"`
CurrentTime string `json:"current_time"`
Timezone string `json:"timezone"`
}

type RequestInfo struct {
ClientIP string `json:"client_ip"`
UserAgent string `json:"user_agent"`
Method string `json:"method"`
Path string `json:"path"`
}

type Endpoint struct {
Path string `json:"path"`
Method string `json:"method"`
Description string `json:"description"`
}

type Response struct {
Service Service `json:"service"`
System System `json:"system"`
Runtime Runtime `json:"runtime"`
Request RequestInfo `json:"request"`
Endpoints []Endpoint `json:"endpoints"`
}

var startTime = time.Now().UTC()

func main() {
host := getenv("HOST", "0.0.0.0")
port := getenv("PORT", "5000")
addr := net.JoinHostPort(host, port)

mux := http.NewServeMux()
mux.HandleFunc("/", rootHandler)
mux.HandleFunc("/health", healthHandler)

handler := recoverMiddleware(loggingMiddleware(mux))

server := &http.Server{
Addr: addr,
Handler: handler,
ReadHeaderTimeout: 5 * time.Second,
}

log.Printf("Starting DevOps Info Service on %s", addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("server error: %v", err)
}
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
writeNotFound(w)
return
}
if r.Method != http.MethodGet {
writeMethodNotAllowed(w)
return
}

uptimeSeconds, uptimeHuman := getUptime()
now := time.Now().UTC()

hostname, _ := os.Hostname()
response := Response{
Service: Service{
Name: "devops-info-service",
Version: "1.0.0",
Description: "DevOps course info service",
Framework: "Go net/http",
},
System: System{
Hostname: hostname,
Platform: runtime.GOOS,
PlatformVersion: getPlatformVersion(),
Architecture: runtime.GOARCH,
CPUCount: runtime.NumCPU(),
PythonVersion: runtime.Version(),
},
Runtime: Runtime{
UptimeSeconds: uptimeSeconds,
UptimeHuman: uptimeHuman,
CurrentTime: now.Format(time.RFC3339),
Timezone: "UTC",
},
Request: RequestInfo{
ClientIP: getClientIP(r),
UserAgent: r.UserAgent(),
Method: r.Method,
Path: r.URL.Path,
},
Endpoints: []Endpoint{
{Path: "/", Method: "GET", Description: "Service information"},
{Path: "/health", Method: "GET", Description: "Health check"},
},
}

writeJSON(w, http.StatusOK, response)
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/health" {
writeNotFound(w)
return
}
if r.Method != http.MethodGet {
writeMethodNotAllowed(w)
return
}

uptimeSeconds, _ := getUptime()
payload := map[string]any{
"status": "healthy",
"timestamp": time.Now().UTC().Format(time.RFC3339),
"uptime_seconds": uptimeSeconds,
}

writeJSON(w, http.StatusOK, payload)
}

func getUptime() (int, string) {
seconds := int(time.Since(startTime).Seconds())
hours := seconds / 3600
minutes := (seconds % 3600) / 60
hourLabel := "hours"
if hours == 1 {
hourLabel = "hour"
}
minuteLabel := "minutes"
if minutes == 1 {
minuteLabel = "minute"
}
return seconds, fmt.Sprintf("%d %s, %d %s", hours, hourLabel, minutes, minuteLabel)
}

func getClientIP(r *http.Request) string {
if forwarded := r.Header.Get("X-Forwarded-For"); forwarded != "" {
parts := strings.Split(forwarded, ",")
return strings.TrimSpace(parts[0])
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
return host
}
return r.RemoteAddr
}

func getPlatformVersion() string {
if value := os.Getenv("OS"); value != "" {
return value
}
if data, err := os.ReadFile("/etc/os-release"); err == nil {
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "PRETTY_NAME=") {
return strings.Trim(strings.TrimPrefix(line, "PRETTY_NAME="), "\"")
}
}
}
return "unknown"
}

func writeJSON(w http.ResponseWriter, status int, payload any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(payload); err != nil {
log.Printf("json encode error: %v", err)
}
}

func writeNotFound(w http.ResponseWriter) {
writeJSON(w, http.StatusNotFound, map[string]string{
"error": "Not Found",
"message": "Endpoint does not exist",
})
}

func writeMethodNotAllowed(w http.ResponseWriter) {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{
"error": "Method Not Allowed",
"message": "Only GET is supported for this endpoint",
})
}

type statusRecorder struct {
http.ResponseWriter
status int
}

func (recorder *statusRecorder) WriteHeader(code int) {
recorder.status = code
recorder.ResponseWriter.WriteHeader(code)
}

func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
recorder := &statusRecorder{ResponseWriter: w, status: http.StatusOK}
start := time.Now()
log.Printf("Request: %s %s", r.Method, r.URL.Path)
next.ServeHTTP(recorder, r)
log.Printf("Response: %s %s -> %d (%s)", r.Method, r.URL.Path, recorder.status, time.Since(start))
})
}

func recoverMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Printf("panic recovered: %v", err)
writeJSON(w, http.StatusInternalServerError, map[string]string{
"error": "Internal Server Error",
"message": "An unexpected error occurred",
})
}
}()
next.ServeHTTP(w, r)
})
}

func getenv(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}
Loading