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

umputun / tg-spam / 14280449854

05 Apr 2025 09:09AM UTC coverage: 80.107% (-1.1%) from 81.199%
14280449854

Pull #282

github

umputun
Expose Lua plugins in settings API and UI

- Add Lua plugins settings to webapi.Settings struct
- Update activateServer to populate Lua plugin settings
- Add Lua plugins tab to settings.html template
Pull Request #282: Add Lua plugin support for custom spam detection

4635 of 5786 relevant lines covered (80.11%)

59.03 hits per line

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

83.76
/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
        SamplesDataPath         string        `json:"samples_data_path"`
95
        DynamicDataPath         string        `json:"dynamic_data_path"`
96
        WatchIntervalSecs       int           `json:"watch_interval_secs"`
97
        SimilarityThreshold     float64       `json:"similarity_threshold"`
98
        MinMsgLen               int           `json:"min_msg_len"`
99
        MaxEmoji                int           `json:"max_emoji"`
100
        MinSpamProbability      float64       `json:"min_spam_probability"`
101
        ParanoidMode            bool          `json:"paranoid_mode"`
102
        FirstMessagesCount      int           `json:"first_messages_count"`
103
        StartupMessageEnabled   bool          `json:"startup_message_enabled"`
104
        TrainingEnabled         bool          `json:"training_enabled"`
105
        StorageTimeout          time.Duration `json:"storage_timeout"`
106
        OpenAIVeto              bool          `json:"openai_veto"`
107
        OpenAIHistorySize       int           `json:"openai_history_size"`
108
        OpenAIModel             string        `json:"openai_model"`
109
        SoftBanEnabled          bool          `json:"soft_ban_enabled"`
110
        AbnormalSpacingEnabled  bool          `json:"abnormal_spacing_enabled"`
111
        HistorySize             int           `json:"history_size"`
112
        DebugModeEnabled        bool          `json:"debug_mode_enabled"`
113
        DryModeEnabled          bool          `json:"dry_mode_enabled"`
114
        TGDebugModeEnabled      bool          `json:"tg_debug_mode_enabled"`
115
}
116

117
// Detector is a spam detector interface.
118
type Detector interface {
119
        Check(req spamcheck.Request) (spam bool, cr []spamcheck.Response)
120
        ApprovedUsers() []approved.UserInfo
121
        AddApprovedUser(user approved.UserInfo) error
122
        RemoveApprovedUser(id string) error
123
}
124

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

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

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

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

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

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

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

181
        router = s.routes(router) // setup routes
3✔
182

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

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

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

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

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

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

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

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

243
                authApi.HandleFunc("GET /settings", func(w http.ResponseWriter, _ *http.Request) {
6✔
244
                        rest.RenderJSON(w, s.Settings)
1✔
245
                })
1✔
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
// htmlSpamCheckHandler handles GET / request.
546
// It returns rendered spam_check.html template with all the components.
547
func (s *Server) htmlSpamCheckHandler(w http.ResponseWriter, _ *http.Request) {
3✔
548
        tmplData := struct {
3✔
549
                Version string
3✔
550
        }{
3✔
551
                Version: s.Version,
3✔
552
        }
3✔
553

3✔
554
        if err := tmpl.ExecuteTemplate(w, "spam_check.html", tmplData); err != nil {
4✔
555
                log.Printf("[WARN] can't execute template: %v", err)
1✔
556
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
557
                return
1✔
558
        }
1✔
559
}
560

561
// htmlManageSamplesHandler handles GET /manage_samples request.
562
// It returns rendered manage_samples.html template with all the components.
563
func (s *Server) htmlManageSamplesHandler(w http.ResponseWriter, _ *http.Request) {
1✔
564
        s.renderSamples(w, "manage_samples.html")
1✔
565
}
1✔
566

