Refactor and remove sqlite and replace with MySQL
This commit is contained in:
96
internal/handlers/admin/audit.go
Normal file
96
internal/handlers/admin/audit.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
templateHelpers "synlotto-website/helpers/template"
|
||||
|
||||
"synlotto-website/middleware"
|
||||
"synlotto-website/models"
|
||||
)
|
||||
|
||||
type AdminLogEntry struct {
|
||||
AccessedAt string
|
||||
UserID int
|
||||
Path string
|
||||
IP string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
func AdminAccessLogHandler(db *sql.DB) http.HandlerFunc {
|
||||
return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
||||
data := models.TemplateData{}
|
||||
context := templateHelpers.TemplateContext(w, r, data)
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT accessed_at, user_id, path, ip, user_agent
|
||||
FROM admin_access_log
|
||||
ORDER BY accessed_at DESC
|
||||
LIMIT 100
|
||||
`)
|
||||
if err != nil {
|
||||
log.Println("⚠️ Failed to load admin access logs:", err)
|
||||
http.Error(w, "Error loading logs", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var logs []AdminLogEntry // ToDo should be in models
|
||||
for rows.Next() {
|
||||
var entry AdminLogEntry
|
||||
if err := rows.Scan(&entry.AccessedAt, &entry.UserID, &entry.Path, &entry.IP, &entry.UserAgent); err != nil {
|
||||
log.Println("⚠️ Scan failed:", err)
|
||||
continue
|
||||
}
|
||||
logs = append(logs, entry)
|
||||
}
|
||||
context["AuditLogs"] = logs
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles("access_log.html", "templates/admin/logs/access_log.html")
|
||||
|
||||
_ = tmpl.ExecuteTemplate(w, "layout", context)
|
||||
})
|
||||
}
|
||||
|
||||
func AuditLogHandler(db *sql.DB) http.HandlerFunc {
|
||||
return middleware.Auth(true)(func(w http.ResponseWriter, r *http.Request) {
|
||||
data := models.TemplateData{}
|
||||
context := templateHelpers.TemplateContext(w, r, data)
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT timestamp, user_id, action, ip, user_agent
|
||||
FROM audit_log
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 100
|
||||
`)
|
||||
if err != nil {
|
||||
log.Println("❌ Failed to load audit log:", err)
|
||||
http.Error(w, "Could not load audit log", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var logs []models.AuditEntry
|
||||
for rows.Next() {
|
||||
var entry models.AuditEntry
|
||||
err := rows.Scan(&entry.Timestamp, &entry.UserID, &entry.Action, &entry.IP, &entry.UserAgent)
|
||||
if err != nil {
|
||||
log.Println("⚠️ Failed to scan row:", err)
|
||||
continue
|
||||
}
|
||||
logs = append(logs, entry)
|
||||
}
|
||||
|
||||
context["AuditLogs"] = logs
|
||||
|
||||
tmpl := templateHelpers.LoadTemplateFiles("audit.html", "templates/admin/logs/audit.html")
|
||||
|
||||
err = tmpl.ExecuteTemplate(w, "layout", context)
|
||||
if err != nil {
|
||||
log.Println("❌ Failed to render audit page:", err)
|
||||
http.Error(w, "Template error", http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user