This commit is contained in:
Tomas Dvorak
2025-11-11 10:29:30 +01:00
parent d5b4faea61
commit 8762bde4bf
139 changed files with 7240 additions and 2870 deletions
+104
View File
@@ -0,0 +1,104 @@
package services
import (
"bytes"
"context"
"encoding/json"
"net/http"
"os"
"time"
"strings"
"fotbal-club/internal/config"
"fotbal-club/pkg/httpclient"
)
type ErrorEvent struct {
Origin string `json:"origin"`
Language string `json:"language"`
Severity string `json:"severity,omitempty"`
Message string `json:"message"`
Stack string `json:"stack,omitempty"`
Component string `json:"component,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
URL string `json:"url,omitempty"`
Method string `json:"method,omitempty"`
Status int `json:"status,omitempty"`
RequestID string `json:"request_id,omitempty"`
UserID *uint `json:"user_id,omitempty"`
Session string `json:"session_token,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Context map[string]interface{} `json:"context,omitempty"`
Env string `json:"env,omitempty"`
Version string `json:"version,omitempty"`
Hostname string `json:"hostname,omitempty"`
OccurredAt time.Time `json:"occurred_at"`
}
type ErrorReporter struct {
client *http.Client
endpoint string
token string
hostname string
appEnv string
}
func NewErrorReporter(cfg *config.Config) *ErrorReporter {
if cfg == nil {
return nil
}
endpoint := cfg.ErrorIngestURL
if strings.TrimSpace(endpoint) == "" {
if cfg.AppEnv == "production" {
endpoint = "https://errors.tdvorak.dev/api/v1/errors"
} else {
base := strings.TrimRight(cfg.PublicAPIBaseURL, "/")
if base != "" {
endpoint = base + "/errors"
}
}
}
if strings.TrimSpace(endpoint) == "" {
return nil
}
host, _ := os.Hostname()
return &ErrorReporter{
client: httpclient.FastClient(),
endpoint: endpoint,
token: cfg.ErrorIngestToken,
hostname: host,
appEnv: cfg.AppEnv,
}
}
func (r *ErrorReporter) Report(ctx context.Context, ev *ErrorEvent) {
if r == nil || ev == nil || r.endpoint == "" {
return
}
if ev.Env == "" {
ev.Env = r.appEnv
}
if ev.Hostname == "" {
ev.Hostname = r.hostname
}
if ev.OccurredAt.IsZero() {
ev.OccurredAt = time.Now()
}
b, err := json.Marshal(ev)
if err != nil {
return
}
ctx2, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx2, http.MethodPost, r.endpoint, bytes.NewReader(b))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
if r.token != "" {
req.Header.Set("Authorization", "Bearer "+r.token)
}
_, _ = r.client.Do(req)
}
@@ -0,0 +1,175 @@
package services
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"fotbal-club/internal/config"
"fotbal-club/internal/models"
"gorm.io/gorm"
)
type erMonitor struct {
ID uint `json:"id,omitempty"`
Name string `json:"name"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
CheckIntervalSec int `json:"check_interval_sec"`
TimeoutMs int `json:"timeout_ms"`
ExpectStatusMin int `json:"expect_status_min"`
ExpectStatusMax int `json:"expect_status_max"`
}
type erListResp struct {
Items []erMonitor `json:"items"`
}
func StartErrorReviewAutoRegister(db *gorm.DB) {
go func() {
defer func() { _ = recover() }()
time.Sleep(2 * time.Second)
for i := 0; i < 3; i++ {
if tryAutoRegister(db) {
return
}
time.Sleep(time.Duration(2+i) * time.Second)
}
}()
}
func tryAutoRegister(db *gorm.DB) bool {
if db == nil || config.AppConfig == nil {
return false
}
var s models.Settings
_ = db.First(&s).Error
// Domain is managed via environment only; fallback based on ERROR_LOCAL
adminURL := strings.TrimSpace(os.Getenv("ERROR_REVIEW_ADMIN_URL"))
if adminURL == "" {
errorLocal := false
if b, err := strconv.ParseBool(strings.TrimSpace(os.Getenv("ERROR_LOCAL"))); err == nil && b {
errorLocal = true
}
if !errorLocal {
if b, err := strconv.ParseBool(strings.TrimSpace(os.Getenv("error_local"))); err == nil && b {
errorLocal = true
}
}
if errorLocal {
adminURL = "http://127.0.0.1:8083/api/v1/admin"
} else {
adminURL = "https://error.tdvorak.dev/api/v1/admin"
}
}
// Prefer env token only
token := strings.TrimSpace(os.Getenv("ERROR_REVIEW_ADMIN_TOKEN"))
if token == "" {
return false
}
// Only register after setup is complete (club name present)
if strings.TrimSpace(s.ClubName) == "" {
return false
}
if strings.HasSuffix(adminURL, "/") {
adminURL = strings.TrimRight(adminURL, "/")
}
base := strings.TrimRight(strings.TrimSpace(s.APIBaseURL), "/")
if base == "" {
base = strings.TrimRight(config.AppConfig.PublicAPIBaseURL, "/")
}
if base == "" {
// Fallback for local dev
errorLocal := false
if b, err := strconv.ParseBool(strings.TrimSpace(os.Getenv("ERROR_LOCAL"))); err == nil && b { errorLocal = true }
if !errorLocal {
if b, err := strconv.ParseBool(strings.TrimSpace(os.Getenv("error_local"))); err == nil && b { errorLocal = true }
}
if errorLocal {
base = "http://127.0.0.1:8080/api/v1"
} else {
return false
}
}
// Ensure API suffix present
if !strings.Contains(base, "/api/") { base = strings.TrimRight(base, "/") + "/api/v1" }
monURL := base + "/health"
disp := "Fotbal Club"
if strings.TrimSpace(s.ClubName) != "" { disp = strings.TrimSpace(s.ClubName) }
host := ""
if u, err := url.Parse(monURL); err == nil { host = u.Hostname() }
name := disp
if host != "" { name = name + " (" + host + ")" }
name = name + " API Health"
mon := erMonitor{
Name: name,
URL: monURL,
Enabled: true,
CheckIntervalSec: 60,
TimeoutMs: 4000,
ExpectStatusMin: 200,
ExpectStatusMax: 399,
}
client := &http.Client{Timeout: 5 * time.Second}
req, _ := http.NewRequest(http.MethodGet, adminURL+"/monitors", nil)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("X-Admin-Token", token)
res, err := client.Do(req)
if err != nil {
return false
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return false
}
var list erListResp
if err := json.NewDecoder(res.Body).Decode(&list); err != nil {
return false
}
var existing *erMonitor
for i := range list.Items {
if strings.TrimSpace(list.Items[i].URL) == monURL {
existing = &list.Items[i]
break
}
}
b, _ := json.Marshal(mon)
if existing == nil {
req2, _ := http.NewRequest(http.MethodPost, adminURL+"/monitors", bytes.NewReader(b))
req2.Header.Set("Authorization", "Bearer "+token)
req2.Header.Set("Content-Type", "application/json")
req2.Header.Set("X-Admin-Token", token)
res2, err2 := client.Do(req2)
if err2 != nil {
return false
}
defer res2.Body.Close()
if res2.StatusCode == http.StatusCreated || res2.StatusCode == http.StatusOK {
return true
}
return false
}
req3, _ := http.NewRequest(http.MethodPut, adminURL+"/monitors/"+itoa(existing.ID), bytes.NewReader(b))
req3.Header.Set("Authorization", "Bearer "+token)
req3.Header.Set("Content-Type", "application/json")
req3.Header.Set("X-Admin-Token", token)
res3, err3 := client.Do(req3)
if err3 != nil {
return false
}
defer res3.Body.Close()
if res3.StatusCode == http.StatusOK {
return true
}
return false
}
func itoa(u uint) string {
return strconv.FormatUint(uint64(u), 10)
}
+31 -2
View File
@@ -17,8 +17,7 @@ func EvaluateSpamScore(s string) (float64, []string) {
rules = append(rules, "too_short")
}
// Excessive repeated characters like 'aaaaaa' or '!!!!'
repeatRe := regexp.MustCompile(`([a-zA-Z!?.])\1{4,}`)
if repeatRe.MatchString(content) {
if hasExcessiveRepetition(content, 5) {
rules = append(rules, "repeated_chars")
}
// Low vowel ratio suggests gibberish in Czech/English latin text
@@ -53,3 +52,33 @@ func EvaluateSpamScore(s string) (float64, []string) {
if score > 1.0 { score = 1.0 }
return score, rules
}
// hasExcessiveRepetition checks if s contains a run of the same character of length >= minRun
// Limited to ASCII letters and the punctuation characters ! ? . to mirror the previous intent.
func hasExcessiveRepetition(s string, minRun int) bool {
if minRun < 2 { minRun = 2 }
run := 1
var prev rune
first := true
for _, r := range s {
if first {
prev = r
first = false
continue
}
if r == prev && (isAsciiLetter(r) || r == '!' || r == '?' || r == '.') {
run++
if run >= minRun {
return true
}
} else {
prev = r
run = 1
}
}
return false
}
func isAsciiLetter(r rune) bool {
return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
}
+2
View File
@@ -87,6 +87,8 @@ func (s *SweepstakesService) FinalizeSweepstake(sw *models.Sweepstake, seed stri
if nWinners == 0 {
if cur.TotalPrizes > 0 { nWinners = cur.TotalPrizes }
}
// Cap winners to a safe maximum
if nWinners > 100 { nWinners = 100 }
if nWinners > len(entries) { nWinners = len(entries) }
// Build seed