-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (132 loc) · 3.98 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
action "github.com/rmrobinson/google-smart-home-action-go"
"github.com/rmrobinson/nerves/services/domotics/integrations/googlehome"
"github.com/rmrobinson/nerves/services/domotics/integrations/googlehome/relay"
"go.uber.org/zap"
"golang.org/x/crypto/acme/autocert"
"google.golang.org/api/homegraph/v1"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
func main() {
var (
auth0Domain = flag.String("auth0-domain", "", "The domain that Auth0 users will be coming from")
letsEncryptHost = flag.String("letsencrypt-host", "", "The host name that LetsEncrypt will generate the cert for")
mockAgentUserID = flag.String("mock-agent-user-id", "", "The HomeGraph account user ID to use for the mock provider")
credsFile = flag.String("creds-file", "", "The Google Service Account key file path")
googleHomeServicePort = flag.Int("google-home-service-port", 20013, "The port the GoogleHome service is listening on")
)
flag.Parse()
logger, _ := zap.NewDevelopment()
defer logger.Sync()
// Setup our authentication validator
auth := &auth0Authenticator{
logger: logger,
domain: *auth0Domain,
client: &http.Client{},
tokens: map[string]string{},
}
// Setup Google Assistant info
ctx := context.Background()
hgService, err := homegraph.NewService(ctx, option.WithCredentialsFile(*credsFile))
if err != nil {
logger.Fatal("err initializing homegraph",
zap.Error(err),
)
}
relayProvider := relay.NewProviderService(logger)
googleActionService := action.NewService(logger, auth, relayProvider, hgService)
relayAPI := relay.NewAPI(logger, relayProvider, googleActionService)
// Register callback from Google
http.HandleFunc(action.GoogleFulfillmentPath, googleActionService.GoogleFulfillmentHandler)
if len(*mockAgentUserID) > 0 {
mockLights := map[string]MockLightbulb{
"123": {
ID: "123",
Name: "test light 1",
IsOn: false,
Brightness: 40,
Color: struct {
Hue float64
Saturation float64
Value float64
}{
100,
100,
10,
},
},
"456": {
ID: "456",
Name: "test light 2",
IsOn: false,
Brightness: 40,
Color: struct {
Hue float64
Saturation float64
Value float64
}{
100,
100,
10,
},
},
}
mockReceiver := MockReceiver{
ID: "789",
Name: "test receiver",
IsOn: false,
Volume: 20,
Muted: false,
CurrInput: "input_1",
}
mp := NewMockProvider(logger, googleActionService, mockLights, mockReceiver, *mockAgentUserID)
relayProvider.RegisterProvider(*mockAgentUserID, mp)
}
// Setup LetsEncrypt
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*letsEncryptHost), //Your domain here
Cache: autocert.DirCache("certs"), //Folder for storing certificates
}
googleCallbackServer := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
logger.Info("listening for google callbacks",
zap.String("local_addr", "0.0.0.0:443"),
)
// Start the HTTP listener used by LetsEncrypt to validate the domain
go http.ListenAndServe(":http", certManager.HTTPHandler(nil))
// Start the HTTPS listener used by Google to pass callbacks to the system
go func() {
err := googleCallbackServer.ListenAndServeTLS("", "")
if err != nil {
logger.Fatal("error listening for google callbacks",
zap.Error(err),
)
}
}()
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", *googleHomeServicePort))
if err != nil {
logger.Fatal("error initializing grpc listener",
zap.Error(err),
)
}
defer lis.Close()
logger.Info("listening for google home service",
zap.String("local_addr", lis.Addr().String()),
)
grpcServer := grpc.NewServer()
googlehome.RegisterGoogleHomeServiceServer(grpcServer, relayAPI)
grpcServer.Serve(lis)
}