|
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import bodyParser from "body-parser"; |
| 4 | +import * as common from "oci-common"; |
| 5 | +import * as aispeech from "oci-aispeech"; |
| 6 | +import https from 'https'; |
| 7 | +import fs from 'fs'; |
| 8 | + |
| 9 | +const app = express(); |
| 10 | +const port = 8448; |
| 11 | + |
| 12 | +// SSL Certificate options with specified paths |
| 13 | +const options = { |
| 14 | + key: fs.readFileSync('C:\\aiholo-app\\localhost-key.pem'), // Path to your private key |
| 15 | + cert: fs.readFileSync('C:\\aiholo-app\\localhost.pem') // Path to your certificate |
| 16 | +}; |
| 17 | + |
| 18 | +// Configure CORS to allow requests from your specific client origin |
| 19 | +// app.use(cors({ |
| 20 | +// origin: 'http://130.61.51.75:4884', // Adjust this to match the origin you are accessing the server from |
| 21 | +// credentials: true // Allows cookies and credentials to be sent along with the requests |
| 22 | +// })); |
| 23 | + |
| 24 | +app.use(cors({ |
| 25 | + origin: ['https://130.61.51.75:4884'], |
| 26 | + credentials: true, |
| 27 | + methods: ['GET', 'POST'], |
| 28 | + allowedHeaders: ['Content-Type', 'Authorization'] |
| 29 | +})); |
| 30 | + |
| 31 | + |
| 32 | +// The OCID of the compartment for authentication and authorization |
| 33 | +const compartmentId = "ocid1.compartment.oc1..aaaaaaaafnah3ogykjsg34qruhixhb2drls6zhsejzm7mubi2i5qj66slcoq"; |
| 34 | + |
| 35 | +// Set the region for OCI services |
| 36 | +const region = "us-phoenix-1"; |
| 37 | +const provider = new common.SessionAuthDetailProvider("C:/Users/opc/.oci/config", "MYSPEECHAIPROFILE"); |
| 38 | + |
| 39 | +/** |
| 40 | + * Generates a real-time session token using Oracle Cloud Infrastructure (OCI) AI Speech Service. |
| 41 | + * Configures the OCI client with a specific region and compartment ID, then requests a real-time session token. |
| 42 | + * |
| 43 | + * @returns {Promise<string>} The real-time session token generated by the AI Speech Service. |
| 44 | + * @throws {Error} If the request to generate the session token fails. |
| 45 | + */ |
| 46 | +async function getRealtimeToken() { |
| 47 | + provider.setRegion(region); |
| 48 | + const speechClient = new aispeech.AIServiceSpeechClient({ authenticationDetailsProvider: provider }); |
| 49 | + |
| 50 | + const createRealtimeSessionTokenDetails = { |
| 51 | + compartmentId: compartmentId, |
| 52 | + }; |
| 53 | + |
| 54 | + const createRealtimeSessionTokenRequest: aispeech.requests.CreateRealtimeSessionTokenRequest = { |
| 55 | + createRealtimeSessionTokenDetails: createRealtimeSessionTokenDetails, |
| 56 | + }; |
| 57 | + |
| 58 | + const createRealtimeSessionTokenResponse = await speechClient.createRealtimeSessionToken(createRealtimeSessionTokenRequest); |
| 59 | + console.log("Token generated: ", createRealtimeSessionTokenResponse); |
| 60 | + return createRealtimeSessionTokenResponse.realtimeSessionToken; |
| 61 | +} |
| 62 | + |
| 63 | +app.use(bodyParser.json()); |
| 64 | + |
| 65 | +app.get("/authenticate", async (req, res) => { |
| 66 | + try { |
| 67 | + const token = await getRealtimeToken(); |
| 68 | + console.log("Token Response: ", token); |
| 69 | + res.send(token); |
| 70 | + } catch (error) { |
| 71 | + console.error("createRealtimeSessionToken Failed with error: ", error); |
| 72 | + res.status(500).send(error.toString()); |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +app.get("/region", (req, res) => { |
| 77 | + console.log('Received headers:', req.headers); |
| 78 | + res.send({ region: region }); |
| 79 | +}); |
| 80 | + |
| 81 | +// Using HTTPS server instead of the standard HTTP server |
| 82 | +https.createServer(options, app).listen(port, '0.0.0.0', () => { |
| 83 | + console.log(`https server running at https://0.0.0.0:${port}`); |
| 84 | +}); |
0 commit comments