• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

umputun / tg-spam / 14284916033

05 Apr 2025 07:04PM UTC coverage: 81.746% (+0.07%) from 81.68%
14284916033

push

github

umputun
Add GetLuaPluginNames method to show available plugins in UI

24 of 27 new or added lines in 3 files covered. (88.89%)

98 existing lines in 3 files now uncovered.

4747 of 5807 relevant lines covered (81.75%)

59.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.89
/app/webapi/webapi.go
1
// Package webapi provides a web API spam detection service.
2
package webapi
3

4
import (
5
        "bytes"
6
        "compress/gzip"
7
        "context"
8
        "crypto/rand"
9
        "crypto/sha1" //nolint
10
        "embed"
11
        "encoding/json"
12
        "errors"
13
        "fmt"
14
        "html/template"
15
        "io"
16
        "io/fs"
17
        "math/big"
18
        "net/http"
19
        "path"
20
        "strconv"
21
        "strings"
22
        "time"
23

24
        "github.com/didip/tollbooth/v8"
25
        log "github.com/go-pkgz/lgr"
26
        "github.com/go-pkgz/rest"
27
        "github.com/go-pkgz/rest/logger"
28
        "github.com/go-pkgz/routegroup"
29

30
        "github.com/umputun/tg-spam/app/storage"
31
        "github.com/umputun/tg-spam/app/storage/engine"
32
        "github.com/umputun/tg-spam/lib/approved"
33
        "github.com/umputun/tg-spam/lib/spamcheck"
34
)
35

36
//go:generate moq --out mocks/detector.go --pkg mocks --with-resets --skip-ensure . Detector
37
//go:generate moq --out mocks/spam_filter.go --pkg mocks --with-resets --skip-ensure . SpamFilter
38
//go:generate moq --out mocks/locator.go --pkg mocks --with-resets --skip-ensure . Locator
39
//go:generate moq --out mocks/detected_spam.go --pkg mocks --with-resets --skip-ensure . DetectedSpam
40
//go:generate moq --out mocks/storage_engine.go --pkg mocks --with-resets --skip-ensure . StorageEngine
41

42
//go:embed assets/* assets/components/*
43
var templateFS embed.FS
44
var tmpl = template.Must(template.ParseFS(templateFS, "assets/*.html", "assets/components/*.html"))
45

46
// startTime tracks when the server started
47
var startTime = time.Now()
48

49
// Server is a web API server.
50
type Server struct {
51
        Config
52
}
53

54
// Config defines  server parameters
55
type Config struct {
56
        Version       string        // version to show in /ping
57
        ListenAddr    string        // listen address
58
        Detector      Detector      // spam detector
59
        SpamFilter    SpamFilter    // spam filter (bot)
60
        DetectedSpam  DetectedSpam  // detected spam accessor
61
        Locator       Locator       // locator for user info
62
        StorageEngine StorageEngine // database engine access for backups
63
        AuthPasswd    string        // basic auth password for user "tg-spam"
64
        AuthHash      string        // basic auth hash for user "tg-spam". If both AuthPasswd and AuthHash are provided, AuthHash is used
65
        Dbg           bool          // debug mode
66
        Settings      Settings      // application settings
67
}
68

69
// Settings contains all application settings
70
type Settings struct {
71
        InstanceID              string        `json:"instance_id"`
72
        PrimaryGroup            string        `json:"primary_group"`
73
        AdminGroup              string        `json:"admin_group"`
74
        DisableAdminSpamForward bool          `json:"disable_admin_spam_forward"`
75
        LoggerEnabled           bool          `json:"logger_enabled"`
76
        SuperUsers              []string      `json:"super_users"`
77
        NoSpamReply             bool          `json:"no_spam_reply"`
78
        CasEnabled              bool          `json:"cas_enabled"`
79
        MetaEnabled             bool          `json:"meta_enabled"`
80
        MetaLinksLimit          int           `json:"meta_links_limit"`
81
        MetaMentionsLimit       int           `json:"meta_mentions_limit"`
82
        MetaLinksOnly           bool          `json:"meta_links_only"`
83
        MetaImageOnly           bool          `json:"meta_image_only"`
84
        MetaVideoOnly           bool          `json:"meta_video_only"`
85
        MetaAudioOnly           bool          `json:"meta_audio_only"`
86
        MetaForwarded           bool          `json:"meta_forwarded"`
87
        MetaKeyboard            bool          `json:"meta_keyboard"`
88
        MetaUsernameSymbols     string        `json:"meta_username_symbols"`
89
        MultiLangLimit          int           `json:"multi_lang_limit"`
90
        OpenAIEnabled           bool          `json:"openai_enabled"`
91
        LuaPluginsEnabled       bool          `json:"lua_plugins_enabled"`
92
        LuaPluginsDir           string        `json:"lua_plugins_dir"`
93
        LuaEnabledPlugins       []string      `json:"lua_enabled_plugins"`
94
        LuaAvailablePlugins     []string      `json:"lua_available_plugins"` // the list of all available Lua plugins
95
        SamplesDataPath         string        `json:"samples_data_path"`
96
        DynamicDataPath         string        `json:"dynamic_data_path"`
97
        WatchIntervalSecs       int           `json:"watch_interval_secs"`
98
        SimilarityThreshold     float64       `json:"similarity_threshold"`
99
        MinMsgLen               int           `json:"min_msg_len"`
100
        MaxEmoji                int           `json:"max_emoji"`
101
        MinSpamProbability      float64       `json:"min_spam_probability"`
102
        ParanoidMode            bool          `json:"paranoid_mode"`
103
        FirstMessagesCount      int           `json:"first_messages_count"`
104
        StartupMessageEnabled   bool          `json:"startup_message_enabled"`
105
        TrainingEnabled         bool          `json:"training_enabled"`
106
        StorageTimeout          time.Duration `json:"storage_timeout"`
107
        OpenAIVeto              bool          `json:"openai_veto"`
108
        OpenAIHistorySize       int           `json:"openai_history_size"`
109
        OpenAIModel             string        `json:"openai_model"`
110
        SoftBanEnabled          bool          `json:"soft_ban_enabled"`
111
        AbnormalSpacingEnabled  bool          `json:"abnormal_spacing_enabled"`
112
        HistorySize             int           `json:"history_size"`
113
        DebugModeEnabled        bool          `json:"debug_mode_enabled"`
114
        DryModeEnabled          bool          `json:"dry_mode_enabled"`
115
        TGDebugModeEnabled      bool          `json:"tg_debug_mode_enabled"`
116
}
117

118
// Detector is a spam detector interface.
119
type Detector interface {
120
        Check(req spamcheck.Request) (spam bool, cr []spamcheck.Response)
121
        ApprovedUsers() []approved.UserInfo
122
        AddApprovedUser(user approved.UserInfo) error
123
        RemoveApprovedUser(id string) error
124
        GetLuaPluginNames() []string // Returns the list of available Lua plugin names
125
}
126

