diff --git a/.gitignore b/.gitignore index bd3f8bc..c0aacec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,3 @@ -# created by virtualenv automatically -Lib -Scripts -include -lib -local -pyvenv.cfg -share - -**/.pytest_cache/ - -*.pem -*.env - - -**/__pycache__ \ No newline at end of file +*.exe +*.mod +*.sum \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 98a939c..8878dcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,8 @@ WORKDIR /app COPY ./src /app -CMD ["go", "run", "main.go"] \ No newline at end of file +RUN go mod tidy + +RUN go build -o main main.go utils.go + +CMD ["./main"] diff --git a/src/main.go b/src/main.go index d184fea..c0975b0 100644 --- a/src/main.go +++ b/src/main.go @@ -1,29 +1,20 @@ package main import ( - "log" "net/http" ) -type healthcheckHandler struct{} - -func (h *healthcheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - w.Header().Set("content-type", "text/plain") - - switch { - case r.Method == http.MethodGet: - msg := []byte("hello there") - w.WriteHeader(http.StatusOK) - w.Write(msg) - return - default: - return - } +func GenerateServerStatusHTML(r *http.Request) string { + metrics := getStatus(r) + return generateServerStatusHTML(metrics) } func main() { - mux := http.NewServeMux() - mux.Handle("/", &healthcheckHandler{}) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + html := GenerateServerStatusHTML(r) + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(html)) + }) - log.Fatal(http.ListenAndServe("0.0.0.0:8001", mux)) + http.ListenAndServe(":8001", nil) } diff --git a/src/old_code.py b/src/old_code.py deleted file mode 100644 index 87b27c6..0000000 --- a/src/old_code.py +++ /dev/null @@ -1,129 +0,0 @@ -class ServerMetrics(BaseModel): - host: str - timestamp: str - cpu_usage: str - memory_usage: str - - -def get_status(request: Request) -> ServerMetrics: - time = datetime.datetime.now(datetime.timezone.utc).isoformat() - - cpu_usage = psutil.cpu_percent() - memory_usage = psutil.virtual_memory().percent - - return ServerMetrics(host=request.client.host, timestamp=time, cpu_usage=f"{cpu_usage} %", - memory_usage=f"{memory_usage} %") - - -def format_timestamp(timestamp): - return timestamp[11:19] + " (UTC)" - - -def generate_server_status_html(metrics: ServerMetrics) -> str: - return f""" - - -
- - - - -