From 709b0fbe211acc1667b725d13b274a3ec1b52f1b Mon Sep 17 00:00:00 2001 From: ahoemann Date: Tue, 28 Jan 2025 07:33:47 +0000 Subject: [PATCH 1/4] feat: convert to golang --- app.py | 93 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 60 insertions(+), 33 deletions(-) diff --git a/app.py b/app.py index 29999af..2520cfc 100755 --- a/app.py +++ b/app.py @@ -1,42 +1,69 @@ -#!/usr/bin/env python3 -from flask import Flask, render_template, redirect, url_for -import requests -import os +package main -app = Flask(__name__) +import ( + "net/http" + "os" + "html/template" + "log" +) -@app.route("/imprint") -def root(): - return render_template("imprint.html") +var templates = template.Must(template.ParseGlob("*.html")) -@app.route("/about") -def about(): - return render_template("about.html") +func main() { + mode := os.Getenv("DEBUG_MODE") + if mode == "true" { + log.Println("Debug mode is on") + } + http.HandleFunc("/imprint", imprint) + http.HandleFunc("/about", about) + http.HandleFunc("/", homepage) + http.HandleFunc("/monitoring", monitoring) + log.Fatal(http.ListenAndServe(":8080", nil)) +} -@app.route("/") -def homepage(): - return render_template("homepage.html") +func imprint(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, "imprint.html") +} -@app.route("/monitoring") -def monitoring(): - git_url = os.environ['GIT_URL'] - website_url = os.environ['WEBSITE_URL'] - return render_template("monitoring.html", git_status = get_status(git_url), website_status = get_status(website_url) ) +func about(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, "about.html") +} -def get_status_code(link): - requested_site = requests.get(link) - return requested_site.status_code +func homepage(w http.ResponseWriter, r *http.Request) { + renderTemplate(w, "homepage.html") +} -def get_status(link): - try: - match get_status_code(link): - case 200: - return "ONLINE" - case _: - return "ERROR" - except: +func monitoring(w http.ResponseWriter, r *http.Request) { + gitURL := os.Getenv("GIT_URL") + websiteURL := os.Getenv("WEBSITE_URL") + gitStatus := getStatus(gitURL) + websiteStatus := getStatus(websiteURL) + data := struct { + GitStatus string + WebsiteStatus string + }{ + GitStatus: gitStatus, + WebsiteStatus: websiteStatus, + } + renderTemplate(w, "monitoring.html", data) +} + +func renderTemplate(w http.ResponseWriter, tmpl string, data ...interface{}) { + err := templates.ExecuteTemplate(w, tmpl, data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + +func getStatus(link string) string { + resp, err := http.Get(link) + if err != nil { return "OFFLINE" + } + defer resp.Body.Close() + if resp.StatusCode == 200 { + return "ONLINE" + } + return "ERROR" +} -if __name__ == "__main__": - mode = os.environ['DEBUG_MODE'] - app.run(debug=mode) From b83a60f8222c23c8e8b217df57042ab253e24dc8 Mon Sep 17 00:00:00 2001 From: ahoemann Date: Tue, 28 Jan 2025 07:37:56 +0000 Subject: [PATCH 2/4] Update website.go --- app.py => website.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app.py => website.go (100%) diff --git a/app.py b/website.go similarity index 100% rename from app.py rename to website.go From 9f8f0c94ff6b0901a7614ff634e26d45e2b6868d Mon Sep 17 00:00:00 2001 From: ahoemann Date: Tue, 28 Jan 2025 07:43:02 +0000 Subject: [PATCH 3/4] Update Containerfile --- Containerfile | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Containerfile b/Containerfile index d8812f0..6060264 100644 --- a/Containerfile +++ b/Containerfile @@ -5,12 +5,10 @@ ENV GIT_URL=https://git.adrianux.net ENV WEBSITE_URL=https://adrianux.net ENV DEBUG_MODE=False -RUN apk add --no-cache git python3 -RUN git clone https://git.adrianux.net/ahoemann/Adrianux.net.git /opt -RUN python3 -m venv /opt/.venv -RUN /opt/.venv/bin/pip install --no-cache-dir -r requirements.txt -RUN apk del git +RUN apk add go +COPY . . +RUN chmod +x website EXPOSE 8080 -CMD [ "/opt/.venv/bin/waitress-serve", "--listen=0.0.0.0:8080", "app:app" ] +CMD [ "/opt/./website" ] \ No newline at end of file From a2b99b29767495b0478a9e7a870ac06ddb7d152e Mon Sep 17 00:00:00 2001 From: ahoemann Date: Tue, 28 Jan 2025 07:47:25 +0000 Subject: [PATCH 4/4] Delete requirements.txt --- requirements.txt | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 398b875..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -flask -waitress -requests \ No newline at end of file