567
func (s *Server) htmlManageUsersHandler(w http.ResponseWriter, _ *http.Request) {
3✔
568
        users := s.Detector.ApprovedUsers()
3✔
569
        tmplData := struct {
3✔
570
                ApprovedUsers      []approved.UserInfo
3✔
571
                TotalApprovedUsers int
3✔
572
        }{
3✔
573
                ApprovedUsers:      users,
3✔
574
                TotalApprovedUsers: len(users),
3✔
575
        }
3✔
576
        tmplData.TotalApprovedUsers = len(tmplData.ApprovedUsers)
3✔
577

3✔
578
        if err := tmpl.ExecuteTemplate(w, "manage_users.html", tmplData); err != nil {
4✔
579
                log.Printf("[WARN] can't execute template: %v", err)
1✔
580
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
581
                return
1✔
582
        }
1✔
583
}
584

585
func (s *Server) htmlDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
2✔
586
        ds, err := s.DetectedSpam.Read(r.Context())
2✔
587
        if err != nil {
3✔
588
                log.Printf("[ERROR] Failed to fetch detected spam: %v", err)
1✔
589
                http.Error(w, "Internal Server Error", http.StatusInternalServerError)
1✔
590
                return
1✔
591
        }
1✔
592

593
        // clean up detected spam entries
594
        for i, d := range ds {
3✔
595
                d.Text = strings.ReplaceAll(d.Text, "'", " ")
2✔
596
                d.Text = strings.ReplaceAll(d.Text, "\n", " ")
2✔
597
                d.Text = strings.ReplaceAll(d.Text, "\r", " ")
2✔
598
                d.Text = strings.ReplaceAll(d.Text, "\t", " ")
2✔
599
                d.Text = strings.ReplaceAll(d.Text, "\"", " ")
2✔
600
                d.Text = strings.ReplaceAll(d.Text, "\\", " ")
2✔
601
                ds[i] = d
2✔
602
        }
2✔
603

604
        // get filter from query param, default to "all"
605
        filter := r.URL.Query().Get("filter")
1✔
606
        if filter == "" {
2✔
607
                filter = "all"
1✔
608
        }
1✔
609

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

643
        tmplData := struct {
1✔
644
                DetectedSpamEntries []storage.DetectedSpamInfo
1✔
645
                TotalDetectedSpam   int
1✔
646
                FilteredCount       int
1✔
647
                Filter              string
1✔
648
                OpenAIEnabled       bool
1✔
649
        }{
1✔
650
                DetectedSpamEntries: filteredDS,
1✔
651
                TotalDetectedSpam:   len(ds),
1✔
652
                FilteredCount:       len(filteredDS),
1✔
653
                Filter:              filter,
1✔
654
                OpenAIEnabled:       s.Settings.OpenAIEnabled,
1✔
655
        }
1✔
656

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

×
661
                // first render the content template
×
662
                if err := tmpl.ExecuteTemplate(&buf, "detected_spam_content", tmplData); err != nil {
×
663
                        log.Printf("[WARN] can't execute content template: %v", err)
×
664
                        http.Error(w, "Error executing template", http.StatusInternalServerError)
×
665
                        return
×
666
                }
×
667

668
                // then append OOB swap for the count display
669
                countHTML := ""
×
670
                if filter != "all" {
×
671
                        countHTML = fmt.Sprintf("(%d/%d)", len(filteredDS), len(ds))
×
672
                } else {
×
673
                        countHTML = fmt.Sprintf("(%d)", len(ds))
×
674
                }
×
675

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

×
678
                // write the combined response
×
679
                if _, err := buf.WriteTo(w); err != nil {
×
680
                        log.Printf("[WARN] failed to write response: %v", err)
×
681
                }
×
682
                return
×
683
        }
684

685
        // full page render for normal requests
686
        if err := tmpl.ExecuteTemplate(w, "detected_spam.html", tmplData); err != nil {
1✔
687
                log.Printf("[WARN] can't execute template: %v", err)
×
688
                http.Error(w, "Error executing template", http.StatusInternalServerError)
×
689
                return
×
690
        }
×
691
}
692

693
func (s *Server) htmlAddDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
5✔
694
        reportErr := func(err error, _ int) {
9✔
695
                w.Header().Set("HX-Retarget", "#error-message")
4✔
696
                fmt.Fprintf(w, "<div class='alert alert-danger'>%s</div>", err)
4✔
697
        }
4✔
698
        msg := r.FormValue("msg")
