first commit

This commit is contained in:
Florian Walther
2026-01-16 16:28:50 +01:00
commit 6bc39598c0
5 changed files with 282 additions and 0 deletions

24
Dockerfile Normal file
View File

@@ -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"]

26
README.md Normal file
View File

@@ -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
```

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module gitea.scu.si/FlorianWalther/Web-Password
go 1.24.1

229
main.go Normal file
View File

@@ -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 := `
<!DOCTYPE html>
<html>
<head>
<title>Hilfe</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Helvetica Neue', Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
color: #333;
}
.help-container {
text-align: left;
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
max-width: 800px;
width: 90%;
min-width: 600px;
}
h1 {
font-size: 1.5rem;
margin-bottom: 1.5rem;
color: #444;
}
pre {
font-family: 'Courier New', Courier, monospace;
background: #f0f0f0;
padding: 0.8rem;
border-radius: 4px;
overflow-x: auto;
}
a {
color: #007BFF;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="help-container">
<h1>Hilfe: API-Endpunkt</h1>
<p>
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.
</p>
<h2>Endpunkt:</h2>
<p><code>http://localhost:8080/api/password</code></p>
<h2>Beispiele:</h2>
<h3>Mac/Linux (Terminal):</h3>
<pre>curl http://localhost:8080/api/password</pre>
<h3>Windows (PowerShell):</h3>
<pre>Invoke-RestMethod -Uri http://localhost:8080/api/password</pre>
<h3>Windows (cmd):</h3>
<pre>curl http://localhost:8080/api/password</pre>
<p>
<a href="/">Zurück zur Passwort-Generierung</a>
</p>
</div>
</body>
</html>
`
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(
`<DOCTYPE html>
<html>
<head>
<title>Passwort-Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Helvetica Neue', Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f5f5f5;
color: #333;
}
.container {
text-align: center;
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
width: 90%%;
min-width: 600px;
position: relative;
}
h1 {
font-size: 1.5rem;
margin-bottom: 1.5rem;
color: #444;
}
#password {
font-family: 'Courier New', Courier, monospace;
font-size: 1.2rem;
letter-spacing: 1px;
margin: 1rem auto;
padding: 0.8rem;
background: #f0f0f0;
border-radius: 4px;
border: 1px solid #ddd;
width: 90%%;
word-break: break-all;
}
button {
background: #007BFF;
color: white;
border: none;
padding: 0.6rem 1.2rem;
font-size: 1rem;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
margin: 0.3rem;
}
button:hover {
background: #0056b3;
}
.help-link {
position: absolute;
top: 1rem;
right: 1rem;
font-size: 1.2rem;
color: #999;
text-decoration: none;
}
.help-link:hover {
color: #444;
}
.buttons {
display: flex;
justify-content: center;
gap: 0.5rem;
}
</style>
<script>
function copyToClipboard() {
navigator.clipboard.writeText(document.getElementById("password").innerText);
alert("Passwort kopiert!");
}
function generateNewPassword() {
fetch("/api/password")
.then(response => response.text())
.then(password => {
document.getElementById("password").innerText = password;
})
.catch(error => console.error("Fehler:", error));
}
</script>
</head>
<body>
<div class="container">
<a href="/help" class="help-link">?</a>
<h1>Generiertes Passwort</h1>
<div id="password">%s</div>
<div class="buttons">
<button onclick="copyToClipboard()">In Zwischenablage kopieren</button>
<button onclick="generateNewPassword()">Neues Passwort generieren</button>
</div>
</div>
</body>
</html>`,
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))
}

BIN
password-generator Executable file

Binary file not shown.