/
server.go
132 lines (115 loc) · 3.2 KB
/
server.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 server
import (
"context"
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/dsnet/try"
"golang.org/x/exp/slog"
"gopkg.in/webhelp.v1/whfatal"
"gopkg.in/webhelp.v1/whmux"
"storj.io/common/time2"
"github.com/jtolio/workpresence/utils"
)
type Config struct {
ScreenshotInterval time.Duration `default:"45s" "the interval between screenshots"`
ScreenshotIntervalJitter bool `default:"false" "if true, will jitter screenshot interval"`
}
type ScreenshotSource interface {
Screenshot(context.Context) (*utils.SerializedImage, error)
}
type ScreenshotDest interface {
Store(context.Context, time.Time, *utils.SerializedImage) error
}
type Server struct {
cfg Config
source ScreenshotSource
dest ScreenshotDest
paused atomic.Bool
latest atomic.Value
cancel func()
http.Handler
}
func New(cfg Config, source ScreenshotSource, dest ScreenshotDest) *Server {
s := &Server{
cfg: cfg,
source: source,
dest: dest,
}
s.Handler = whmux.Dir{
"": whmux.Exact(http.HandlerFunc(s.pageLanding)),
"pause": whmux.ExactPath(http.HandlerFunc(s.pagePause)),
"resume": whmux.ExactPath(http.HandlerFunc(s.pageResume)),
"quit": whmux.ExactPath(http.HandlerFunc(s.pageQuit)),
"latest": whmux.Exact(http.HandlerFunc(s.pageLatest)),
"favicon.ico": whmux.Exact(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
}
return s
}
func (s *Server) interval() time.Duration {
rv := s.cfg.ScreenshotInterval
if s.cfg.ScreenshotIntervalJitter {
rv = utils.NormJitter(rv) // should this be an exponential jitter?
}
if rv < time.Second {
rv = time.Second
}
return rv
}
func (s *Server) takeScreenshot(ctx context.Context) {
if s.paused.Load() {
return
}
ts := time.Now()
img, err := s.source.Screenshot(ctx)
if err != nil {
slog.Error("failed to capture screenshot", fmt.Errorf("%+w", err))
return
}
s.latest.Store(img)
err = s.dest.Store(ctx, ts, img)
if err != nil {
slog.Error("failed to store screenshot", err)
return
}
}
func (s *Server) Run(ctx context.Context) error {
cancelCtx, cancel := context.WithCancel(ctx)
s.cancel = cancel
s.takeScreenshot(cancelCtx)
for time2.Sleep(cancelCtx, s.interval()) {
s.takeScreenshot(cancelCtx)
}
return ctx.Err()
}
func (s *Server) pagePause(w http.ResponseWriter, r *http.Request) {
s.paused.Store(true)
whfatal.Redirect("/")
}
func (s *Server) pageQuit(w http.ResponseWriter, r *http.Request) {
cancel := s.cancel
if cancel != nil {
cancel()
}
}
func (s *Server) pageResume(w http.ResponseWriter, r *http.Request) {
s.paused.Store(false)
s.takeScreenshot(r.Context())
whfatal.Redirect("/")
}
func (s *Server) pageLanding(w http.ResponseWriter, r *http.Request) {
try.E1(w.Write(indexHTMLHeader))
if s.paused.Load() {
try.E1(w.Write([]byte(`<p style="color:red;">Paused</p><p><a href="/resume">Resume</a></p>`)))
} else {
try.E1(w.Write([]byte(`<p>Running</p><p><a href="/pause">Pause</a></p>`)))
}
try.E1(w.Write(indexHTMLFooter))
}
func (s *Server) pageLatest(w http.ResponseWriter, r *http.Request) {
if latest, ok := s.latest.Load().(*utils.SerializedImage); ok && latest != nil {
w.Header().Set("Content-Type", latest.MIMEType)
try.E1(w.Write(latest.Data))
}
}