-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
130 lines (115 loc) · 2.66 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
package main
import (
"context"
"crypto/tls"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
action "github.com/rmrobinson/google-smart-home-action-go"
"go.uber.org/zap"
"golang.org/x/crypto/acme/autocert"
"google.golang.org/api/homegraph/v1"
"google.golang.org/api/option"
)
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")
agentUserID = flag.String("agent-user-id", "", "The HomeGraph account user ID to synchronize state with")
credsFile = flag.String("creds-file", "", "The Google Service Account key file path")
)
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 the 'mock' service
es := &echoService{
logger: logger,
lights: map[string]lightbulb{
"123": {
"123",
"test light 1",
false,
40,
struct {
hue float64
saturation float64
value float64
}{
100,
100,
10,
},
},
"456": {
"456",
"test light 2",
false,
40,
struct {
hue float64
saturation float64
value float64
}{
100,
100,
10,
},
},
},
receiver: receiver{
"789",
"test receiver",
false,
20,
false,
"input_1",
},
agentID: *agentUserID,
}
// 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),
)
}
svc := action.NewService(logger, auth, es, hgService)
es.service = svc
// Register callback from Google
http.HandleFunc(action.GoogleFulfillmentPath, svc.GoogleFulfillmentHandler)
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
go func() {
for {
<-c
logger.Debug("toggling light 1 via signal")
es.toggleLight1()
}
}()
// Setup LetsEncrypt
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*letsEncryptHost), //Your domain here
Cache: autocert.DirCache("certs"), //Folder for storing certificates
}
server := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
go http.ListenAndServe(":http", certManager.HTTPHandler(nil))
logger.Info("listening")
log.Fatal(server.ListenAndServeTLS("", ""))
}