update layout, pdf export for headeranalyzer needs fixing layout

This commit is contained in:
nahakubuilde
2025-07-17 08:33:04 +01:00
parent 518cc2e275
commit 4e4e4f735e
13 changed files with 2377 additions and 1512 deletions
+73
View File
@@ -0,0 +1,73 @@
package passwordgenerator
import (
"encoding/json"
"net/http"
)
// PasswordAPIHandler handles password generation requests
func PasswordAPIHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var requestData struct {
Type string `json:"type"`
Length int `json:"length"`
IncludeUpper bool `json:"includeUpper"`
IncludeLower bool `json:"includeLower"`
NumberCount int `json:"numberCount"`
SpecialChars string `json:"specialChars"`
NoConsecutive bool `json:"noConsecutive"`
WordCount int `json:"wordCount"`
NumberPosition string `json:"numberPosition"`
UseNumbers bool `json:"useNumbers"`
UseSpecial bool `json:"useSpecial"`
}
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid JSON"))
return
}
// Convert to internal Config format
config := Config{
Length: requestData.Length,
IncludeUpper: requestData.IncludeUpper,
IncludeLower: requestData.IncludeLower,
NumberCount: requestData.NumberCount,
SpecialChars: requestData.SpecialChars,
NoConsecutive: requestData.NoConsecutive,
UsePassphrase: requestData.Type == "passphrase",
WordCount: requestData.WordCount,
NumberPosition: requestData.NumberPosition,
PassphraseUseNumbers: requestData.UseNumbers,
PassphraseUseSpecial: requestData.UseSpecial,
}
password, err := GeneratePassword(config)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte(password))
}
// PasswordInfoAPIHandler handles word list info requests
func PasswordInfoAPIHandler(w http.ResponseWriter, r *http.Request) {
count, source, lastUpdate := GetWordListInfo()
info := map[string]interface{}{
"wordCount": count,
"source": source,
"lastUpdate": lastUpdate.Format("2006-01-02 15:04:05"),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(info)
}
+9 -5
View File
@@ -391,20 +391,23 @@ func generatePassphrase(config Config) (string, error) {
if config.PassphraseUseSpecial && len(config.SpecialChars) > 0 {
sepIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(config.SpecialChars))))
if err != nil {
return "-" // fallback
return "" // fallback to no separator
}
return string(config.SpecialChars[sepIndex.Int64()])
}
return "-"
return ""
}
// Default separator for non-random cases
defaultSeparator := "-"
defaultSeparator := ""
if config.PassphraseUseSpecial && len(config.SpecialChars) > 0 {
defaultSeparator = "-"
}
// Combine based on number position
var result string
if len(numbers) == 0 {
// No numbers, join words with random separators
// No numbers, join words with separators only if special chars are enabled
if config.PassphraseUseSpecial && len(config.SpecialChars) > 0 {
var parts []string
for i, word := range words {
@@ -415,7 +418,8 @@ func generatePassphrase(config Config) (string, error) {
}
result = strings.Join(parts, "")
} else {
result = strings.Join(words, defaultSeparator)
// No special characters, just concatenate words without separators
result = strings.Join(words, "")
}
} else {
switch config.NumberPosition {
+112
View File
@@ -0,0 +1,112 @@
package passwordgenerator
import (
"embed"
"html/template"
"net/http"
"strconv"
"strings"
)
type Handler struct {
templates *template.Template
}
type PasswordConfig struct {
Type string
Length int
IncludeUpper bool
IncludeLower bool
NumberCount int
SpecialChars string
NoConsecutive bool
WordCount int
UseNumbers bool
UseSpecial bool
NumberPosition string
SavePasswords bool
}
func NewHandler(embeddedFiles embed.FS) *Handler {
tmpl := template.Must(template.New("").Funcs(template.FuncMap{
"splitString": func(s, delimiter string) []string {
return strings.Split(s, delimiter)
},
"contains": func(s, substr string) bool {
return strings.Contains(s, substr)
},
"add": func(a, b int) int {
return a + b
},
"ge": func(a, b int) bool {
return a >= b
},
"len": func(v interface{}) int {
switch s := v.(type) {
case []string:
return len(s)
case map[string]string:
return len(s)
case string:
return len(s)
default:
return 0
}
},
}).ParseFS(embeddedFiles, "web/base.html", "web/password.html"))
return &Handler{
templates: tmpl,
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Parse URL parameters to set default values
config := PasswordConfig{
Type: getStringParam(r, "type", "passphrase"),
Length: getIntParam(r, "length", 12),
IncludeUpper: getBoolParam(r, "includeUpper", true),
IncludeLower: getBoolParam(r, "includeLower", true),
NumberCount: getIntParam(r, "numberCount", 1),
SpecialChars: getStringParam(r, "specialChars", "!@#$%^&*-_=+"),
NoConsecutive: getBoolParam(r, "noConsecutive", false),
WordCount: getIntParam(r, "wordCount", 3),
UseNumbers: getBoolParam(r, "useNumbers", true),
UseSpecial: getBoolParam(r, "useSpecial", false),
NumberPosition: getStringParam(r, "numberPosition", "end"),
SavePasswords: getBoolParam(r, "savePasswords", false),
}
data := struct {
CurrentPage string
Config PasswordConfig
}{
CurrentPage: "password",
Config: config,
}
h.templates.ExecuteTemplate(w, "password.html", data)
}
// Helper functions to parse URL parameters
func getStringParam(r *http.Request, key, defaultValue string) string {
if value := r.URL.Query().Get(key); value != "" {
return value
}
return defaultValue
}
func getIntParam(r *http.Request, key string, defaultValue int) int {
if value := r.URL.Query().Get(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
func getBoolParam(r *http.Request, key string, defaultValue bool) bool {
if value := r.URL.Query().Get(key); value != "" {
return value == "true" || value == "1" || value == "on"
}
return defaultValue
}