commit 6bc39598c0402ff464aee25da98cd43f730c64c6 Author: Florian Walther Date: Fri Jan 16 16:28:50 2026 +0100 first commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c461d23 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +# Verwende das offizielle Go-Image als Basis +FROM golang:1.24-alpine AS builder + +# Setze den Arbeitsordner +WORKDIR /app + +# Kopiere den Quellcode +COPY . . + +# Baue die Anwendung +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/password-generator + +# Verwende ein minimales Image für die finale Stage +FROM scratch + +# Kopiere die gebaute Binärdatei +COPY --from=builder /app/password-generator /password-generator + +# Setze die Umgebungsvariable für die Ports +ENV PORT=8080 + +# Starte die Anwendung +CMD ["/password-generator"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..14f8b87 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ + +# Funktionsweise + +* Passwortgenerierung: Die Anwendung generiert ein 32-stelliges Passwort mit Großbuchstaben, Kleinbuchstaben und Ziffern (entspricht dem Befehl `apg -a 1 -m 32 -n 1 -M NCL`). +* Zwischenablage: Mit dem Button "In Zwischenablage kopieren" wird das Passwort in die Zwischenablage kopiert. +* Docker: Der Container enthält nur die Go-Anwendung und keine zusätzliche Linux-Distribution. + + +# Baue die Go-Anwendung + +``` +go build -o password-generator ./ +``` + +# Baue das Docker-Image + +``` +docker build -t password-generator . +``` + +# Starte den Docker Container + +``` +docker run -p 8080:8080 password-generator +``` + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..deafd30 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.scu.si/FlorianWalther/Web-Password + +go 1.24.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..db498eb --- /dev/null +++ b/main.go @@ -0,0 +1,229 @@ +package main + +import ( + "crypto/rand" + "fmt" + "log" + "net/http" +) + +const ( + passwordLength = 32 + chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?§$%&=#+<>-:,.;_*@" +) + +func generatePassword() string { + password := make([]byte, passwordLength) + _, err := rand.Read(password) + if err != nil { + log.Fatal(err) + } + + for i := 0; i < passwordLength; i++ { + password[i] = chars[int(password[i])%len(chars)] + } + + return string(password) +} + +func passwordHandler(w http.ResponseWriter, r *http.Request) { + password := generatePassword() + fmt.Fprint(w, password) +} + +func helpHandler(w http.ResponseWriter, r *http.Request) { + helpHTML := ` + + + + Hilfe + + + + +
+

Hilfe: API-Endpunkt

+

+ Diese Anwendung bietet einen API-Endpunkt, um Passwörter direkt über die Kommandozeile abzurufen. + Der Endpunkt gibt das Passwort im Plain-Text-Format zurück. +

+

Endpunkt:

+

http://localhost:8080/api/password

+

Beispiele:

+

Mac/Linux (Terminal):

+
curl http://localhost:8080/api/password
+

Windows (PowerShell):

+
Invoke-RestMethod -Uri http://localhost:8080/api/password
+

Windows (cmd):

+
curl http://localhost:8080/api/password
+

+ Zurück zur Passwort-Generierung +

+
+ + + ` + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprint(w, helpHTML) +} + +func webHandler(w http.ResponseWriter, r *http.Request) { + password := generatePassword() + html := fmt.Sprintf( + ` + + + Passwort-Generator + + + + + +
+ ? +

Generiertes Passwort

+
%s
+
+ + +
+
+ + `, + password, + ) + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + fmt.Fprint(w, html) +} + +func main() { + http.HandleFunc("/", webHandler) + http.HandleFunc("/api/password", passwordHandler) + http.HandleFunc("/help", helpHandler) + log.Println("Server läuft auf http://localhost:8080") + log.Println("Plain-Text-Passwort: curl http://localhost:8080/api/password") + log.Fatal(http.ListenAndServe(":8080", nil)) +} + diff --git a/password-generator b/password-generator new file mode 100755 index 0000000..1cca9c6 Binary files /dev/null and b/password-generator differ