-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
52 lines (42 loc) · 984 Bytes
/
service.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
package service
import (
"context"
"time"
"github.com/boreq/eggplant/adapters/auth"
"github.com/boreq/eggplant/internal/config"
httpPort "github.com/boreq/eggplant/ports/http"
"github.com/pkg/errors"
)
const updateLastSeenEvery = 5 * time.Minute
type Service struct {
httpServer *httpPort.Server
lastSeenUpdater *auth.LastSeenUpdater
conf *config.Config
}
func NewService(
httpServer *httpPort.Server,
lastSeenUpdater *auth.LastSeenUpdater,
conf *config.Config,
) *Service {
return &Service{
httpServer: httpServer,
lastSeenUpdater: lastSeenUpdater,
conf: conf,
}
}
func (s *Service) Run(ctx context.Context) error {
ch := make(chan error)
go func() {
ch <- s.httpServer.Serve(ctx, s.conf.ServeAddress)
}()
go func() {
s.lastSeenUpdater.Run(ctx, updateLastSeenEvery)
ch <- nil
}()
for i := 0; i < 2; i++ {
if err := <-ch; err != nil {
return errors.Wrap(err, "error during shutdown")
}
}
return nil
}