5✔
699

5✔
700
        id, err := strconv.ParseInt(r.FormValue("id"), 10, 64)
5✔
701
        if err != nil || msg == "" {
7✔
702
                log.Printf("[WARN] bad request: %v", err)
2✔
703
                reportErr(fmt.Errorf("bad request: %v", err), http.StatusBadRequest)
2✔
704
                return
2✔
705
        }
2✔
706

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

1✔
712
        }
1✔
713
        if err := s.DetectedSpam.SetAddedToSamplesFlag(r.Context(), id); err != nil {
3✔
714
                log.Printf("[WARN] failed to update detected spam: %v", err)
1✔
715
                reportErr(fmt.Errorf("can't update detected spam: %v", err), http.StatusInternalServerError)
1✔
716
                return
1✔
717
        }
1✔
718
        w.WriteHeader(http.StatusOK)
1✔
719
}
720

721
func (s *Server) htmlSettingsHandler(w http.ResponseWriter, _ *http.Request) {
4✔
722
        // get database information if StorageEngine is available
4✔
723
        var dbInfo struct {
4✔
724
                DatabaseType   string `json:"database_type"`
4✔
725
                GID            string `json:"gid"`
4✔
726
                DatabaseStatus string `json:"database_status"`
4✔
727
        }
4✔
728

4✔
729
        if s.StorageEngine != nil {
6✔
730
                // try to cast to SQL engine to get type information
2✔
731
                if sqlEngine, ok := s.StorageEngine.(*engine.SQL); ok {
2✔
732
                        dbInfo.DatabaseType = string(sqlEngine.Type())
×
733
                        dbInfo.GID = sqlEngine.GID()
×
734
                        dbInfo.DatabaseStatus = "Connected"
×
735
                } else {
2✔
736
                        dbInfo.DatabaseType = "Unknown"
2✔
737
                        dbInfo.DatabaseStatus = "Connected (unknown type)"
2✔
738
                }
2✔
739
        } else {
2✔
740
                dbInfo.DatabaseStatus = "Not connected"
2✔
741
        }
2✔
742

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

4✔
747
        // get system info - uptime since server start
4✔
748
        uptime := time.Since(startTime)
4✔
749

4✔
750
        data := struct {
4✔
751
                Settings
4✔
752
                Version  string
4✔
753
                Database struct {
4✔
754
                        Type   string
4✔
755
                        GID    string
4✔
756
                        Status string
4✔
757
                }
4✔
758
                Backup struct {
4✔
759
                        URL      string
4✔
760
                        Filename string
4✔
761
                }
4✔
762
                System struct {
4✔
763
                        Uptime string
4✔
764
                }
4✔
765
        }{
4✔
766
                Settings: s.Settings,
4✔
767
                Version:  s.Version,
4✔
768
                Database: struct {
4✔
769
                        Type   string
4✔
770
                        GID    string
4✔
771
                        Status string
4✔
772
                }{
4✔
773
                        Type:   dbInfo.DatabaseType,
4✔
774
                        GID:    dbInfo.GID,
4✔
775
                        Status: dbInfo.DatabaseStatus,
4✔
776
                },
4✔
777
                Backup: struct {
4✔
778
                        URL      string
4✔
779
                        Filename string
4✔
780
                }{
4✔
781
                        URL:      backupURL,
4✔
782
                        Filename: backupFilename,
4✔
783
                },
4✔
784
                System: struct {
4✔
785
                        Uptime string
4✔
786
                }{
4✔
787
                        Uptime: formatDuration(uptime),
4✔
788
                },
4✔
789
        }
4✔
790

4✔
791
        if err := tmpl.ExecuteTemplate(w, "settings.html", data); err != nil {
5✔
792
                log.Printf("[WARN] can't execute template: %v", err)
1✔
793
                http.Error(w, "Error executing template", http.StatusInternalServerError)
1✔
794
                return
1✔
795
        }
1✔
796
}
797

