Skip to content

Commit

Permalink
feat: Migrated status app to golang (#17)
Browse files Browse the repository at this point in the history
* minor fix

* removed python apps

* added go app

* added docker

* dockerfile updated
  • Loading branch information
diegolagospagopa authored Dec 22, 2024
1 parent 24f5681 commit 7af13fc
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 188 deletions.
8 changes: 0 additions & 8 deletions .flake8

This file was deleted.

1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": false,
"python.linting.enabled": true,
"java.compile.nullAnalysis.mode": "automatic",
}
56 changes: 50 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
FROM python:3.10-alpine
# Build stage
FROM golang:1.21-alpine AS builder
WORKDIR /build

WORKDIR /code
# Add necessary build tools
RUN apk add --no-cache make build-base

COPY ./requirements.txt /code/requirements.txt
# Install dependencies first (better layer caching)
COPY go.mod go.sum ./
RUN echo "📦 Installing dependencies..." && \
go mod download

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Copy source code
COPY . .

COPY ./app /code/app
# Build the application
RUN echo "🔨 Building application..." && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags='-w -s -extldflags "-static"' \
-o status-service

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
# Security scan (optional but recommended)
RUN echo "🔍 Running security checks..." && \
go vet ./...

# Final stage
FROM alpine:3.19
WORKDIR /app

# Add non-root user
RUN echo "🔒 Creating non-root user..." && \
addgroup -S appgroup && \
adduser -S appuser -G appgroup

# Copy binary from builder
COPY --from=builder /build/status-service .

# Set ownership
RUN chown -R appuser:appgroup /app

# Use non-root user
USER appuser

# Container configuration
EXPOSE 8000
ENV TZ=Europe/Rome \
APP_USER=appuser

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8000/health || exit 1

# Run application
CMD echo "🚀 Starting status-service..." && \
./status-service
25 changes: 0 additions & 25 deletions app/main.py

This file was deleted.

13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ services:
restart: always
ports:
- "8000:8000"
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:8080/"]
interval: 30s
timeout: 10s
retries: 3
deploy:
resources:
limits:
cpus: '0.1'
memory: 6M
reservations:
cpus: '0.1'
memory: 6M
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module status-service

go 1.21

require github.com/gorilla/mux v1.8.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
34 changes: 34 additions & 0 deletions handlers/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package handlers

import (
"encoding/json"
"net/http"
)

type Response struct {
Result string `json:"result"`
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{Result: "ok"})
}

func StatusHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{Result: "ok"})
}

func HealthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{Result: "ok"})
}

func HealthzHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{Result: "ok"})
}
23 changes: 0 additions & 23 deletions helm/devopslab/diego/.helmignore

This file was deleted.

6 changes: 0 additions & 6 deletions helm/devopslab/diego/Chart.lock

This file was deleted.

10 changes: 0 additions & 10 deletions helm/devopslab/diego/Chart.yaml

This file was deleted.

96 changes: 0 additions & 96 deletions helm/devopslab/diego/values-dev.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions launch-debug.sh

This file was deleted.

6 changes: 0 additions & 6 deletions launch.sh

This file was deleted.

22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"
"net/http"

"status-service/handlers"

"github.com/gorilla/mux"
)

func main() {
r := mux.NewRouter()

r.HandleFunc("/", handlers.HomeHandler)
r.HandleFunc("/status", handlers.StatusHandler)
r.HandleFunc("/health", handlers.HealthHandler)
r.HandleFunc("/healthz", handlers.HealthzHandler)

log.Printf("Server starting on port 8000")
log.Fatal(http.ListenAndServe(":8000", r))
}
2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

7 changes: 7 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

echo "Building Go application..."
go build -o status-service

echo "Starting server on port 8000..."
./status-service

0 comments on commit 7af13fc

Please sign in to comment.