Skip to content

Commit e0f78de

Browse files
committed
V5.2.1 - Added Cors
1 parent 7525337 commit e0f78de

File tree

1 file changed

+3
-25
lines changed

1 file changed

+3
-25
lines changed

index.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,20 @@ import cors from "cors"
55
import express from "express"
66
import basicAuth from "express-basic-auth"
77
import config from "./config.js"
8-
98
const __dirname = process.cwd()
109
const server = http.createServer()
1110
const app = express()
1211
const bareServer = createBareServer("/o/")
1312
const PORT = process.env.PORT || 8080
14-
1513
if (config.challenge) {
1614
console.log(`Password protection is enabled. Usernames are: ${Object.keys(config.users)}`)
1715
console.log(`Passwords are: ${Object.values(config.users)}`)
18-
19-
app.use(
20-
basicAuth({
21-
users: config.users,
22-
challenge: true,
23-
})
24-
)
16+
app.use(basicAuth({ users: config.users, challenge: true }))
2517
}
26-
2718
app.use(express.json())
2819
app.use(express.urlencoded({ extended: true }))
29-
app.use(cors())
3020
app.use(express.static(path.join(__dirname, "static")))
31-
21+
app.use("/o/", cors({ origin: true }))
3222
const routes = [
3323
{ path: "/as", file: "apps.html" },
3424
{ path: "/gm", file: "games.html" },
@@ -37,13 +27,11 @@ const routes = [
3727
{ path: "/", file: "index.html" },
3828
{ path: "/tos", file: "tos.html" },
3929
]
40-
4130
routes.forEach((route) => {
4231
app.get(route.path, (req, res) => {
4332
res.sendFile(path.join(__dirname, "static", route.file))
4433
})
4534
})
46-
4735
app.get("/e/*", (req, res, next) => {
4836
const baseUrls = [
4937
"https://raw.githubusercontent.com/v-5x/x/fixy",
@@ -52,21 +40,18 @@ app.get("/e/*", (req, res, next) => {
5240
]
5341
fetchData(req, res, next, baseUrls)
5442
})
55-
5643
const fetchData = async (req, res, next, baseUrls) => {
5744
try {
5845
const reqTarget = baseUrls.map((baseUrl) => `${baseUrl}/${req.params[0]}`)
5946
let data
6047
let asset
61-
6248
for (const target of reqTarget) {
6349
asset = await fetch(target)
6450
if (asset.ok) {
6551
data = await asset.arrayBuffer()
6652
break
6753
}
6854
}
69-
7055
if (data) {
7156
res.end(Buffer.from(data))
7257
} else {
@@ -77,32 +62,25 @@ const fetchData = async (req, res, next, baseUrls) => {
7762
res.status(500).send()
7863
}
7964
}
80-
8165
app.use((err, req, res, next) => {
8266
console.error(err.stack)
8367
res.status(500).send()
8468
})
85-
8669
server.on("request", (req, res) => {
8770
if (bareServer.shouldRoute(req)) {
8871
bareServer.routeRequest(req, res)
8972
} else {
9073
app(req, res)
9174
}
9275
})
93-
9476
server.on("upgrade", (req, socket, head) => {
9577
if (bareServer.shouldRoute(req)) {
9678
bareServer.routeUpgrade(req, socket, head)
9779
} else {
9880
socket.end()
9981
}
10082
})
101-
10283
server.on("listening", () => {
10384
console.log(`Running at http://localhost:${PORT}`)
10485
})
105-
106-
server.listen({
107-
port: PORT,
108-
})
86+
server.listen({ port: PORT })

0 commit comments

Comments
 (0)