798
// formatDuration formats a duration in a human-readable way
799
func formatDuration(d time.Duration) string {
12✔
800
        days := int(d.Hours() / 24)
12✔
801
        hours := int(d.Hours()) % 24
12✔
802
        minutes := int(d.Minutes()) % 60
12✔
803

12✔
804
        if days > 0 {
15✔
805
                return fmt.Sprintf("%dd %dh %dm", days, hours, minutes)
3✔
806
        }
3✔
807

808
        if hours > 0 {
11✔
809
                return fmt.Sprintf("%dh %dm", hours, minutes)
2✔
810
        }
2✔
811

812
        return fmt.Sprintf("%dm", minutes)
7✔
813
}
814

815
func (s *Server) downloadDetectedSpamHandler(w http.ResponseWriter, r *http.Request) {
3✔
816
        ctx := r.Context()
3✔
817
        spam, err := s.DetectedSpam.Read(ctx)
3✔
818
        if err != nil {
4✔
819
                w.WriteHeader(http.StatusInternalServerError)
1✔
820
                rest.RenderJSON(w, rest.JSON{"error": "can't get detected spam", "details": err.Error()})
1✔
821
                return
1✔
822
        }
1✔
823

824
        type jsonSpamInfo struct {
2✔
825
                ID        int64                `json:"id"`
2✔
826
                GID       string               `json:"gid"`
2✔
827
                Text      string               `json:"text"`
2✔
828
                UserID    int64                `json:"user_id"`
2✔
829
                UserName  string               `json:"user_name"`
2✔
830
                Timestamp time.Time            `json:"timestamp"`
2✔
831
                Added     bool                 `json:"added"`
2✔
832
                Checks    []spamcheck.Response `json:"checks"`
2✔
833
        }
2✔
834

2✔
835
        // convert entries to jsonl format with lowercase fields
2✔
836
        lines := make([]string, 0, len(spam))
2✔
837
        for _, entry := range spam {
5✔
838
                data, err := json.Marshal(jsonSpamInfo{
3✔
839
                        ID:        entry.ID,
3✔
840
                        GID:       entry.GID,
3✔
841
                        Text:      entry.Text,
3✔
842
                        UserID:    entry.UserID,
3✔
843
                        UserName:  entry.UserName,
3✔
844
                        Timestamp: entry.Timestamp,
3✔
845
                        Added:     entry.Added,
3✔
846
                        Checks:    entry.Checks,
3✔
847
                })
3✔
848
                if err != nil {
3✔
849
                        w.WriteHeader(http.StatusInternalServerError)
×
850
                        rest.RenderJSON(w, rest.JSON{"error": "can't marshal entry", "details": err.Error()})
×
851
                        return
×
852
                }
×
853
                lines = append(lines, string(data))
3✔
854
        }
855

856
        body := strings.Join(lines, "\n")
2✔
857
        w.Header().Set("Content-Type", "application/x-jsonlines")
2✔
858
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", "detected_spam.jsonl"))
2✔
859
        w.Header().Set("Content-Length", strconv.Itoa(len(body)))
2✔
860
        w.WriteHeader(http.StatusOK)
2✔
861
        _, _ = w.Write([]byte(body))
2✔
862
}
863

864
// downloadBackupHandler streams a database backup as an SQL file with gzip compression
865
// Files are always compressed and always have .gz extension to ensure consistency
866
func (s *Server) downloadBackupHandler(w http.ResponseWriter, r *http.Request) {
2✔
867
        if s.StorageEngine == nil {
3✔
868
                w.WriteHeader(http.StatusInternalServerError)
1✔
869
                rest.RenderJSON(w, rest.JSON{"error": "storage engine not available"})
1✔
870
                return
1✔
871
        }
1✔
872

873
        // set filename based on database type and timestamp
874
        dbType := "db"
1✔
875
        sqlEng, ok := s.StorageEngine.(*engine.SQL)
1✔
876
        if ok {
1✔
877
                dbType = string(sqlEng.Type())
×
878
        }
×
879
        timestamp := time.Now().Format("20060102-150405")
1✔
880

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

1✔
884
        // set headers for file download - note we're using application/octet-stream
1✔
885
        // instead of application/sql to prevent browsers from trying to interpret the file
1✔
886
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
887
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
888
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
889
        w.Header().Set("Pragma", "no-cache")
1✔
890
        w.Header().Set("Expires", "0")
1✔
891

1✔
892
        // create a gzip writer that streams to response
1✔
893
        gzipWriter := gzip.NewWriter(w)
1✔
894
        defer func() {
2✔
895
                if err := gzipWriter.Close(); err != nil {
1✔
896
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
897
                }
×
898
        }()
899

900
        // stream backup directly to response through gzip
901
        if err := s.StorageEngine.Backup(r.Context(), gzipWriter); err != nil {
1✔
902
                log.Printf("[ERROR] failed to create backup: %v", err)
×
903
                // we've already started writing the response, so we can't send a proper error response
×
904
                return
×
905
        }
×
906

907
        // flush the gzip writer to ensure all data is written
908
        if err := gzipWriter.Flush(); err != nil {
1✔
909
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
910
        }
×
911
}
912

