-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
132 lines (114 loc) · 2.78 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"flag"
"fmt"
"net/http"
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/golang/glog"
"github.com/livefir/fir"
"github.com/livefir/fir/pubsub"
)
type Counter struct {
count int32
updated time.Time
sync.RWMutex
}
func (c *Counter) Inc(ctx fir.RouteContext) error {
c.Lock()
defer c.Unlock()
c.count += 1
c.updated = time.Now()
return ctx.Data(map[string]any{"count": c.count})
}
func (c *Counter) Dec(ctx fir.RouteContext) error {
c.Lock()
defer c.Unlock()
c.count -= 1
c.updated = time.Now()
return ctx.Data(map[string]any{"count": c.count})
}
func (c *Counter) Updated() float64 {
c.RLock()
defer c.RUnlock()
return time.Since(c.updated).Seconds()
}
func (c *Counter) Count() int32 {
c.RLock()
defer c.RUnlock()
return c.count
}
func NewCounterIndex(pubsub pubsub.Adapter) *index {
c := &index{
model: &Counter{},
pubsub: pubsub,
eventSender: make(chan fir.Event),
id: "counter",
}
ticker := time.NewTicker(time.Second)
pattern := fmt.Sprintf("*:%s", c.id)
go func() {
for ; true; <-ticker.C {
if !c.pubsub.HasSubscribers(context.Background(), pattern) {
// if userID:viewID(*:viewID) channel pattern has no subscribers, skip costly operation
glog.Errorf("channel pattern %s has no subscribers", pattern)
continue
}
c.eventSender <- fir.NewEvent("updated", countUpdate{CountUpdated: c.model.Updated()})
}
}()
return c
}
type countUpdate struct {
CountUpdated float64
}
type index struct {
model *Counter
pubsub pubsub.Adapter
eventSender chan fir.Event
id string
}
func (i *index) Options() fir.RouteOptions {
return fir.RouteOptions{
fir.ID(i.id),
fir.Content("count.html"),
fir.Layout("layout.html"),
fir.OnLoad(i.load),
fir.OnEvent("inc", i.inc),
fir.OnEvent("dec", i.dec),
fir.OnEvent("updated", i.updated),
fir.EventSender(i.eventSender),
}
}
func (i *index) load(ctx fir.RouteContext) error {
return ctx.Data(map[string]any{"count": i.model.Count()})
}
func (i *index) inc(ctx fir.RouteContext) error {
return i.model.Inc(ctx)
}
func (i *index) dec(ctx fir.RouteContext) error {
return i.model.Dec(ctx)
}
func (i *index) updated(ctx fir.RouteContext) error {
req := &countUpdate{}
err := ctx.Bind(req)
if err != nil {
return err
}
return ctx.Data(req)
}
func main() {
port := flag.String("port", "9867", "port to listen on")
pubsubAdapter := pubsub.NewRedis(
redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
}),
)
controller := fir.NewController("counter_app", fir.DevelopmentMode(true), fir.WithPubsubAdapter(pubsubAdapter))
http.Handle("/", controller.Route(NewCounterIndex(pubsubAdapter)))
http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
}