127
// SpamFilter is a spam filter, bot interface.
128
type SpamFilter interface {
129
        UpdateSpam(msg string) error
130
        UpdateHam(msg string) error
131
        ReloadSamples() (err error)
132
        DynamicSamples() (spam, ham []string, err error)
133
        RemoveDynamicSpamSample(sample string) error
134
        RemoveDynamicHamSample(sample string) error
135
}
136

137
// Locator is a storage interface used to get user id by name and vice versa.
138
type Locator interface {
139
        UserIDByName(ctx context.Context, userName string) int64
140
        UserNameByID(ctx context.Context, userID int64) string
141
}
142

143
// DetectedSpam is a storage interface used to get detected spam messages and set added flag.
144
type DetectedSpam interface {
145
        Read(ctx context.Context) ([]storage.DetectedSpamInfo, error)
146
        SetAddedToSamplesFlag(ctx context.Context, id int64) error
147
        FindByUserID(ctx context.Context, userID int64) (*storage.DetectedSpamInfo, error)
148
}
149

150
// StorageEngine provides access to the database engine for operations like backup
151
type StorageEngine interface {
152
        Backup(ctx context.Context, w io.Writer) error
153
        Type() engine.Type
154
        BackupSqliteAsPostgres(ctx context.Context, w io.Writer) error
155
}
156

157
// NewServer creates a new web API server.
158
func NewServer(config Config) *Server {
46✔
159
        return &Server{Config: config}
46✔
160
}
46✔
161

162
// Run starts server and accepts requests checking for spam messages.
163
func (s *Server) Run(ctx context.Context) error {
3✔
164
        router := routegroup.New(http.NewServeMux())
3✔
165
        router.Use(rest.Recoverer(log.Default()))
3✔
166
        router.Use(logger.New(logger.Log(log.Default()), logger.Prefix("[DEBUG]")).Handler)
3✔
167
        router.Use(rest.Throttle(1000))
3✔
168
        router.Use(rest.AppInfo("tg-spam", "umputun", s.Version), rest.Ping)
3✔
169
        router.Use(tollbooth.HTTPMiddleware(tollbooth.NewLimiter(50, nil)))
3✔
170
        router.Use(rest.SizeLimit(1024 * 1024)) // 1M max request size
3✔
171

3✔
172
        if s.AuthPasswd != "" || s.AuthHash != "" {
6✔
173
                log.Printf("[INFO] basic auth enabled for webapi server")
3✔
174
                if s.AuthHash != "" {
4✔
175
                        router.Use(rest.BasicAuthWithBcryptHashAndPrompt("tg-spam", s.AuthHash))
1✔
176
                } else {
3✔
177
                        router.Use(rest.BasicAuthWithPrompt("tg-spam", s.AuthPasswd))
2✔
178
                }
2✔
179
        } else {
×
UNCOV
180
                log.Printf("[WARN] basic auth disabled, access to webapi is not protected")
×
UNCOV
181
        }
×
182

183
        router = s.routes(router) // setup routes
3✔
184

3✔
185
        srv := &http.Server{Addr: s.ListenAddr, Handler: router, ReadTimeout: 5 * time.Second, WriteTimeout: 5 * time.Second}
3✔
186
        go func() {
6✔
187
                <-ctx.Done()
3✔
188
                if err := srv.Shutdown(ctx); err != nil {
3✔
UNCOV
189
                        log.Printf("[WARN] failed to shutdown webapi server: %v", err)
×
190
                } else {
3✔
191
                        log.Printf("[INFO] webapi server stopped")
3✔
192
                }
3✔
193
        }()
194

195
        log.Printf("[INFO] start webapi server on %s", s.ListenAddr)
3✔
196
        if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
3✔
UNCOV
197
                return fmt.Errorf("failed to run server: %w", err)
×
UNCOV
198
        }
×
199
        return nil
3✔
200
}
201

202
func (s *Server) routes(router *routegroup.Bundle) *routegroup.Bundle {
5✔
203
        // auth api routes
5✔
204
        router.Route(func(authApi *routegroup.Bundle) {
10✔
205
                authApi.Use(s.authMiddleware(rest.BasicAuthWithUserPasswd("tg-spam", s.AuthPasswd)))
5✔
206
                authApi.HandleFunc("POST /check", s.checkMsgHandler)         // check a message for spam
5✔
207
                authApi.HandleFunc("GET /check/{user_id}", s.checkIDHandler) // check user id for spam
5✔
208

5✔
209
                authApi.Mount("/update").Route(func(r *routegroup.Bundle) {
10✔
210
                        // update spam/ham samples
5✔
211
                        r.HandleFunc("POST /spam", s.updateSampleHandler(s.SpamFilter.UpdateSpam)) // update spam samples
5✔
212
                        r.HandleFunc("POST /ham", s.updateSampleHandler(s.SpamFilter.UpdateHam))   // update ham samples
5✔
213
                })
5✔
214

215
                authApi.Mount("/delete").Route(func(r *routegroup.Bundle) {
10✔
216
                        // delete spam/ham samples
5✔
217
                        r.HandleFunc("POST /spam", s.deleteSampleHandler(s.SpamFilter.RemoveDynamicSpamSample))
5✔
218
                        r.HandleFunc("POST /ham", s.deleteSampleHandler(s.SpamFilter.RemoveDynamicHamSample))
5✔
219
                })
5✔
220

221
                authApi.Mount("/download").Route(func(r *routegroup.Bundle) {
10✔
222
                        r.HandleFunc("GET /spam", s.downloadSampleHandler(func(spam, _ []string) ([]string, string) {
5✔
UNCOV
223
                                return spam, "spam.txt"
×
224
                        }))
×
225
                        r.HandleFunc("GET /ham", s.downloadSampleHandler(func(_, ham []string) ([]string, string) {
5✔
UNCOV
226
                                return ham, "ham.txt"
×
UNCOV
227
                        }))
×
228
                        r.HandleFunc("GET /detected_spam", s.downloadDetectedSpamHandler)
5✔
229
                        r.HandleFunc("GET /backup", s.downloadBackupHandler)
5✔
230
                        r.HandleFunc("GET /export-to-postgres", s.downloadExportToPostgresHandler)
5✔
231
                })
232

233
                authApi.HandleFunc("GET /samples", s.getDynamicSamplesHandler)    // get dynamic samples
5✔
234
                authApi.HandleFunc("PUT /samples", s.reloadDynamicSamplesHandler) // reload samples
5✔
235

5✔
236
                authApi.Mount("/users").Route(func(r *routegroup.Bundle) { // manage approved users
10✔
237
                        // add user to the approved list and storage
5✔
238
                        r.HandleFunc("POST /add", s.updateApprovedUsersHandler(s.Detector.AddApprovedUser))
5✔
239
                        // remove user from an approved list and storage
5✔
240
                        r.HandleFunc("POST /delete", s.updateApprovedUsersHandler(s.removeApprovedUser))
5✔
241
                        // get approved users
5✔
242
                        r.HandleFunc("GET /", s.getApprovedUsersHandler)
5✔
243
                })
5✔
244

245
                authApi.HandleFunc("GET /settings", s.getSettingsHandler) // get application settings
5✔
246
        })
247

248
        router.Route(func(webUI *routegroup.Bundle) {
10✔
249
                webUI.Use(s.authMiddleware(rest.BasicAuthWithPrompt("tg-spam", s.AuthPasswd)))
5✔
250
                webUI.HandleFunc("GET /", s.htmlSpamCheckHandler)                         // serve template for webUI UI
5✔
251
                webUI.HandleFunc("GET /manage_samples", s.htmlManageSamplesHandler)       // serve manage samples page
5✔
252
                webUI.HandleFunc("GET /manage_users", s.htmlManageUsersHandler)           // serve manage users page
5✔
253
                webUI.HandleFunc("GET /detected_spam", s.htmlDetectedSpamHandler)         // serve detected spam page
5✔
254
                webUI.HandleFunc("GET /list_settings", s.htmlSettingsHandler)             // serve settings
5✔
255
                webUI.HandleFunc("POST /detected_spam/add", s.htmlAddDetectedSpamHandler) // add detected spam to samples
5✔
256

5✔
257
                // handle logout - force Basic Auth re-authentication
5✔
258
                webUI.HandleFunc("GET /logout", func(w http.ResponseWriter, _ *http.Request) {
5✔
259
                        w.Header().Set("WWW-Authenticate", `Basic realm="tg-spam"`)
×
260
                        w.WriteHeader(http.StatusUnauthorized)
×
261
                        fmt.Fprintln(w, "Logged out successfully")
×
262
                })
×
263

264
                // serve only specific static files at root level
265
                staticFiles := newStaticFS(templateFS,
5✔
266
                        staticFileMapping{urlPath: "styles.css", filesysPath: "assets/styles.css"},
5✔
267
                        staticFileMapping{urlPath: "logo.png", filesysPath: "assets/logo.png"},
5✔
268
                        staticFileMapping{urlPath: "spinner.svg", filesysPath: "assets/spinner.svg"},
5✔
269
                )
5✔
270
                webUI.HandleFiles("/", http.FS(staticFiles))
5✔
271
        })
272

273
        return router
5✔
274
}
275

