Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 6966dd2

Browse files
committed
work out of the box with mqtt broker
1 parent d5706f0 commit 6966dd2

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

local.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ DEVS_LOCALHOST="1"
55
DEVS_STORAGE_CONNECTION_STRING="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;"
66
DEVS_PASSWORDS_SECRET="DEVS_LOCAL_USER_PASSWORD"
77
DEVS_LOCAL_USER_PASSWORD="devstoreaccount1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
8-
DEVS_CONNECTION_STRING="AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;ApiRoot=$WEBSITE_PROTOCOL://$WEBSITE_HOSTNAME"
8+
DEVS_CONNECTION_STRING="AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;ApiRoot=$WEBSITE_PROTOCOL://$WEBSITE_HOSTNAME"
9+
DEVS_MQTT_CONNETION_STRING="mqtt://public.mqtthq.com:1883"

src/azure/storagequeue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { serverTelemetry } from "./appinsights"
33
import { registerMessageSink } from "../messages"
44
import { readStorageConnectionString } from "../storage"
55

6-
const MESSAGE_TIME_TO_LIVE = 360
6+
const MESSAGE_TIME_TO_LIVE = 60
77

88
export async function setup() {
99
const connStr = await readStorageConnectionString()

src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as mq from "./mq"
1010
import { wsskInit } from "./wssk"
1111
import { fwdSockInit } from "./fwdsock"
1212

13-
import { createSecretClient } from "./secrets"
13+
import { getSecret } from "./secrets"
1414
import { generateOpenApiSpec } from "./swagger/openapi"
1515
import { setup as appInsightsSetup, serverTelemetry } from "./azure/appinsights"
1616
import { setup as eventHubSetup } from "./azure/eventhub"
@@ -21,13 +21,13 @@ import { setup as mqttSetup } from "./mqtt"
2121

2222
async function initAuth(server: FastifyInstance) {
2323
console.log(`starting gateway...`)
24-
const secrets = createSecretClient()
25-
const passwordSecretName =
26-
process.env["DEVS_PASSWORDS_SECRET"] || "passwords"
27-
const passwordsSecret = await secrets.getSecret(passwordSecretName)
28-
if (!passwordsSecret.value) throw new Error("passwords is empty")
24+
const passwordsSecret = await getSecret(
25+
"passwords",
26+
"DEVS_PASSWORDS_SECRET"
27+
)
28+
if (!passwordsSecret) throw new Error("passwords is empty")
2929

30-
const passwords = passwordsSecret.value
30+
const passwords = passwordsSecret
3131
.split(/,/)
3232
.map(s => s.trim())
3333
.filter(s => /^\w+:.+/.test(s))

src/mqtt.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { connect } from "mqtt" // import connect from mqtt
22
import { serverTelemetry } from "./azure/appinsights"
33
import { registerMessageSink } from "./messages"
4-
import { createSecretClient } from "./secrets"
5-
import { pubToDevice } from "./devutil"
4+
import { getSecret } from "./secrets"
65
import { defaultPartition, getDevice } from "./storage"
7-
import { sendBinary, sendJSON } from "./apidevices"
6+
import { sendJSON } from "./apidevices"
87
import { DeviceId } from "./schema"
98

109
export async function setup() {
11-
const secrets = createSecretClient()
12-
const connectionStringSecretName =
13-
process.env.DEVS_MQTT_CONNECTION_STRING_SECRET || "mqttConnectionString"
14-
const connStrSecret = await secrets.getSecret(connectionStringSecretName)
15-
const connStr = connStrSecret.value
10+
const connStr = await getSecret(
11+
"mqttConnectionString",
12+
"DEVS_MQTT_CONNECTION_STRING_SECRET",
13+
"DEVS_MQTT_CONNETION_STRING"
14+
)
1615
if (!connStr) {
1716
console.log("no MQTT connection string secret, skipping registration")
1817
return
@@ -51,6 +50,8 @@ export async function setup() {
5150
if (err) {
5251
console.error(err)
5352
telemetry?.trackException({ exception: err })
53+
} else {
54+
console.log(`mqtt: subscribe to 'devs/to/+/#'`)
5455
}
5556
})
5657
})
@@ -65,6 +66,7 @@ export async function setup() {
6566
}
6667
const dev = await getDevice(did)
6768
if (!dev) return // unknown device
69+
6870
await sendJSON(did, msgTopic, JSON.parse(message.toString("utf-8")))
6971
})
7072
}

src/secrets.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ export function createSecretClient(): Secrets {
1717
},
1818
}
1919
}
20+
21+
export async function getSecret(
22+
defaultName: string,
23+
secretNameEnvironmentName: string,
24+
environmentName?: string
25+
) {
26+
const secrets = createSecretClient()
27+
const connectionStringSecretName =
28+
process.env[secretNameEnvironmentName] || defaultName
29+
const connStrSecret = await secrets.getSecret(connectionStringSecretName)
30+
const connStr =
31+
connStrSecret.value ||
32+
(environmentName ? process.env[environmentName] : undefined)
33+
return connStr
34+
}

src/storage.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { gunzip, gzip } from "zlib"
1616
import { pubToDevice } from "./devutil"
1717
import { DeviceId, DeviceInfo, DeviceStats, zeroDeviceStats } from "./schema"
1818
import { delay, throwStatus } from "./util"
19-
import { createSecretClient } from "./secrets"
19+
import { getSecret } from "./secrets"
2020
import { DebugInfo } from "./interop"
2121
import { createHash } from "crypto"
2222

@@ -37,13 +37,11 @@ export interface ScriptBody {
3737
}
3838

3939
export async function readStorageConnectionString() {
40-
const secrets = createSecretClient()
41-
const connectionStringSecretName =
42-
process.env["DEVS_STORAGE_CONNECTION_STRING_SECRET"] ||
43-
"storageAccountConnectionString"
44-
const connStrSecret = await secrets.getSecret(connectionStringSecretName)
45-
const connStr =
46-
connStrSecret.value || process.env.DEVS_STORAGE_CONNECTION_STRING
40+
const connStr = getSecret(
41+
"storageAccountConnectionString",
42+
"DEVS_STORAGE_CONNECTION_STRING_SECRET",
43+
"DEVS_STORAGE_CONNECTION_STRING"
44+
)
4745
return connStr
4846
}
4947

0 commit comments

Comments
 (0)