Skip to content

Commit

Permalink
Added so many new stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Aug 6, 2023
1 parent e1a107b commit c947bb3
Show file tree
Hide file tree
Showing 40 changed files with 8,348 additions and 256 deletions.
608 changes: 608 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"node": ">=18.x"
},
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js"
"start": "npm run build:website && npm run api",
"build:website": "cd website && npm run build",
"api": "node src/index.js",
"dev": "npm run dev:website & npm run dev:api",
"dev:website": "cd website && npm run dev",
"dev:api": "nodemon src/index.js"
},
"dependencies": {
"chalk": "^4.1.2",
Expand All @@ -28,6 +32,7 @@
"moment": "^2.29.4",
"mongodb": "^3.6.9",
"mongoose": "^5.13.20",
"next": "^13.4.12",
"ora": "^5.4.1",
"owoify-js": "^2.0.0",
"path": "^0.12.7",
Expand All @@ -36,6 +41,7 @@
"winston-daily-rotate-file": "^4.7.1"
},
"devDependencies": {
"concurrently": "^8.2.0",
"nodemon": "^2.0.22",
"prettier": "^2.8.8"
},
Expand Down
66 changes: 46 additions & 20 deletions src/app.js
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;
73 changes: 73 additions & 0 deletions src/controllers/utils/user.js
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)
}
}
91 changes: 0 additions & 91 deletions src/frontend/index.html

This file was deleted.

Loading

0 comments on commit c947bb3

Please sign in to comment.