forked from pauldotknopf/automounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (78 loc) · 1.81 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
package main
import (
"context"
"log"
"os"
"github.com/coreos/go-systemd/daemon"
"github.com/coreos/go-systemd/journal"
"github.com/sirupsen/logrus"
"github.com/wercker/journalhook"
"github.com/pauldotknopf/automounter/leaser"
"github.com/pauldotknopf/automounter/utils/appcontext"
"github.com/pauldotknopf/automounter/providers/ios"
"github.com/pauldotknopf/automounter/providers/muxer"
"github.com/pauldotknopf/automounter/providers/smb"
"github.com/pauldotknopf/automounter/providers/udisks"
"github.com/pauldotknopf/automounter/web"
"golang.org/x/sync/errgroup"
)
var eg errgroup.Group
func main() {
if journal.Enabled() {
logrus.AddHook(&journalhook.JournalHook{})
}
ctx, cancel := context.WithCancel(appcontext.Context())
udisksProvider, err := udisks.Create()
if err != nil {
log.Println(err)
os.Exit(1)
}
iosProvider, err := ios.Create()
if err != nil {
log.Println(err)
os.Exit(1)
}
smbProvider, err := smb.Create()
if err != nil {
log.Println(err)
os.Exit(1)
}
mediaProvider := muxer.Create(udisksProvider, iosProvider, smbProvider)
leaser := leaser.Create(mediaProvider)
// Start the processing of leases.
eg.Go(func() error {
leaseErr := leaser.Process(ctx)
if leaseErr != nil {
cancel()
return leaseErr
}
return nil
})
// Start the monitoring of media.
eg.Go(func() error {
startErr := mediaProvider.Start(ctx)
if startErr != nil {
cancel()
return startErr
}
return nil
})
// Start the web API.
eg.Go(func() error {
server := web.Create(leaser, smbProvider)
serverErr := server.Listen(ctx, 3000, func() {
// We have started listening for requests.
daemon.SdNotify(false, "READY=1")
})
if serverErr != nil {
cancel()
return serverErr
}
return nil
})
err = eg.Wait()
if err != nil {
log.Println(err)
os.Exit(1)
}
}