276
// checkMsgHandler handles POST /check request.
277
// it gets message text and user id from request body and returns spam status and check results.
278
func (s *Server) checkMsgHandler(w http.ResponseWriter, r *http.Request) {
8✔
279
        type CheckResultDisplay struct {
8✔
280
                Spam   bool
8✔
281
                Checks []spamcheck.Response
8✔
282
        }
8✔
283

8✔
284
        isHtmxRequest := r.Header.Get("HX-Request") == "true"
8✔
285

8✔
286
        req := spamcheck.Request{CheckOnly: true}
8✔
287
        if !isHtmxRequest {
15✔
288
                // API request
7✔
289
                if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
8✔
290
                        w.WriteHeader(http.StatusBadRequest)
1✔
291
                        rest.RenderJSON(w, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
292
                        log.Printf("[WARN] can't decode request: %v", err)
1✔
293
                        return
1✔
294
                }
1✔
295
        } else {
1✔
296
                // for hx-request (HTMX) we need to get the values from the form
1✔
297
                req.UserID = r.FormValue("user_id")
1✔
298
                req.UserName = r.FormValue("user_name")
1✔
299
                req.Msg = r.FormValue("msg")
1✔
300
        }
1✔
301

302
        spam, cr := s.Detector.Check(req)
7✔
303
        if !isHtmxRequest {
13✔
304
                // for API request return JSON
6✔
305
                rest.RenderJSON(w, rest.JSON{"spam": spam, "checks": cr})
6✔
306
                return
6✔
307
        }
6✔
308

309
        if req.Msg == "" {
1✔
310
                w.Header().Set("HX-Retarget", "#error-message")
×
311
                fmt.Fprintln(w, "<div class='alert alert-danger'>Valid message required.</div>")
×
312
                return
×
313
        }
×
314

315
        // render result for HTMX request
316
        resultDisplay := CheckResultDisplay{
1✔
317
                Spam:   spam,
1✔
318
                Checks: cr,
1✔
319
        }
1✔
320

1✔
321
        if err := tmpl.ExecuteTemplate(w, "check_results", resultDisplay); err != nil {
1✔
322
                log.Printf("[WARN] can't execute result template: %v", err)
×
323
                http.Error(w, "Error rendering result", http.StatusInternalServerError)
×
324
                return
×
325
        }
×
326
}
327

328
// checkIDHandler handles GET /check/{user_id} request.
329
// it returns JSON with the status "spam" or "ham" for a given user id.
330
// if user is spammer, it also returns check results.
331
func (s *Server) checkIDHandler(w http.ResponseWriter, r *http.Request) {
2✔
332
        type info struct {
2✔
333
                UserName  string               `json:"user_name,omitempty"`
2✔
334
                Message   string               `json:"message,omitempty"`
2✔
335
                Timestamp time.Time            `json:"timestamp,omitempty"`
2✔
336
                Checks    []spamcheck.Response `json:"checks,omitempty"`
2✔
337
        }
2✔
338
        resp := struct {
2✔
339
                Status string `json:"status"`
2✔
340
                Info   *info  `json:"info,omitempty"`
2✔
341
        }{
2✔
342
                Status: "ham",
2✔
343
        }
2✔
344

2✔
345
        userID, err := strconv.ParseInt(r.PathValue("user_id"), 10, 64)
2✔
346
        if err != nil {
2✔
347
                w.WriteHeader(http.StatusBadRequest)
×
348
                rest.RenderJSON(w, rest.JSON{"error": "can't parse user id", "details": err.Error()})
×
349
                return
×
350
        }
×
351

352
        si, err := s.DetectedSpam.FindByUserID(r.Context(), userID)
2✔
353
        if err != nil {
2✔
354
                w.WriteHeader(http.StatusInternalServerError)
×
355
                rest.RenderJSON(w, rest.JSON{"error": "can't get user info", "details": err.Error()})
×
356
                return
×
357
        }
×
358
        if si != nil {
3✔
359
                resp.Status = "spam"
1✔
360
                resp.Info = &info{
1✔
361
                        UserName:  si.UserName,
1✔
362
                        Message:   si.Text,
1✔
363
                        Timestamp: si.Timestamp,
1✔
364
                        Checks:    si.Checks,
1✔
365
                }
1✔
366
        }
1✔
367
        rest.RenderJSON(w, resp)
2✔
368
}
369

