feat: PostgreSQL 17 вместо SQLite
pg + connect-pg-simple, async routes, docker-compose, скрипт setup-postgres. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+71
-58
@@ -1,75 +1,88 @@
|
||||
const path = require('path');
|
||||
const express = require('express');
|
||||
const session = require('express-session');
|
||||
const SQLiteStore = require('connect-sqlite3')(session);
|
||||
|
||||
require('./db');
|
||||
require('./seed').runSeed();
|
||||
const pgSession = require('connect-pg-simple')(session);
|
||||
|
||||
const { pool, initSchema, checkConnection } = require('./db');
|
||||
const { runSeed } = require('./seed');
|
||||
const { loadUser } = require('./middleware/auth');
|
||||
const healthRoutes = require('./routes/health');
|
||||
const shopRoutes = require('./routes/shop');
|
||||
const authRoutes = require('./routes/auth');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const HOST = process.env.HOST || '0.0.0.0';
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
|
||||
if (process.env.TRUST_PROXY === '1' || isProduction) {
|
||||
app.set('trust proxy', 1);
|
||||
async function start() {
|
||||
await checkConnection();
|
||||
await initSchema();
|
||||
await runSeed();
|
||||
|
||||
const app = express();
|
||||
|
||||
if (process.env.TRUST_PROXY === '1' || isProduction) {
|
||||
app.set('trust proxy', 1);
|
||||
}
|
||||
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
app.use(healthRoutes);
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.use(
|
||||
session({
|
||||
store: new pgSession({
|
||||
pool,
|
||||
createTableIfMissing: true,
|
||||
tableName: 'session',
|
||||
}),
|
||||
secret: process.env.SESSION_SECRET || 'dev-secret-change-in-production',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
maxAge: 7 * 24 * 60 * 60 * 1000,
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: isProduction,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
app.use(loadUser);
|
||||
app.use('/', shopRoutes);
|
||||
app.use('/', authRoutes);
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(404).render('error', {
|
||||
title: 'Не найдено',
|
||||
message: 'Страница не найдена',
|
||||
code: 404,
|
||||
});
|
||||
});
|
||||
|
||||
app.use((err, req, res, _next) => {
|
||||
console.error(err);
|
||||
res.status(500).render('error', {
|
||||
title: 'Ошибка',
|
||||
message: 'Внутренняя ошибка сервера',
|
||||
code: 500,
|
||||
});
|
||||
});
|
||||
|
||||
const server = app.listen(PORT, HOST, () => {
|
||||
console.log(`Магазин: http://${HOST}:${PORT} (PostgreSQL)`);
|
||||
});
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.error('Не удалось запустить сервер:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
app.set('view engine', 'ejs');
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
app.use(healthRoutes);
|
||||
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.use(
|
||||
session({
|
||||
store: new SQLiteStore({ db: 'sessions.db', dir: path.join(__dirname, '..', 'data') }),
|
||||
secret: process.env.SESSION_SECRET || 'dev-secret-change-in-production',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {
|
||||
maxAge: 7 * 24 * 60 * 60 * 1000,
|
||||
httpOnly: true,
|
||||
sameSite: 'lax',
|
||||
secure: isProduction,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
app.use(loadUser);
|
||||
|
||||
app.use('/', shopRoutes);
|
||||
app.use('/', authRoutes);
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(404).render('error', {
|
||||
title: 'Не найдено',
|
||||
message: 'Страница не найдена',
|
||||
code: 404,
|
||||
});
|
||||
});
|
||||
|
||||
app.use((err, req, res, _next) => {
|
||||
console.error(err);
|
||||
res.status(500).render('error', {
|
||||
title: 'Ошибка',
|
||||
message: 'Внутренняя ошибка сервера',
|
||||
code: 500,
|
||||
});
|
||||
});
|
||||
|
||||
const server = app.listen(PORT, HOST, () => {
|
||||
console.log(`Магазин: http://${HOST}:${PORT}`);
|
||||
});
|
||||
|
||||
server.on('error', (err) => {
|
||||
console.error('Не удалось запустить сервер:', err.message);
|
||||
start().catch((err) => {
|
||||
console.error('Ошибка запуска:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user