-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Migrated status app to golang (#17)
* minor fix * removed python apps * added go app * added docker * dockerfile updated
- Loading branch information
1 parent
24f5681
commit 7af13fc
Showing
17 changed files
with
134 additions
and
188 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
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 |
This file contains 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
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= |
This file contains 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
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"}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
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)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
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 |