370
// getDynamicSamplesHandler handles GET /samples request. It returns dynamic samples both for spam and ham.
371
func (s *Server) getDynamicSamplesHandler(w http.ResponseWriter, _ *http.Request) {
2✔
372
        spam, ham, err := s.SpamFilter.DynamicSamples()
2✔
373
        if err != nil {
3✔
374
                w.WriteHeader(http.StatusInternalServerError)
1✔
375
                rest.RenderJSON(w, rest.JSON{"error": "can't get dynamic samples", "details": err.Error()})
1✔
376
                return
1✔
377
        }
1✔
378
        rest.RenderJSON(w, rest.JSON{"spam": spam, "ham": ham})
1✔
379
}
380

381
// downloadSampleHandler handles GET /download/spam|ham request. It returns dynamic samples both for spam and ham.
382
func (s *Server) downloadSampleHandler(pickFn func(spam, ham []string) ([]string, string)) func(w http.ResponseWriter, r *http.Request) {
13✔
383
        return func(w http.ResponseWriter, _ *http.Request) {
16✔
384
                spam, ham, err := s.SpamFilter.DynamicSamples()
3✔
385
                if err != nil {
4✔
386
                        w.WriteHeader(http.StatusInternalServerError)
1✔
387
                        rest.RenderJSON(w, rest.JSON{"error": "can't get dynamic samples", "details": err.Error()})
1✔
388
                        return
1✔
389
                }
1✔
390
                samples, name := pickFn(spam, ham)
2✔
391
                body := strings.Join(samples, "\n")
2✔
392
                w.Header().Set("Content-Type", "text/plain; charset=utf-8")
2✔
393
                w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", name))
2✔
394
                w.Header().Set("Content-Length", strconv.Itoa(len(body)))
2✔
395
                w.WriteHeader(http.StatusOK)
2✔
396
                _, _ = w.Write([]byte(body))
2✔
397
        }
398
}
399

400
// updateSampleHandler handles POST /update/spam|ham request. It updates dynamic samples both for spam and ham.
401
func (s *Server) updateSampleHandler(updFn func(msg string) error) func(w http.ResponseWriter, r *http.Request) {
13✔
402
        return func(w http.ResponseWriter, r *http.Request) {
18✔
403
                var req struct {
5✔
404
                        Msg string `json:"msg"`
5✔
405
                }
5✔
406

5✔
407
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
5✔
408

5✔
409
                if isHtmxRequest {
5✔
410
                        req.Msg = r.FormValue("msg")
×
411
                } else {
5✔
412
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
6✔
413
                                w.WriteHeader(http.StatusBadRequest)
1✔
414
                                rest.RenderJSON(w, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
415
                                return
1✔
416
                        }
1✔
417
                }
418

419
                err := updFn(req.Msg)
4✔
420
                if err != nil {
5✔
421
                        w.WriteHeader(http.StatusInternalServerError)
1✔
422
                        rest.RenderJSON(w, rest.JSON{"error": "can't update samples", "details": err.Error()})
1✔
423
                        return
1✔
424
                }
1✔
425

426
                if isHtmxRequest {
3✔
427
                        s.renderSamples(w, "samples_list")
×
428
                } else {
3✔
429
                        rest.RenderJSON(w, rest.JSON{"updated": true, "msg": req.Msg})
3✔
430
                }
3✔
431
        }
432
}
433

434
// deleteSampleHandler handles DELETE /samples request. It deletes dynamic samples both for spam and ham.
435
func (s *Server) deleteSampleHandler(delFn func(msg string) error) func(w http.ResponseWriter, r *http.Request) {
13✔
436
        return func(w http.ResponseWriter, r *http.Request) {
18✔
437
                var req struct {
5✔
438
                        Msg string `json:"msg"`
5✔
439
                }
5✔
440
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
5✔
441
                if isHtmxRequest {
6✔
442
                        req.Msg = r.FormValue("msg")
1✔
443
                } else {
5✔
444
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
4✔
445
                                w.WriteHeader(http.StatusBadRequest)
×
446
                                rest.RenderJSON(w, rest.JSON{"error": "can't decode request", "details": err.Error()})
×
447
                                return
×
448
                        }
×
449
                }
450

451
                if err := delFn(req.Msg); err != nil {
6✔
452
                        w.WriteHeader(http.StatusInternalServerError)
1✔
453
                        rest.RenderJSON(w, rest.JSON{"error": "can't delete sample", "details": err.Error()})
1✔
454
                        return
1✔
455
                }
1✔
456

457
                if isHtmxRequest {
5✔
458
                        s.renderSamples(w, "samples_list")
1✔
459
                } else {
4✔
460
                        rest.RenderJSON(w, rest.JSON{"deleted": true, "msg": req.Msg, "count": 1})
3✔
461
                }
3✔
462
        }
463
}
464

465
// reloadDynamicSamplesHandler handles PUT /samples request. It reloads dynamic samples from db storage.
466
func (s *Server) reloadDynamicSamplesHandler(w http.ResponseWriter, _ *http.Request) {
2✔
467
        if err := s.SpamFilter.ReloadSamples(); err != nil {
3✔
468
                w.WriteHeader(http.StatusInternalServerError)
1✔
469
                rest.RenderJSON(w, rest.JSON{"error": "can't reload samples", "details": err.Error()})
1✔
470
                return
1✔
471
        }
1✔
472
        rest.RenderJSON(w, rest.JSON{"reloaded": true})
1✔
473
}
474

475
// updateApprovedUsersHandler handles POST /users/add and /users/delete requests, it adds or removes users from approved list.
476
func (s *Server) updateApprovedUsersHandler(updFn func(ui approved.UserInfo) error) func(w http.ResponseWriter, r *http.Request) {
14✔
477
        return func(w http.ResponseWriter, r *http.Request) {
23✔
478
                req := approved.UserInfo{}
9✔
479
                isHtmxRequest := r.Header.Get("HX-Request") == "true"
9✔
480
                if isHtmxRequest {
10✔
481
                        req.UserID = r.FormValue("user_id")
1✔
482
                        req.UserName = r.FormValue("user_name")
1✔
483
                } else {
9✔
484
                        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
9✔
485
                                w.WriteHeader(http.StatusBadRequest)
1✔
486
                                rest.RenderJSON(w, rest.JSON{"error": "can't decode request", "details": err.Error()})
1✔
487
                                return
1✔
488
                        }
1✔
489
                }
490

491
                // try to get userID from request and fallback to userName lookup if it's empty
492
                if req.UserID == "" {
12✔
493
                        req.UserID = strconv.FormatInt(s.Locator.UserIDByName(r.Context(), req.UserName), 10)
4✔
494
                }
4✔
495

496
                if req.UserID == "" || req.UserID == "0" {
9✔
497
                        if isHtmxRequest {
1✔
498
                                w.Header().Set("HX-Retarget", "#error-message")
×
499
                                fmt.Fprintln(w, "<div class='alert alert-danger'>Either userid or valid username required.</div>")
×
500
                                return
×
501
                        }
×
502
                        w.WriteHeader(http.StatusBadRequest)
1✔
503
                        rest.RenderJSON(w, rest.JSON{"error": "user ID is required"})
1✔
504
                        return
1✔
505
                }
506

507
                // add or remove user from the approved list of detector
508
                if err := updFn(req); err != nil {
7✔
509
                        w.WriteHeader(http.StatusInternalServerError)
×
510
                        rest.RenderJSON(w, rest.JSON{"error": "can't update approved users", "details": err.Error()})
×
511
                        return
×
512
                }
×
513

514
                if isHtmxRequest {
8✔
515
                        users := s.Detector.ApprovedUsers()
1✔
516
                        tmplData := struct {
1✔
517
                                ApprovedUsers      []approved.UserInfo
1✔
518
                                TotalApprovedUsers int
1✔
519
                        }{
1✔
520
                                ApprovedUsers:      users,
1✔
521
                                TotalApprovedUsers: len(users),
1✔
522
                        }
1✔
523

1✔
524
                        if err := tmpl.ExecuteTemplate(w, "users_list", tmplData); err != nil {
1✔
525
                                http.Error(w, "Error executing template", http.StatusInternalServerError)
×
526
                                return
×
527
                        }
×
528

529
                } else {
6✔
530
                        rest.RenderJSON(w, rest.JSON{"updated": true, "user_id": req.UserID, "user_name": req.UserName})
6✔
531
                }
6✔
532
        }