913
// downloadExportToPostgresHandler streams a PostgreSQL-compatible export from a SQLite database
914
func (s *Server) downloadExportToPostgresHandler(w http.ResponseWriter, r *http.Request) {
3✔
915
        if s.StorageEngine == nil {
4✔
916
                w.WriteHeader(http.StatusInternalServerError)
1✔
917
                rest.RenderJSON(w, rest.JSON{"error": "storage engine not available"})
1✔
918
                return
1✔
919
        }
1✔
920

921
        // check if the database is SQLite
922
        if s.StorageEngine.Type() != engine.Sqlite {
3✔
923
                w.WriteHeader(http.StatusBadRequest)
1✔
924
                rest.RenderJSON(w, rest.JSON{"error": "source database must be SQLite"})
1✔
925
                return
1✔
926
        }
1✔
927

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

1✔
932
        // set headers for file download
1✔
933
        w.Header().Set("Content-Type", "application/octet-stream")
1✔
934
        w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
1✔
935
        w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
1✔
936
        w.Header().Set("Pragma", "no-cache")
1✔
937
        w.Header().Set("Expires", "0")
1✔
938

1✔
939
        // create a gzip writer that streams to response
1✔
940
        gzipWriter := gzip.NewWriter(w)
1✔
941
        defer func() {
2✔
942
                if err := gzipWriter.Close(); err != nil {
1✔
943
                        log.Printf("[ERROR] failed to close gzip writer: %v", err)
×
944
                }
×
945
        }()
946

947
        // stream export directly to response through gzip
948
        if err := s.StorageEngine.BackupSqliteAsPostgres(r.Context(), gzipWriter); err != nil {
1✔
949
                log.Printf("[ERROR] failed to create export: %v", err)
×
950
                // we've already started writing the response, so we can't send a proper error response
×
951
                return
×
952
        }
×
953

954
        // flush the gzip writer to ensure all data is written
955
        if err := gzipWriter.Flush(); err != nil {
1✔
956
                log.Printf("[ERROR] failed to flush gzip writer: %v", err)
×
957
        }
×
958
}
959

960
func (s *Server) renderSamples(w http.ResponseWriter, tmplName string) {
6✔
961
        spam, ham, err := s.SpamFilter.DynamicSamples()
6✔
962
        if err != nil {
7✔
963
                w.WriteHeader(http.StatusInternalServerError)
1✔
964
                rest.RenderJSON(w, rest.JSON{"error": "can't fetch samples", "details": err.Error()})
1✔
965
                return
1✔
966
        }
1✔
967

968
        spam, ham = s.reverseSamples(spam, ham)
5✔
969

5✔
970
        type smpleWithID struct {
5✔
971
                ID     string
5✔
972
                Sample string
5✔
973
        }
5✔
974

5✔
975
        makeID := func(s string) string {
19✔
976
                hash := sha1.New() //nolint
14✔
977
                if _, err := hash.Write([]byte(s)); err != nil {
14✔
978
                        return fmt.Sprintf("%x", s)
×
979
                }
×
980
                return fmt.Sprintf("%x", hash.Sum(nil))
14✔
981
        }
982

983
        tmplData := struct {
5✔
984
                SpamSamples      []smpleWithID
5✔
985
                HamSamples       []smpleWithID
5✔
986
                TotalHamSamples  int
5✔
987
                TotalSpamSamples int
5✔
988
        }{
5✔
989
                TotalHamSamples:  len(ham),
5✔
990
                TotalSpamSamples: len(spam),
5✔
991
        }
5✔
992
        for _, s := range spam {
12✔
993
                tmplData.SpamSamples = append(tmplData.SpamSamples, smpleWithID{ID: makeID(s), Sample: s})
7✔
994
        }
7✔
995
        for _, h := range ham {
12✔
996
                tmplData.HamSamples = append(tmplData.HamSamples, smpleWithID{ID: makeID(h), Sample: h})
7✔
997
        }
7✔
998

999
        if err := tmpl.ExecuteTemplate(w, tmplName, tmplData); err != nil {
6✔
1000
                w.WriteHeader(http.StatusInternalServerError)
1✔
1001
                rest.RenderJSON(w, rest.JSON{"error": "can't execute template", "details": err.Error()})
1✔
1002
                return
1✔
1003
        }
1✔
1004
}
1005

