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
|
||||
RUN cd backend && npm run build
|
||||
|
||||
# Create database file
|
||||
RUN touch database.sqlite
|
||||
|
||||
# Production stage
|
||||
FROM node:18-alpine
|
||||
|
||||
# Set working directory
|
||||
WORKDIR /app
|
||||
|
||||
# Create uploads directory and set permissions before switching user
|
||||
# Create necessary directories and set permissions
|
||||
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*.json ./
|
||||
@@ -45,7 +45,6 @@ RUN cd backend && npm install --production
|
||||
# Copy built files from builder stage
|
||||
COPY --from=builder /app/backend/dist ./dist
|
||||
COPY --from=builder /app/frontend/build ./frontend/build
|
||||
COPY --from=builder /app/database.sqlite ./database.sqlite
|
||||
|
||||
# Switch to non-root user
|
||||
USER node
|
||||
|
||||
@@ -23,10 +23,21 @@ app.use(express.static(path.join(__dirname, '../frontend/build')));
|
||||
let db: any;
|
||||
|
||||
async function connectToDatabase() {
|
||||
db = await open({
|
||||
filename: './database.sqlite',
|
||||
driver: sqlite3.Database
|
||||
});
|
||||
try {
|
||||
// Ensure database directory exists with proper permissions
|
||||
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
|
||||
@@ -254,6 +265,7 @@ app.put('/api/events/:slug/rsvps/:id', async (req: Request, res: Response) => {
|
||||
// Initialize database tables
|
||||
async function initializeDatabase() {
|
||||
try {
|
||||
// Create events table
|
||||
await db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS events (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -268,6 +280,7 @@ async function initializeDatabase() {
|
||||
)
|
||||
`);
|
||||
|
||||
// Create RSVPs table
|
||||
await db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS rsvps (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -285,6 +298,7 @@ async function initializeDatabase() {
|
||||
console.log('Database initialized successfully');
|
||||
} catch (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 () => {
|
||||
console.log(`Server running on port ${port}`);
|
||||
await connectToDatabase();
|
||||
await initializeDatabase();
|
||||
});
|
||||
@@ -7,7 +7,7 @@ services:
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- data:/app/database
|
||||
- data:/app/database.sqlite
|
||||
- uploads:/app/uploads
|
||||
environment:
|
||||
- NODE_ENV=development
|
||||
|
||||
Reference in New Issue
Block a user