533
}
534

535
// removeApprovedUser is adopter for updateApprovedUsersHandler updFn
536
func (s *Server) removeApprovedUser(req approved.UserInfo) error {
2✔
537
        return s.Detector.RemoveApprovedUser(req.UserID)
2✔
538
}
2✔
539

540
// getApprovedUsersHandler handles GET /users request. It returns list of approved users.
541
func (s *Server) getApprovedUsersHandler(w http.ResponseWriter, _ *http.Request) {
1✔
542
        rest.RenderJSON(w, rest.JSON{"user_ids": s.Detector.ApprovedUsers()})
1✔
543
}
1✔
544

545
// getSettingsHandler returns application settings, including the list of available Lua plugins
546
func (s *Server) getSettingsHandler(w http.ResponseWriter, _ *http.Request) {
3✔
547
        // get the list of available Lua plugins before returning settings
3✔
548
        s.Settings.LuaAvailablePlugins = s.Detector.GetLuaPluginNames()
3✔
549
        rest.RenderJSON(w, s.Settings)
3✔
550
}
3✔
551

552
// htmlSpamCheckHandler handles GET / request.
553
// It returns rendered spam_check.html template with all the components.
554
func (s *Server) htmlSpamCheckHandler(w http.ResponseWriter, _ *http.Request) {
3✔
555
        tmplData := struct {
3✔
556
                Version string
3✔
557
        }{
3✔
558
                Version: s.Version,
3✔
559
        }
3✔
560

3✔
561
        if err := tmpl.ExecuteTemplate(w, "spam_check.html", tmplData); err != nil {
4✔
562
                log.Printf("[WARN] can't execute template: %v", err)
1✔
563
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
564
                return
1✔
565
        }
1✔
566
}
567

568
// htmlManageSamplesHandler handles GET /manage_samples request.
569
// It returns rendered manage_samples.html template with all the components.
570
func (s *Server) htmlManageSamplesHandler(w http.ResponseWriter, _ *http.Request) {
1✔
571
        s.renderSamples(w, "manage_samples.html")
1✔
572
}
1✔
573

574
func (s *Server) htmlManageUsersHandler(w http.ResponseWriter, _ *http.Request) {
3✔
575
        users := s.Detector.ApprovedUsers()
3✔
576
        tmplData := struct {
3✔
577
                ApprovedUsers      []approved.UserInfo
3✔
578
                TotalApprovedUsers int
3✔
579
        }{
3✔
580
                ApprovedUsers:      users,
3✔
581
                TotalApprovedUsers: len(users),
3✔
582
        }
3✔
583
        tmplData.TotalApprovedUsers = len(tmplData.ApprovedUsers)
3✔
584

3✔
585
        if err := tmpl.ExecuteTemplate(w, "manage_users.html", tmplData); err != nil {
4✔
586
                log.Printf("[WARN] can't execute template: %v", err)
1✔
587
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
588
                return
1✔
589
        }
1✔
590
}
591

592
func (s *Server) htmlDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
2✔
593
        ds, err := s.DetectedSpam.Read(r.Context())
2✔
594
        if err != nil {
3✔
595
                log.Printf("[ERROR] Failed to fetch detected spam: %v", err)
1✔
596
                http.Error(w, "Internal Server Error", http.StatusInternalServerError)
1✔
597
                return
1✔
598
        }
1✔
599

600
        // clean up detected spam entries
601
        for i, d := range ds {
3✔
602
                d.Text = strings.ReplaceAll(d.Text, "'", " ")
2✔
603
                d.Text = strings.ReplaceAll(d.Text, "\n", " ")
2✔
604
                d.Text = strings.ReplaceAll(d.Text, "\r", " ")
2✔
605
                d.Text = strings.ReplaceAll(d.Text, "\t", " ")
2✔
606
                d.Text = strings.ReplaceAll(d.Text, "\"", " ")
2✔
607
                d.Text = strings.ReplaceAll(d.Text, "\\", " ")
2✔
608
                ds[i] = d
2✔
609
        }
2✔
610

611
        // get filter from query param, default to "all"
612
        filter := r.URL.Query().Get("filter")
1✔
613
        if filter == "" {
2✔
614
                filter = "all"
1✔
615
        }
1✔
616

617
        // apply filtering
618
        var filteredDS []storage.DetectedSpamInfo
1✔
619
        switch filter {
1✔
UNCOV
620
        case "non-classified":
×
UNCOV
621
                for _, entry := range ds {
×
622
                        hasClassifierHam := false
×
623
                        for _, check := range entry.Checks {
×
624
                                if check.Name == "classifier" && !check.Spam {
×
UNCOV
625
                                        hasClassifierHam = true
×
626
                                        break
×
627
                                }
628
                        }
629
                        if hasClassifierHam {
×
630
                                filteredDS = append(filteredDS, entry)
×
631
                        }
×
632
                }
UNCOV
633
        case "openai":
×
UNCOV
634
                for _, entry := range ds {
×
635
                        hasOpenAI := false
×
636
                        for _, check := range entry.Checks {
×
637
                                if check.Name == "openai" {
×
UNCOV
638
                                        hasOpenAI = true
×
UNCOV
639
                                        break
×
640
                                }
641
                        }
UNCOV
642
                        if hasOpenAI {
×
UNCOV
643
                                filteredDS = append(filteredDS, entry)
×
UNCOV
644
                        }
×
645
                }
646
        default: // "all" or any other value
1✔
647
                filteredDS = ds
1✔
648
        }
