Skip to content

Commit

Permalink
update template for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHallen122 committed Mar 1, 2025
1 parent accd1c0 commit da0f66c
Show file tree
Hide file tree
Showing 5 changed files with 2,492 additions and 68 deletions.
33 changes: 33 additions & 0 deletions backend/template/template-backend/database_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import { logger } from './logger';
import path from 'path';
import fs from 'fs';

const dbPath = path.join(__dirname, '../database.sqlite');

export const initializeDatabase = async () => {
try {
// Ensure directory exists
const dbDir = path.dirname(dbPath);
logger.info(dbDir);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}

// Open the database
const db = await open({
filename: dbPath,
driver: sqlite3.Database,
});

// Enable foreign keys
await db.exec('PRAGMA foreign_keys = ON');

logger.info('Database initialized successfully');
return db;
} catch (error) {
logger.error('Database initialization failed', error);
throw error;
}
};
11 changes: 11 additions & 0 deletions backend/template/template-backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express';
const app = express();
const port = 3000;

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
22 changes: 22 additions & 0 deletions backend/template/template-backend/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// /**
// * Simple logger utility for application logging
// */
// export const logger = {
// info: (message: string) => {
// console.log(`[INFO] ${new Date().toISOString()}: ${message}`);
// },
// error: (message: string, error?: any) => {
// console.error(`[ERROR] ${new Date().toISOString()}: ${message}`);
// if (error) {
// console.error(error);
// }
// },
// warn: (message: string) => {
// console.warn(`[WARN] ${new Date().toISOString()}: ${message}`);
// },
// debug: (message: string) => {
// if (process.env.NODE_ENV !== 'production') {
// console.debug(`[DEBUG] ${new Date().toISOString()}: ${message}`);
// }
// },
// };
Loading

0 comments on commit da0f66c

Please sign in to comment.