-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
8,348 additions
and
256 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,58 @@ | ||
const express = require('express') | ||
const cors = require('cors') | ||
// const winston = require('winston') | ||
// const expressWinston = require('express-winston') | ||
// const requestIp = require('request-ip'); | ||
const routes = require('./routes') | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const next = require('next'); | ||
const path = require('path'); | ||
|
||
// Import custom error handlers and logger | ||
const { | ||
handler404, | ||
errorsLogger, | ||
errorsHandler, | ||
} = require('./handlers/errors/index') | ||
require('winston-daily-rotate-file') | ||
const { ipLogger } = require('./handlers/logger/ip') | ||
const path = require('path') | ||
} = require('./handlers/errors/index'); | ||
const { ipLogger } = require('./handlers/logger/ip'); | ||
const routes = require('./routes'); | ||
|
||
// Set development mode | ||
const dev = process.env.NODE_ENV !== 'production'; | ||
|
||
// Initialize Next.js app | ||
// const nextApp = next({ dev, dir: path.join(__dirname, '../website') }); | ||
// const handle = nextApp.getRequestHandler(); | ||
|
||
// Express APP | ||
const app = express() | ||
app.use(cors()) | ||
app.set('trust proxy', 1) | ||
const app = express(); | ||
|
||
// Enable CORS for all routes | ||
app.use(cors()); | ||
|
||
// Trust the first proxy (when running behind a reverse proxy like Nginx) | ||
app.set('trust proxy', 1); | ||
|
||
// Logger | ||
// Middleware to parse JSON-encoded bodies | ||
app.use(express.json()); | ||
|
||
// Middleware to parse URL-encoded bodies | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
|
||
// Logger middleware | ||
// Uncomment the following block if you want to enable IP logging | ||
if (process.env.LOGGER === 'true') { | ||
app.use(ipLogger) | ||
app.use(ipLogger); | ||
} | ||
|
||
// Main website (waifu.it) | ||
app.use('/', express.static(path.join(__dirname, 'frontend'))) | ||
// Custom API routes | ||
app.use(routes); | ||
|
||
// Serve Next.js static files from the frontend folder | ||
app.use('/', express.static(path.join(__dirname, '../website/.next'))); | ||
|
||
// Handle server-side rendering for Next.js pages | ||
// app.get('*', (req, res) => { | ||
// return handle(req, res); | ||
// }); | ||
|
||
app.use(routes) | ||
app.use(handler404, errorsLogger, errorsHandler) | ||
// Error handling middleware | ||
app.use(handler404, errorsLogger, errorsHandler); | ||
|
||
module.exports = app | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Assuming you have imported the required modules and set up the MongoDB connection. | ||
const crypto = require('crypto') | ||
const Users = require('../../models/schemas/Users') | ||
const generateToken = require('../../utils/generateToken') | ||
|
||
module.exports = async function userEndpoint(req, res, next) { | ||
try { | ||
const { body, headers, method } = req | ||
const { key } = headers | ||
|
||
if (!key || key !== process.env.ACCESS_KEY) { | ||
return res.status(401).json({ | ||
message: 'Unauthorized', | ||
}) | ||
} | ||
if (method === 'POST') { | ||
const { token, id } = body | ||
|
||
if (!token || !id) { | ||
return res.status(400).json({ | ||
message: 'Token and User ID are required in the request body', | ||
}) | ||
} | ||
|
||
// Assuming you have a MongoDB collection called "users" and you want to update the token for a specific user. | ||
// Replace "Users" with your actual model name for users. | ||
await Users.updateOne( | ||
{ _id: id }, | ||
{ $set: { token: token } }, | ||
{ upsert: true } // This option creates the document if it doesn't exist. | ||
) | ||
|
||
return res.status(200).json({ | ||
message: 'Token updated successfully', | ||
}) | ||
} else if (method === 'GET') { | ||
const { id, email } = headers | ||
|
||
if (!id) { | ||
return res.status(400).json({ | ||
message: 'User ID missing in the request body', | ||
}) | ||
} | ||
|
||
// Assuming you have a MongoDB collection called "users" and you want to fetch user details based on the provided user ID. | ||
// Replace "Users" with your actual model name for users. | ||
const user = await Users.findOne({ _id: id }) | ||
|
||
if (!user) { | ||
// If user not found, create a new user with the provided ID and token. | ||
const newUser = { | ||
_id: id, | ||
email: email, | ||
password: crypto.randomBytes(22).toString('base64'), | ||
token: generateToken(id, process.env.HMAC_KEY), // Assuming you have a "token" field in your schema. | ||
// Add other fields in the "newUser" object based on your schema. | ||
} | ||
|
||
await Users.create(newUser) | ||
|
||
return res.status(201).json(newUser) | ||
} | ||
|
||
return res.status(200).json(user.token) | ||
} else { | ||
return res.status(405).json({ | ||
message: 'Method Not Allowed', | ||
}) | ||
} | ||
} catch (error) { | ||
return next(error) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.