-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_service.go
92 lines (76 loc) · 1.55 KB
/
user_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
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
package main
import (
"context"
"log"
"os"
"path/filepath"
"sync"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
)
// userService provides user info for the currently "logged in" user.
// We don't really have "users" in this demo though and neither do we have login.
// So it's basically just a thing that provides a set of attributes from a live file
type userService struct {
mu sync.Mutex
user user
}
const (
userInfoPath = "./data/userinfo.yaml"
)
func newUserService(ctx context.Context) (*userService, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
u := &userService{}
go func() {
defer watcher.Close()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("user reload returned:")
if event.Has(fsnotify.Write) && filepath.Ext(event.Name) == ".yaml" {
log.Println("user reload returned:", u.reload())
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("watch error:", err)
}
}
}()
err = watcher.Add(userInfoPath)
if err != nil {
return nil, err
}
log.Println("watching for events on", userInfoPath)
return u, u.reload()
}
func (u *userService) reload() error {
f, err := os.Open(userInfoPath)
if err != nil {
return err
}
defer f.Close()
y := yaml.NewDecoder(f)
y.SetStrict(true)
u2 := user{}
err = y.Decode(&u2)
if err != nil {
return err
}
u.mu.Lock()
u.user = u2
u.mu.Unlock()
return nil
}
func (u *userService) get() user {
u.mu.Lock()
defer u.mu.Unlock()
return u.user
}