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