649

650
        tmplData := struct {
1✔
651
                DetectedSpamEntries []storage.DetectedSpamInfo
1✔
652
                TotalDetectedSpam   int
1✔
653
                FilteredCount       int
1✔
654
                Filter              string
1✔
655
                OpenAIEnabled       bool
1✔
656
        }{
1✔
657
                DetectedSpamEntries: filteredDS,
1✔
658
                TotalDetectedSpam:   len(ds),
1✔
659
                FilteredCount:       len(filteredDS),
1✔
660
                Filter:              filter,
1✔
661
                OpenAIEnabled:       s.Settings.OpenAIEnabled,
1✔
662
        }
1✔
663

1✔
664
        // if it's an HTMX request, render both content and count display for OOB swap
1✔
665
        if r.Header.Get("HX-Request") == "true" {
1✔
666
                var buf bytes.Buffer
×
UNCOV
667

×
UNCOV
668
                // first render the content template
×
669
                if err := tmpl.ExecuteTemplate(&buf, "detected_spam_content", tmplData); err != nil {
×
670
                        log.Printf("[WARN] can't execute content template: %v", err)
×
671
                        http.Error(w, "Error executing template", http.StatusInternalServerError)
×
672
                        return
×
673
                }
×
674

675
                // then append OOB swap for the count display
676
                countHTML := ""
×
677
                if filter != "all" {
×
678
                        countHTML = fmt.Sprintf("(%d/%d)", len(filteredDS), len(ds))
×
679
                } else {
×
680
                        countHTML = fmt.Sprintf("(%d)", len(ds))
×
681
                }
×
682

UNCOV
683
                buf.WriteString(fmt.Sprintf(`<span id="count-display" hx-swap-oob="true">%s</span>`, countHTML))
×
UNCOV
684

×
UNCOV
685
                // write the combined response
×
UNCOV
686
                if _, err := buf.WriteTo(w); err != nil {
×
687
                        log.Printf("[WARN] failed to write response: %v", err)
×
688
                }
×
689
                return
×
690
        }
691

692
        // full page render for normal requests
693
        if err := tmpl.ExecuteTemplate(w, "detected_spam.html", tmplData); err != nil {
1✔
UNCOV
694
                log.Printf("[WARN] can't execute template: %v", err)
×
UNCOV
695
                http.Error(w, "Error executing template", http.StatusInternalServerError)
×
UNCOV
696
                return
×
UNCOV
697
        }
×
698
}
699

700
func (s *Server) htmlAddDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
5✔
701
        reportErr := func(err error, _ int) {
9✔
702
                w.Header().Set("HX-Retarget", "#error-message")
4✔
703
                fmt.Fprintf(w, "<div class='alert alert-danger'>%s</div>", err)
4✔
704
        }
4✔
705
        msg := r.FormValue("msg")
5✔
706

5✔
707
        id, err := strconv.ParseInt(r.FormValue("id"), 10, 64)
5✔
708
        if err != nil || msg == "" {
7✔
709
                log.Printf("[WARN] bad request: %v", err)
2✔
710
                reportErr(fmt.Errorf("bad request: %v", err), http.StatusBadRequest)
2✔
711
                return
2✔
712
        }
2✔
713

714
        if err := s.SpamFilter.UpdateSpam(msg); err != nil {
4✔
715
                log.Printf("[WARN] failed to update spam samples: %v", err)
1✔
716
                reportErr(fmt.Errorf("can't update spam samples: %v", err), http.StatusInternalServerError)
1✔
717
                return
1✔
718

1✔
719
        }
1✔
720
        if err := s.DetectedSpam.SetAddedToSamplesFlag(r.Context(), id); err != nil {
3✔
721
                log.Printf("[WARN] failed to update detected spam: %v", err)
1✔
722
                reportErr(fmt.Errorf("can't update detected spam: %v", err), http.StatusInternalServerError)
1✔
723
                return
1✔
724
        }
1✔
725
        w.WriteHeader(http.StatusOK)
1✔
726
}
727

728
func (s *Server) htmlSettingsHandler(w http.ResponseWriter, _ *http.Request) {
4✔
729
        // get database information if StorageEngine is available
4✔
730
        var dbInfo struct {
4✔
731
                DatabaseType   string `json:"database_type"`
4✔
732
                GID            string `json:"gid"`
4✔
733
                DatabaseStatus string `json:"database_status"`
4✔
734
        }
4✔
735

4✔
736
        if s.StorageEngine != nil {
6✔
737
                // try to cast to SQL engine to get type information
2✔
738
                if sqlEngine, ok := s.StorageEngine.(*engine.SQL); ok {
2✔
UNCOV
739
                        dbInfo.DatabaseType = string(sqlEngine.Type())
×
UNCOV
740
                        dbInfo.GID = sqlEngine.GID()
×
UNCOV
741
                        dbInfo.DatabaseStatus = "Connected"
×
742
                } else {
2✔
743
                        dbInfo.DatabaseType = "Unknown"
2✔
744
                        dbInfo.DatabaseStatus = "Connected (unknown type)"
2✔
745
                }
2✔
746
        } else {
2✔
747
                dbInfo.DatabaseStatus = "Not connected"
2✔
748
        }
2✔
749

750
        // get backup information
751
        backupURL := "/download/backup"
4✔
752
        backupFilename := fmt.Sprintf("tg-spam-backup-%s-%s.sql.gz", dbInfo.DatabaseType, time.Now().Format("20060102-150405"))
4✔
753

4✔
754
        // get system info - uptime since server start
4✔
755
        uptime := time.Since(startTime)
4✔
756

4✔
757
        // get the list of available Lua plugins
4✔
758
        s.Settings.LuaAvailablePlugins = s.Detector.GetLuaPluginNames()
4✔
759

4✔
760
        data := struct {
4✔
761
                Settings
4✔
762
                Version  string
4✔
763
                Database struct {
4✔
764
                        Type   string
4✔
765
                        GID    string
4✔
766
                        Status string
4✔
767
                }
4✔
768
                Backup struct {
4✔
769
                        URL      string
4✔
770
                        Filename string
4✔
771
                }
4✔
772
                System struct {
4✔
773
                        Uptime string
4✔
774
                }
4✔
775
        }{
4✔
776
                Settings: s.Settings,
4✔
777
                Version:  s.Version,
4✔
778
                Database: struct {
4✔
779
                        Type   string
4✔
780
                        GID    string
4✔
781
                        Status string
4✔
782
                }{
4✔
783
                        Type:   dbInfo.DatabaseType,
4✔
784
                        GID:    dbInfo.GID,
4✔
785
                        Status: dbInfo.DatabaseStatus,
4✔
786
                },
4✔
787
                Backup: struct {
4✔
788
                        URL      string
4✔
789
                        Filename string
4✔
790
                }{
4✔
791
                        URL:      backupURL,
4✔
792
                        Filename: backupFilename,
4✔
793
                },
4✔
794
                System: struct {
4✔
795
                        Uptime string
4✔
796
                }{
4✔
797
                        Uptime: formatDuration(uptime),
4✔
798
                },
4✔
799
        }
4✔
800

4✔
801
        if err := tmpl.ExecuteTemplate(w, "settings.html", data); err != nil {
5✔
802
                log.Printf("[WARN] can't execute template: %v", err)
1✔
803
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
804
                return
1✔
805
        }
1✔
806
}
807

