package main import ( "crypto/rand" "fmt" "log" "net/http" ) const ( passwordLength = 32 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" //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):

echo $(curl -s 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
API

Generiertes Passwort

%s
✓ Kopiert!
`, 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)) }