1006
func (s *Server) authMiddleware(mw func(next http.Handler) http.Handler) func(next http.Handler) http.Handler {
10✔
1007
        if s.AuthPasswd == "" {
16✔
1008
                return func(next http.Handler) http.Handler {
105✔
1009
                        return next
99✔
1010
                }
99✔
1011
        }
1012
        return func(next http.Handler) http.Handler {
70✔
1013
                return mw(next)
66✔
1014
        }
66✔
1015
}
1016

1017
// reverseSamples returns reversed lists of spam and ham samples
1018
func (s *Server) reverseSamples(spam, ham []string) (revSpam, revHam []string) {
8✔
1019
        revSpam = make([]string, len(spam))
8✔
1020
        revHam = make([]string, len(ham))
8✔
1021

8✔
1022
        for i, j := 0, len(spam)-1; i < len(spam); i, j = i+1, j-1 {
19✔
1023
                revSpam[i] = spam[j]
11✔
1024
        }
11✔
1025
        for i, j := 0, len(ham)-1; i < len(ham); i, j = i+1, j-1 {
19✔
1026
                revHam[i] = ham[j]
11✔
1027
        }
11✔
1028
        return revSpam, revHam
8✔
1029
}
1030

1031
// staticFS is a filtered filesystem that only exposes specific static files
1032
type staticFS struct {
1033
        fs        fs.FS
1034
        urlToPath map[string]string
1035
}
1036

1037
// staticFileMapping defines a mapping between URL path and filesystem path
1038
type staticFileMapping struct {
1039
        urlPath     string
1040
        filesysPath string
1041
}
1042

1043
func newStaticFS(fsys fs.FS, files ...staticFileMapping) *staticFS {
5✔
1044
        urlToPath := make(map[string]string)
5✔
1045
        for _, f := range files {
20✔
1046
                urlToPath[f.urlPath] = f.filesysPath
15✔
1047
        }
15✔
1048

1049
        return &staticFS{
5✔
1050
                fs:        fsys,
5✔
1051
                urlToPath: urlToPath,
5✔
1052
        }
5✔
1053
}
1054

1055
func (sfs *staticFS) Open(name string) (fs.File, error) {
5✔
1056
        name = path.Clean("/" + name)[1:]
5✔
1057
        if fsPath, ok := sfs.urlToPath[name]; ok {
8✔
1058
                return sfs.fs.Open(fsPath)
3✔
1059
        }
3✔
1060
        return nil, fs.ErrNotExist
2✔
1061
}
1062

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

2✔
1067
        var password strings.Builder
2✔
1068
        charsetSize := big.NewInt(int64(len(charset)))
2✔
1069

2✔
1070
        for i := 0; i < length; i++ {
66✔
1071
                randomNumber, err := rand.Int(rand.Reader, charsetSize)
64✔
1072
                if err != nil {
64✔
1073
                        return "", err
×
1074
                }
×
1075

1076
                password.WriteByte(charset[randomNumber.Int64()])
64✔
1077
        }
1078

1079
        return password.String(), nil
2✔
1080
}
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