808
// formatDuration formats a duration in a human-readable way
809
func formatDuration(d time.Duration) string {
12✔
810
        days := int(d.Hours() / 24)
12✔
811
        hours := int(d.Hours()) % 24
12✔
812
        minutes := int(d.Minutes()) % 60
12✔
813

12✔
814
        if days > 0 {
15✔
815
                return fmt.Sprintf("%dd %dh %dm", days, hours, minutes)
3✔
816
        }
3✔
817

818
        if hours > 0 {
11✔
819
                return fmt.Sprintf("%dh %dm", hours, minutes)
2✔
820
        }
2✔
821

822
        return fmt.Sprintf("%dm", minutes)
7✔
823
}
824

825
func (s *Server) downloadDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
3✔
826
        ctx := r.Context()
3✔
827
        spam, err := s.DetectedSpam.Read(ctx)
3✔
828
        if err != nil {
4✔
829
                w.WriteHeader(http.StatusInternalServerError)
1✔
830
                rest.RenderJSON(w, rest.JSON{"error": "can't get detected spam", "details": err.Error()})
1✔
831
                return
1✔
832
        }
1✔
833

834
        type jsonSpamInfo struct {
2✔
835
                ID        int64                `json:"id"`
2✔
836
                GID       string               `json:"gid"`
2✔
837
                Text      string               `json:"text"`
2✔
838
                UserID    int64                `json:"user_id"`
2✔
839
                UserName  string               `json:"user_name"`
2✔
840
                Timestamp time.Time            `json:"timestamp"`
2✔
841
                Added     bool                 `json:"added"`
2✔
842
                Checks    []spamcheck.Response `json:"checks"`
2✔
843
        }
2✔
844

2✔
845
        // convert entries to jsonl format with lowercase fields
2✔
846
        lines := make([]string, 0, len(spam))
2✔
847
        for _, entry := range spam {
5✔
848
                data, err := json.Marshal(jsonSpamInfo{
3✔
849
                        ID:        entry.ID,
3✔
850
                        GID:       entry.GID,
3✔
851
                        Text:      entry.Text,
3✔
852
                        UserID:    entry.UserID,
3✔
853
                        UserName:  entry.UserName,
3✔
854
                        Timestamp: entry.Timestamp,
3✔
855
                        Added:     entry.Added,
3✔
856
                        Checks:    entry.Checks,
3✔
857
                })
3✔
858
                if err != nil {
3✔
UNCOV
859
                        w.WriteHeader(http.StatusInternalServerError)
×
UNCOV
860
                        rest.RenderJSON(w, rest.JSON{"error": "can't marshal entry", "details": err.Error()})
×
UNCOV
861
                        return
×
UNCOV
862
                }
×
863
                lines = append(lines, string(data))
3✔
864
        }
865

866
        body := strings.Join(lines, "\n")
2✔
867
        w.Header().Set("Content-Type", "application/x-jsonlines")
2✔
868
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", "detected_spam.jsonl"))
2✔
869
        w.Header().Set("Content-Length", strconv.Itoa(len(body)))
2✔
870
        w.WriteHeader(http.StatusOK)
2✔
871
        _, _ = w.Write([]byte(body))
2✔
872
}
873

874
// downloadBackupHandler streams a database backup as an SQL file with gzip compression
875
// Files are always compressed and always have .gz extension to ensure consistency
876
func (s *Server) downloadBackupHandler(w http.ResponseWriter, r *http.Request) {
2✔
877
        if s.StorageEngine == nil {
3✔
878
                w.WriteHeader(http.StatusInternalServerError)
1✔
879
                rest.RenderJSON(w, rest.JSON{"error": "storage engine not available"})
1✔
880
                return
1✔
881
        }
1✔
882

883
        // set filename based on database type and timestamp
884
        dbType := "db"
1✔
885
        sqlEng, ok := s.StorageEngine.(*engine.SQL)
1✔
886
        if ok {
1✔
UNCOV
887
                dbType = string(sqlEng.Type())
×
UNCOV
888
        }
×
889
        timestamp := time.Now().Format("20060102-150405")
1✔
890

1✔
891
        // always use a .gz extension as the content is always compressed
1✔
892
        filename := fmt.Sprintf("tg-spam-backup-%s-%s.sql.gz", dbType, timestamp)
1✔
893

1✔
894
        // set headers for file download - note we're using application/octet-stream
1✔
895
        // instead of application/sql to prevent browsers from trying to interpret the file
1✔
896
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
897
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
898
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
899
        w.Header().Set("Pragma", "no-cache")
1✔
900
        w.Header().Set("Expires", "0")
1✔
901

1✔
902
        // create a gzip writer that streams to response
1✔
903
        gzipWriter := gzip.NewWriter(w)
1✔
904
        defer func() {
2✔
905
                if err := gzipWriter.Close(); err != nil {
1✔
UNCOV
906
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
UNCOV
907
                }
×
908
        }()
909

910
        // stream backup directly to response through gzip
911
        if err := s.StorageEngine.Backup(r.Context(), gzipWriter); err != nil {
1✔
UNCOV
912
                log.Printf("[ERROR] failed to create backup: %v", err)
×
UNCOV
913
                // we've already started writing the response, so we can't send a proper error response
×
UNCOV
914
                return
×
UNCOV
915
        }
×
916

917
        // flush the gzip writer to ensure all data is written
918
        if err := gzipWriter.Flush(); err != nil {
1✔
UNCOV
919
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
UNCOV
920
        }
×
921
}
922

