-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.mjs
87 lines (77 loc) · 1.98 KB
/
app.mjs
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
import express from "express";
import { NFTStorage, File } from "nft.storage";
import axios from "axios";
import { OpenAI } from "openai";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const app = express();
app.use(cors());
app.use(express.json());
const PORT = 8080;
const tubeStations = [
"Paddington",
"Victoria",
"King's Cross St Pancras",
"Liverpool Street",
"Waterloo",
"Baker Street",
"Oxford Circus",
"Bond Street",
"Holborn",
"Euston Square",
];
// Route to get all station names
app.get("/stations", (req, res) => {
res.json({ stations: tubeStations });
});
// Route to get information about a specific station
app.get("/stations/:stationName", (req, res) => {
const stationName = req.params.stationName;
const stationInfo = {
name: stationName,
// You can add more information about the station if needed
};
res.json(stationInfo);
});
app.get("/", (req, res) => {
res.send("Hello world!");
});
app.post("/generate", async (req, res) => {
const destination = req.body.destination;
if (!destination) {
res.status(400).json({ error: "Destination not provided" });
return;
}
console.log(`Generating image for ${destination} station...`);
const response = await openai.images.generate({
model: "dall-e-3",
prompt: `${destination} station, London`,
n: 1,
size: "1024x1024",
});
console.log("Generated image!");
console.log(`${response.data[0].url}`);
res.json({
url: response.data[0].url,
});
});
app.get("/getPrice", async (req, res) => {
const destination = req.body.destination;
if (!destination) {
res.status(400).json({ error: "Destination not provided" });
return;
}
const price = (Math.random() * (10 - 1) + 1).toFixed(2);
res.json({
destination: destination,
price: price,
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});