Fix database permissions and initialization issues
This commit is contained in:
11
Dockerfile
11
Dockerfile
@@ -23,18 +23,18 @@ RUN cd frontend && npm run build
|
|||||||
# Build backend
|
# Build backend
|
||||||
RUN cd backend && npm run build
|
RUN cd backend && npm run build
|
||||||
|
|
||||||
# Create database file
|
|
||||||
RUN touch database.sqlite
|
|
||||||
|
|
||||||
# Production stage
|
# Production stage
|
||||||
FROM node:18-alpine
|
FROM node:18-alpine
|
||||||
|
|
||||||
# Set working directory
|
# Set working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Create uploads directory and set permissions before switching user
|
# Create necessary directories and set permissions
|
||||||
RUN mkdir -p /app/uploads/wallpapers && \
|
RUN mkdir -p /app/uploads/wallpapers && \
|
||||||
chown -R node:node /app
|
touch /app/database.sqlite && \
|
||||||
|
chown -R node:node /app && \
|
||||||
|
chmod 755 /app/uploads && \
|
||||||
|
chmod 644 /app/database.sqlite
|
||||||
|
|
||||||
# Copy package files and install dependencies
|
# Copy package files and install dependencies
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
@@ -45,7 +45,6 @@ RUN cd backend && npm install --production
|
|||||||
# Copy built files from builder stage
|
# Copy built files from builder stage
|
||||||
COPY --from=builder /app/backend/dist ./dist
|
COPY --from=builder /app/backend/dist ./dist
|
||||||
COPY --from=builder /app/frontend/build ./frontend/build
|
COPY --from=builder /app/frontend/build ./frontend/build
|
||||||
COPY --from=builder /app/database.sqlite ./database.sqlite
|
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
USER node
|
USER node
|
||||||
|
|||||||
@@ -23,10 +23,21 @@ app.use(express.static(path.join(__dirname, '../frontend/build')));
|
|||||||
let db: any;
|
let db: any;
|
||||||
|
|
||||||
async function connectToDatabase() {
|
async function connectToDatabase() {
|
||||||
db = await open({
|
try {
|
||||||
filename: './database.sqlite',
|
// Ensure database directory exists with proper permissions
|
||||||
driver: sqlite3.Database
|
const dbPath = path.join(__dirname, '../database.sqlite');
|
||||||
});
|
|
||||||
|
db = await open({
|
||||||
|
filename: dbPath,
|
||||||
|
driver: sqlite3.Database
|
||||||
|
});
|
||||||
|
|
||||||
|
// Initialize tables immediately after connection
|
||||||
|
await initializeDatabase();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error connecting to database:', error);
|
||||||
|
process.exit(1); // Exit if we can't connect to the database
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure multer for file uploads
|
// Configure multer for file uploads
|
||||||
@@ -254,6 +265,7 @@ app.put('/api/events/:slug/rsvps/:id', async (req: Request, res: Response) => {
|
|||||||
// Initialize database tables
|
// Initialize database tables
|
||||||
async function initializeDatabase() {
|
async function initializeDatabase() {
|
||||||
try {
|
try {
|
||||||
|
// Create events table
|
||||||
await db.exec(`
|
await db.exec(`
|
||||||
CREATE TABLE IF NOT EXISTS events (
|
CREATE TABLE IF NOT EXISTS events (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -268,6 +280,7 @@ async function initializeDatabase() {
|
|||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
// Create RSVPs table
|
||||||
await db.exec(`
|
await db.exec(`
|
||||||
CREATE TABLE IF NOT EXISTS rsvps (
|
CREATE TABLE IF NOT EXISTS rsvps (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -285,6 +298,7 @@ async function initializeDatabase() {
|
|||||||
console.log('Database initialized successfully');
|
console.log('Database initialized successfully');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error initializing database:', error);
|
console.error('Error initializing database:', error);
|
||||||
|
throw error; // Re-throw to handle in the connection function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -297,5 +311,4 @@ app.get('*', (req: Request, res: Response) => {
|
|||||||
app.listen(port, async () => {
|
app.listen(port, async () => {
|
||||||
console.log(`Server running on port ${port}`);
|
console.log(`Server running on port ${port}`);
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
await initializeDatabase();
|
|
||||||
});
|
});
|
||||||
@@ -7,7 +7,7 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "3000:3000"
|
- "3000:3000"
|
||||||
volumes:
|
volumes:
|
||||||
- data:/app/database
|
- data:/app/database.sqlite
|
||||||
- uploads:/app/uploads
|
- uploads:/app/uploads
|
||||||
environment:
|
environment:
|
||||||
- NODE_ENV=development
|
- NODE_ENV=development
|
||||||
|
|||||||
Reference in New Issue
Block a user