923
// downloadExportToPostgresHandler streams a PostgreSQL-compatible export from a SQLite database
924
func (s *Server) downloadExportToPostgresHandler(w http.ResponseWriter, r *http.Request) {
3✔
925
        if s.StorageEngine == nil {
4✔
926
                w.WriteHeader(http.StatusInternalServerError)
1✔
927
                rest.RenderJSON(w, rest.JSON{"error": "storage engine not available"})
1✔
928
                return
1✔
929
        }
1✔
930

931
        // check if the database is SQLite
932
        if s.StorageEngine.Type() != engine.Sqlite {
3✔
933
                w.WriteHeader(http.StatusBadRequest)
1✔
934
                rest.RenderJSON(w, rest.JSON{"error": "source database must be SQLite"})
1✔
935
                return
1✔
936
        }
1✔
937

938
        // set filename based on timestamp
939
        timestamp := time.Now().Format("20060102-150405")
1✔
940
        filename := fmt.Sprintf("tg-spam-sqlite-to-postgres-%s.sql.gz", timestamp)
1✔
941

1✔
942
        // set headers for file download
1✔
943
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
944
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
945
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
946
        w.Header().Set("Pragma", "no-cache")
1✔
947
        w.Header().Set("Expires", "0")
1✔
948

1✔
949
        // create a gzip writer that streams to response
1✔
950
        gzipWriter := gzip.NewWriter(w)
1✔
951
        defer func() {
2✔
952
                if err := gzipWriter.Close(); err != nil {
1✔
UNCOV
953
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
UNCOV
954
                }
×
955
        }()
956

957
        // stream export directly to response through gzip
958
        if err := s.StorageEngine.BackupSqliteAsPostgres(r.Context(), gzipWriter); err != nil {
1✔
UNCOV
959
                log.Printf("[ERROR] failed to create export: %v", err)
×
UNCOV
960
                // we've already started writing the response, so we can't send a proper error response
×
UNCOV
961
                return
×
UNCOV
962
        }
×
963

964
        // flush the gzip writer to ensure all data is written
965
        if err := gzipWriter.Flush(); err != nil {
1✔
UNCOV
966
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
UNCOV
967
        }
×
968
}
969

970
func (s *Server) renderSamples(w http.ResponseWriter, tmplName string) {
6✔
971
        spam, ham, err := s.SpamFilter.DynamicSamples()
6✔
972
        if err != nil {
7✔
973
                w.WriteHeader(http.StatusInternalServerError)
1✔
974
                rest.RenderJSON(w, rest.JSON{"error": "can't fetch samples", "details": err.Error()})
1✔
975
                return
1✔
976
        }
1✔
977

978
        spam, ham = s.reverseSamples(spam, ham)
5✔
979

5✔
980
        type smpleWithID struct {
5✔
981
                ID     string
5✔
982
                Sample string
5✔
983
        }
5✔
984

5✔
985
        makeID := func(s string) string {
19✔
986
                hash := sha1.New() //nolint
14✔
987
                if _, err := hash.Write([]byte(s)); err != nil {
14✔
UNCOV
988
                        return fmt.Sprintf("%x", s)
×
UNCOV
989
                }
×
990
                return fmt.Sprintf("%x", hash.Sum(nil))
14✔
991
        }
992

993
        tmplData := struct {
5✔
994
                SpamSamples      []smpleWithID
5✔
995
                HamSamples       []smpleWithID
5✔
996
                TotalHamSamples  int
5✔
997
                TotalSpamSamples int
5✔
998
        }{
5✔
999
                TotalHamSamples:  len(ham),
5✔
1000
                TotalSpamSamples: len(spam),
5✔
1001
        }
5✔
1002
        for _, s := range spam {
12✔
1003
                tmplData.SpamSamples = append(tmplData.SpamSamples, smpleWithID{ID: makeID(s), Sample: s})
7✔
1004
        }
7✔
1005
        for _, h := range ham {
12✔
1006
                tmplData.HamSamples = append(tmplData.HamSamples, smpleWithID{ID: makeID(h), Sample: h})
7✔
1007
        }
7✔
1008

1009
        if err := tmpl.ExecuteTemplate(w, tmplName, tmplData); err != nil {
6✔
1010
                w.WriteHeader(http.StatusInternalServerError)
1✔
1011
                rest.RenderJSON(w, rest.JSON{"error": "can't execute template", "details": err.Error()})
1✔
1012
                return
1✔
1013
        }
1✔
1014
}
1015

1016
func (s *Server) authMiddleware(mw func(next http.Handler) http.Handler) func(next http.Handler) http.Handler {
10✔
1017
        if s.AuthPasswd == "" {
16✔
1018
                return func(next http.Handler) http.Handler {
105✔
1019
                        return next
99✔
1020
                }
99✔
1021
        }
1022
        return func(next http.Handler) http.Handler {
70✔
1023
                return mw(next)
66✔
1024
        }
66✔
1025
}
1026

1027
// reverseSamples returns reversed lists of spam and ham samples
1028
func (s *Server) reverseSamples(spam, ham []string) (revSpam, revHam []string) {
8✔
1029
        revSpam = make([]string, len(spam))
8✔
1030
        revHam = make([]string, len(ham))
8✔
1031

8✔
1032
        for i, j := 0, len(spam)-1; i < len(spam); i, j = i+1, j-1 {
19✔
1033
                revSpam[i] = spam[j]
11✔
1034
        }
11✔
1035
        for i, j := 0, len(ham)-1; i < len(ham); i, j = i+1, j-1 {
19✔
1036
                revHam[i] = ham[j]
11✔
1037
        }
11✔
1038
        return revSpam, revHam
8✔
1039
}
1040

1041
// staticFS is a filtered filesystem that only exposes specific static files
1042
type staticFS struct {
1043
        fs        fs.FS
1044
        urlToPath map[string]string
1045
}
1046

1047
// staticFileMapping defines a mapping between URL path and filesystem path
1048
type staticFileMapping struct {
1049
        urlPath     string
1050
        filesysPath string
1051
}
1052

1053
func newStaticFS(fsys fs.FS, files ...staticFileMapping) *staticFS {
5✔
1054
        urlToPath := make(map[string]string)
5✔
1055
        for _, f := range files {
20✔
1056
                urlToPath[f.urlPath] = f.filesysPath
15✔
1057
        }
15✔
1058

1059
        return &staticFS{
5✔
1060
                fs:        fsys,
5✔
1061
                urlToPath: urlToPath,
5✔
1062
        }
5✔
1063
}
1064

1065
func (sfs *staticFS) Open(name string) (fs.File, error) {
5✔
1066
        name = path.Clean("/" + name)[1:]
5✔
1067
        if fsPath, ok := sfs.urlToPath[name]; ok {
8✔
1068
                return sfs.fs.Open(fsPath)
3✔
1069
        }
3✔
1070
        return nil, fs.ErrNotExist
2✔
1071
}
1072

1073
// GenerateRandomPassword generates a random password of a given length
1074
func GenerateRandomPassword(length int) (string, error) {
2✔
1075
        const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+"
2✔
1076

2✔
1077
        var password strings.Builder
2✔
1078
        charsetSize := big.NewInt(int64(len(charset)))
2✔
1079

2✔
1080
        for i := 0; i < length; i++ {
66✔
1081
                randomNumber, err := rand.Int(rand.Reader, charsetSize)
64✔
1082
                if err != nil {
64✔
UNCOV
1083
                        return "", err
×
UNCOV
1084
                }
×
1085

1086
                password.WriteByte(charset[randomNumber.Int64()])
64✔
1087
        }
1088

1089
        return password.String(), nil
2✔
1090
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc