Spaces:
Running
Running
File size: 4,420 Bytes
26c2f3f 3ec3f5f c129e9b 26c2f3f 697deee 26c2f3f 697deee 26c2f3f e5eb4d0 c129e9b e5eb4d0 c129e9b e5eb4d0 faca0e0 e5eb4d0 faca0e0 e5eb4d0 faca0e0 c129e9b e5eb4d0 c129e9b e5eb4d0 c129e9b e5eb4d0 697deee 26c2f3f |
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 |
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const session = require('express-session');
const path = require('path');
const fetch = require('node-fetch');
const cors = require('cors');
const logger = require('./logger');
const bcrypt = require('bcryptjs');
const User = require('./models/User'); // Assuming User model is defined for your MongoDB users
const app = express();
const port = process.env.PORT || 3001;
// MongoDB connection setup
const mongoUri = process.env.MONGO_URI;
mongoose.connect(mongoUri)
.then(() => logger.info('Connected to MongoDB successfully'))
.catch(err => logger.error('Failed to connect to MongoDB:', err));
// Configuration and constants setup
const config = {
SESSION_SECRET: process.env.SESSION_SECRET || "default-session-secret"
};
const STREMIO_API = {
BASE_URL: "https://api.strem.io/api",
LOGIN: "/login",
REGISTER: "/register",
ADDON_COLLECTION_GET: "/addonCollectionGet",
ADDON_COLLECTION_SET: "/addonCollectionSet",
LOGOUT: "/logout"
};
const corsOptions = {
origin: "*",
methods: ["GET", "POST", "DELETE", "PUT", "PATCH"],
allowedHeaders: ["Content-Type", "Authorization"],
credentials: true,
maxAge: 86400
};
// Process error handlers
process.on("uncaughtException", (error) => {
logger.error("Uncaught Exception:", error);
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
logger.error("Unhandled Rejection at:", promise, "reason:", reason);
});
// Middleware setup
app.use(cors(corsOptions));
app.options("*", cors(corsOptions));
app.use(express.json({ limit: "50mb" }));
app.use(express.static("public"));
app.use(session({
secret: config.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 24 * 60 * 60 * 1000
}
}));
// POST login route
app.post('/api/login', async (req, res) => {
const { email, password } = req.body;
try {
logger.info('Attempting login for email:', email);
const user = await User.findOne({ email });
if (!user) {
logger.info('User not found');
return res.status(401).send('User not found');
}
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) {
logger.info('Invalid credentials');
return res.status(401).send('Invalid credentials');
}
// Session handling
req.session.user = user; // Save user to session
res.status(200).send({ message: 'Login successful' });
} catch (error) {
logger.error('Login error:', error);
// Send generic error message if unexpected token error occurs
if (error.message && error.message.startsWith('Unexpected token')) {
return res.status(500).send('Internal server error: invalid response format');
} else {
return res.status(500).send('Internal server error');
}
}
});
// Register route for user creation with bcrypt hashing
app.post('/api/register', async (req, res) => {
const { email, password } = req.body;
try {
const hashedPassword = await bcrypt.hash(password, 10);
const newUser = new User({ email, password: hashedPassword });
await newUser.save();
res.status(201).send({ message: 'User registered successfully' });
} catch (error) {
logger.error('Registration error:', error);
res.status(500).send('Internal server error');
}
});
// Hugging Face API Example Function (to demonstrate API interaction)
async function fetchHuggingFaceData(apiUrl) {
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP Error: ${response.status}`);
}
const data = await response.json();
logger.info('Hugging Face API response:', data);
return data;
} catch (error) {
logger.error('Failed to fetch Hugging Face API:', error);
throw error;
}
}
// Server initialization
async function startServer() {
try {
app.listen(port, () => {
logger.info(`Server started on port ${port}`);
});
} catch (error) {
logger.error('Failed to initialize:', error);
process.exit(1);
}
}
startServer();
module.exports = app;
|