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
7 changes: 7 additions & 0 deletions lab2c/app_go/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.exe
*.log
.git/
.gitignore
.idea/
.vscode/
docs/
21 changes: 21 additions & 0 deletions lab2c/app_go/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:1.22 AS builder

WORKDIR /src

COPY go.mod ./
RUN go mod download

COPY main.go ./
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o devops-info

FROM gcr.io/distroless/static-debian12:nonroot

WORKDIR /app
COPY --from=builder /src/devops-info /app/devops-info

ENV HOST=0.0.0.0 \
PORT=5000

EXPOSE 5000

ENTRYPOINT ["/app/devops-info"]
41 changes: 41 additions & 0 deletions lab2c/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 |
131 changes: 131 additions & 0 deletions lab2c/app_go/docs/LAB02.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lab2c/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 lab2c/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
}
12 changes: 12 additions & 0 deletions lab2c/app_python/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
__pycache__/
*.py[cod]
*.log
venv/
.venv/
.env
.git/
.gitignore
.idea/
.vscode/
docs/
tests/
14 changes: 14 additions & 0 deletions lab2c/app_python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
__pycache__/
*.py[cod]
*.log
venv/
.venv/
.env

# IDE
.idea/
.vscode/

# OS
.DS_Store
Thumbs.db
19 changes: 19 additions & 0 deletions lab2c/app_python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM python:3.13-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

WORKDIR /app

RUN useradd -m -u 10001 appuser

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY --chown=appuser:appuser app.py .

USER appuser

EXPOSE 5000

CMD ["python", "app.py"]
Loading