-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
164 lines (142 loc) · 4.55 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const app = express();
const fs = require('fs');
const HOST_URL = "https://cheapshark.com";
// Enable CORS from chat.openai.com
app.use(cors({
origin: 'https://chat.openai.com'
}));
// Add a public directory
app.use(express.static('public'));
//==============================================
// Function: Deal Query String Builder
//==============================================
function buildDealQueryString(req) {
const {
storeID,
pageNumber = 0,
pageSize = 5,
sortBy = 'Deal Rating',
desc = 0,
lowerPrice = 0,
upperPrice,
metacritic,
steamRating,
steamAppID,
title,
exact = 0,
AAA = 0,
steamworks = 0,
onSale = 0,
output
} = req.query;
const queryParams = {
storeID,
pageNumber,
pageSize,
sortBy,
desc,
lowerPrice,
upperPrice,
metacritic,
steamRating,
steamAppID,
title,
exact,
AAA,
steamworks,
onSale,
output
};
Object.keys(queryParams).forEach(key => queryParams[key] === undefined && delete queryParams[key]);
const queryString = Object.keys(queryParams)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`)
.join('&');
return queryString;
}
//==============================================
// Route: Redirect root to Github
//==============================================
app.get('/', (req, res) => {
res.redirect('https://github.com/CyrisXD/GameDealGenie-ChatGPT-Plugin');
});
//==============================================
// Route: Get all deals
//==============================================
app.get('/deals', async (req, res) => {
let stores = await axios.get(
`${HOST_URL}/api/1.0/stores`
);
console.log(HOST_URL + "/api/1.0/deals?" + buildDealQueryString(req))
let response = await axios.get(
HOST_URL + "/api/1.0/deals?" + buildDealQueryString(req)
);
// replace store id with store name
response.data.forEach((deal) => {
let store = stores.data.find((store) => store.storeID === deal.storeID);
deal.storeName = store.storeName;
});
res.json(response.data);
});
//==============================================
// Route: Get store IDs
//==============================================
app.get('/getstoreids', async (req, res) => {
let response = await axios.get(
`${HOST_URL}/api/1.0/stores`
);
res.json(response.data);
});
//================================================
// Route: Get currency exchange rates
//================================================
app.get('/getcurrencyrates', async (req, res) => {
let response = await axios.get(
`https://api.exchangerate-api.com/v4/latest/usd`
);
res.json(response.data);
});
//================================================
// Route: Serve Legal Page
//================================================
app.get('/legal', (req, res) => {
res.sendFile(__dirname + '/public/legal.html');
});
//================================================
// Route: Serve ai-plugin.json
//================================================
app.get('/.well-known/ai-plugin.json', (req, res) => {
let host = req.headers['host'];
let protocol = req.headers['x-forwarded-proto'] || 'http';
fs.readFile('./.well-known/ai-plugin.json', 'utf8', (err, text) => {
if (err) {
return res.status(500).send({ message: err });
}
text = text.replace(/PLUGIN_HOSTNAME/g, `${protocol}://${host}`);
res.setHeader('Content-Type', 'application/json');
res.send(text);
});
});
//================================================
// Route: Serve openapi.yaml
//================================================
app.get('/openapi.yaml', (req, res) => {
let host = req.headers['host'];
let protocol = req.headers['x-forwarded-proto'] || 'http';
fs.readFile('openapi.yaml', 'utf8', (err, text) => {
if (err) {
return res.status(500).send({ message: err });
}
text = text.replace(/PLUGIN_HOSTNAME/g, `${protocol}://${host}`);
res.setHeader('Content-Type', 'text/yaml');
res.send(text);
});
});
//================================================
// Start the server and listen on port 80
//================================================
app.listen(3000, '0.0.0.0', () => {
console.log('Server is running on port 3000');
});