-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (53 loc) · 1.66 KB
/
server.js
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
const { DefaultAzureCredential } = require("@azure/identity");
const { SecretClient } = require("@azure/keyvault-secrets");
const app = require("express")();
const express = require("express");
const path = require("path");
// const cors = require("cors");
// var corsOptions = {
// origin: "http://localhost:8080",
// optionsSuccessStatus: 200,
// };
// app.use(cors(corsOptions));
const port = 8080;
const keyVaultName = "fruit-detector-kv";
const KVUri = "https://" + keyVaultName + ".vault.azure.net";
const credential = new DefaultAzureCredential();
const client = new SecretClient(KVUri, credential);
const bpath = path.normalize(path.join(__dirname, "./client/build"));
app.use(express.static(bpath));
app.get("/api/cnakey", async (req, res) => {
const cnakey = await getSecret("CALORIENINJA-API-KEY");
res.type("text");
res.send(cnakey);
});
app.get("/api/cna", async (req, res) => {
const cna = await getSecret("CALORIENINJA-API");
res.type("text");
res.send(cna);
});
app.get("/api/cvpk", async (req, res) => {
const cvpk = await getSecret("CUSTOM-VISION-PREDICTION-KEY");
res.type("text");
res.send(cvpk);
});
app.get("/api/cva", async (req, res) => {
const cva = await getSecret("CUSTOM-VISION-API");
res.type("text");
res.send(cva);
});
const rootrouter = express.Router();
rootrouter.get("(/*)?", async (req, res) => {
res.sendFile(path.join(bpath, "index.html"));
});
app.use(rootrouter);
app.get("/test", (req, res) => {
res.send("test");
});
async function getSecret(secretName) {
const retrievedSecret = await client.getSecret(secretName);
return retrievedSecret;
}
app.listen(port, () => {
console.log("server running at", port);
});