Cleanup for readability
This commit is contained in:
parent
e726a4fe01
commit
201f9df3d3
@ -1,3 +1,4 @@
|
||||
// Package glist handles the list processing
|
||||
package glist
|
||||
|
||||
import (
|
||||
@ -34,16 +35,17 @@ func NewGList(chatID int, items ...string) *GList {
|
||||
return &g
|
||||
}
|
||||
|
||||
var PersistReqC chan<- *GList
|
||||
var persistReqC chan<- *GList
|
||||
|
||||
func startPersistenceGoR() {
|
||||
reqs := make(chan *GList, 50)
|
||||
PersistReqC = reqs
|
||||
persistReqC = reqs
|
||||
go func() {
|
||||
lists := map[*GList]struct{}{}
|
||||
for g := range reqs {
|
||||
lists[g] = struct{}{}
|
||||
time.Sleep(5 * time.Second)
|
||||
lists := map[*GList]struct{}{
|
||||
g: struct{}{},
|
||||
}
|
||||
time.Sleep(5 * time.Second) // Collect all persist requests for 5 seconds
|
||||
for len(reqs) > 0 {
|
||||
g := <-reqs
|
||||
lists[g] = struct{}{}
|
||||
@ -51,7 +53,6 @@ func startPersistenceGoR() {
|
||||
for g := range lists {
|
||||
g.persist()
|
||||
}
|
||||
lists = make(map[*GList]struct{}, len(lists))
|
||||
}
|
||||
}()
|
||||
}
|
||||
@ -83,14 +84,14 @@ outer:
|
||||
}
|
||||
g.Items = append(g.Items, Entry{text, false})
|
||||
}
|
||||
PersistReqC <- g
|
||||
persistReqC <- g
|
||||
}
|
||||
|
||||
func (g *GList) Toggle(text string) {
|
||||
for i, item := range g.Items {
|
||||
if item.Text == text {
|
||||
g.Items[i].Checked = !g.Items[i].Checked
|
||||
PersistReqC <- g
|
||||
persistReqC <- g
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -105,7 +106,7 @@ func (g *GList) ClearChecked() {
|
||||
}
|
||||
}
|
||||
g.Items = remaining
|
||||
PersistReqC <- g
|
||||
persistReqC <- g
|
||||
}
|
||||
|
||||
type button struct {
|
||||
|
@ -21,7 +21,7 @@ func TestGList(t *testing.T) {
|
||||
func TestSplit(t *testing.T) {
|
||||
g := &GList{}
|
||||
//This resets the channel, so test does not try to persist
|
||||
PersistReqC = make(chan *GList, 50)
|
||||
persistReqC = make(chan *GList, 50)
|
||||
g.Add("foo")
|
||||
g.Add("bar\nfoo\nblah")
|
||||
g.Add("foo")
|
||||
|
35
main.go
35
main.go
@ -31,8 +31,8 @@ func main() {
|
||||
|
||||
port := func() int {
|
||||
portStr := os.Getenv("CHKBOT_PORT")
|
||||
port, err := strconv.Atoi(portStr)
|
||||
if err == nil {
|
||||
|
||||
if port, err := strconv.Atoi(portStr); err == nil {
|
||||
return port
|
||||
}
|
||||
return 28923
|
||||
@ -41,10 +41,10 @@ func main() {
|
||||
dataPath := func() string {
|
||||
dataPath := os.Getenv("CHKBOT_DATA_PATH")
|
||||
if dataPath == "" {
|
||||
dataPath = "."
|
||||
return "."
|
||||
}
|
||||
err := os.MkdirAll(dataPath, 0755)
|
||||
if err != nil {
|
||||
|
||||
if err := os.MkdirAll(dataPath, 0755); err != nil {
|
||||
log.Panicf("Failed to create datapath, path: %s, err: %s", dataPath, err)
|
||||
}
|
||||
return dataPath
|
||||
@ -52,15 +52,16 @@ func main() {
|
||||
|
||||
glist.DataPath = dataPath
|
||||
|
||||
commit := "unknown"
|
||||
|
||||
if bi, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, s := range bi.Settings {
|
||||
if s.Key == "vcs.revision" {
|
||||
commit = s.Value[:8]
|
||||
commit := func() string {
|
||||
if bi, ok := debug.ReadBuildInfo(); ok {
|
||||
for _, s := range bi.Settings {
|
||||
if s.Key == "vcs.revision" {
|
||||
return s.Value[:8]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "unknown"
|
||||
}()
|
||||
|
||||
log.Printf("Grocery List bot (%s) starting with datapath:%s, port:%d\n", commit, dataPath, port)
|
||||
|
||||
@ -70,11 +71,11 @@ func main() {
|
||||
}
|
||||
botPath := fmt.Sprintf("/bot%s", apiToken)
|
||||
http.HandleFunc(botPath, func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := w.Write([]byte("ok"))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
if _, err := w.Write([]byte("ok")); err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}()
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
Loading…
Reference in New Issue
Block a user