@@ -5,30 +5,20 @@ import cors from "cors"
55import express from "express"
66import basicAuth from "express-basic-auth"
77import config from "./config.js"
8-
98const __dirname = process . cwd ( )
109const server = http . createServer ( )
1110const app = express ( )
1211const bareServer = createBareServer ( "/o/" )
1312const PORT = process . env . PORT || 8080
14-
1513if ( 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-
2718app . use ( express . json ( ) )
2819app . use ( express . urlencoded ( { extended : true } ) )
29- app . use ( cors ( ) )
3020app . use ( express . static ( path . join ( __dirname , "static" ) ) )
31-
21+ app . use ( "/o/" , cors ( { origin : true } ) )
3222const 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-
4130routes . forEach ( ( route ) => {
4231 app . get ( route . path , ( req , res ) => {
4332 res . sendFile ( path . join ( __dirname , "static" , route . file ) )
4433 } )
4534} )
46-
4735app . 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-
5643const 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-
8165app . use ( ( err , req , res , next ) => {
8266 console . error ( err . stack )
8367 res . status ( 500 ) . send ( )
8468} )
85-
8669server . on ( "request" , ( req , res ) => {
8770 if ( bareServer . shouldRoute ( req ) ) {
8871 bareServer . routeRequest ( req , res )
8972 } else {
9073 app ( req , res )
9174 }
9275} )
93-
9476server . on ( "upgrade" , ( req , socket , head ) => {
9577 if ( bareServer . shouldRoute ( req ) ) {
9678 bareServer . routeUpgrade ( req , socket , head )
9779 } else {
9880 socket . end ( )
9981 }
10082} )
101-
10283server . 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