forked from google-home/smart-home-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
47 lines (42 loc) · 1.03 KB
/
app.ts
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
import { Handler } from "express";
import express from "express";
// Import the appropriate service
import { json } from "body-parser";
import { createAuthRouter } from "./auth";
import { createSmarthomeRouter } from "./smarthome";
export async function createApp(
config: {
agentUserId: string;
clientId: string;
clientSecret: string;
accessToken: string;
refreshToken: string;
code: string;
googleProjectId: string;
googleApiKey: string;
mqttBaseTopic: string;
},
mqtt: { send: Function },
logger: any,
devices: any,
state: any
) {
const authRouter = createAuthRouter(config, logger);
const smarthomeRouter = await createSmarthomeRouter(
config,
mqtt,
logger,
devices,
state
);
const auth: Handler = (req, res, next) => {
const { authorization } = req.headers;
if (!authorization) res.status(401).send();
next();
};
const app = express()
.use(json())
.use("/auth", authRouter)
.post("/fulfillment", auth, smarthomeRouter);
return app;
}