-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_rule_list.go
103 lines (85 loc) · 2.16 KB
/
log_rule_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Author: Markus Stenberg <[email protected]>
*
* Copyright (c) 2024 Markus Stenberg
*
*/
package main
import (
"net/http"
"net/url"
"strconv"
"github.com/fingon/lixie/cm"
"github.com/fingon/lixie/data"
)
const indexKey = "i"
// This struct represents external configuration - what we can get as query/form parameters
type LogRuleListConfig struct {
// Global configuration (cookie)
Global GlobalConfig
// Paging support
Index int
}
func (self *LogRuleListConfig) Init(r *http.Request, w http.ResponseWriter) error {
_, err := cm.IntFromForm(r, indexKey, &self.Index)
if err != nil {
return err
}
return cm.Run(r, w, &self.Global)
}
func (self *LogRuleListConfig) ToLinkString() string {
base := topLevelLogRule.Path + "/"
v := url.Values{}
// Handle run-time state
if self.Index != 0 {
v.Set(indexKey, strconv.Itoa(self.Index))
}
if len(v) == 0 {
return base
}
return base + "?" + v.Encode()
}
type LogRuleListModel struct {
Config LogRuleListConfig
DB *data.Database
// This is the list actually rendered to the client in this
// request; subset of DB rules
LogRules []*data.LogRule
HasMore bool
Limit int
}
func (self *LogRuleListModel) Filter() {
// First do fts (if necessary)
last := self.Config.Index + self.Limit
rules := filterFTS(self.LogRules, self.Config.Global.Search, last+1)
self.HasMore = len(rules) >= last
if last >= len(rules) {
last = len(rules) - 1
}
result := make([]*data.LogRule, 0, self.Limit)
if last > self.Config.Index {
result = append(result, rules[self.Config.Index:last]...)
}
self.LogRules = result
}
func (self *LogRuleListModel) NextLinkString() string {
next := self.Config
next.Index += self.Limit
return next.ToLinkString()
}
func logRuleListHandler(st State) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
config := LogRuleListConfig{}
err := config.Init(r, w)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
m := LogRuleListModel{Config: config, DB: st.DB, LogRules: st.DB.LogRules.Reversed, Limit: 10}
m.Filter()
err = LogRuleList(st, m).Render(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), 500)
}
})
}