기본적인 설정들을 main으로 merge 승인

Merge pull request 'dev.sean' (#1) from dev.sean into main
This commit is contained in:
김선규 2024-07-25 04:59:08 +00:00
commit 53d3be333d
5 changed files with 365 additions and 7 deletions

215
apps/app.js Normal file
View File

@ -0,0 +1,215 @@
import express, { query } from 'express';
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일
import { HandlePush } from './push.js';
import { dbConfig } from '../private/config.js';
// const cmcd = "/JJ";
// const db = "/JJ/db";
const app = express();
app.use(express.json());
// const getQueryModule = async (type, className) => {
// if (type === 'create') {
// const module = await import('../../db/queries/create_query.js');
// return module[className];
// }
// else if (type === 'read') {
// const module = await import('../../db/queries/read_query.js');
// return module[className];
// }
// else if (type === 'update') {
// const module = await import('../../db/queries/update_query.js');
// return module[className];
// }
// else if (type === 'delete') {
// const module = await import('../../db/queries/update_query.js');
// return module[className];
// }
// else if (type === 'function') {
// const module = await import('../../db/queries/functions_query.js');
// return module[className];
// }
// return ''
// };
// app.get(`${cmcd}/push`, async (req, res) => {
// // HandlePush(req, res);
// //3138117983JJ20240611
// // const { cid, title, body, cd } = req.query;
// const { cid, title, body, badge, parameter } = req.query;
// if (!cid || !title || !body) {
// return res.status(400).json({ error: 'All parameters (cid, title, body) are required' });
// }
// let query;
// query = await getQueryModule('function', 'Push');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// try {
// const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db'});
// const [rows] = await connection.query(query.loadFCM_token, [cid]);
// await connection.end();
// if (rows.length === 0) {
// return res.status(404).json({ error: 'No fcm_token found for the provided cid' });
// }
// const fcmToken = rows[0].fcm_token;
// console.log(fcmToken);
// // HandlePush 호출 시 필요한 req.body에 fcmToken, title, body 설정
// req.body = { fcmToken, title, body, badge, parameter };
// HandlePush(req, res);
// } catch (err) {
// console.error(err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
// DB 연결 테스트
app.get(`${db}/test`, async (req, res) => {
try {
const connection = await createDatabaseConnection(dbConfig);
const [rows] = await connection.query('SELECT 1 + 1 AS solution');
await connection.end();
res.json({ result: rows[0].solution });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// ///////////////////////////////////////////////////////////////
// // GET
// ///////////////////////////////////////////////////////////////
// app.get(`${db}/version/read`, async (req, res) => {
// const { os_type } = req.query;
// if (!os_type) { return res.status(400).json({ error: 'ostype parameter is required' }) }
// let query;
// query = await getQueryModule('read', 'Read');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// try {
// const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' });
// const [rows] = await connection.query(query.version, [os_type]);
// await connection.end();
// if (rows.length === 0) {
// return res.status(404).json({ error: 'No data found' });
// }
// res.json(rows);
// } catch (err) {
// console.error(err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
// app.get(`${db}/userInfo/read`, async (req, res) => {
// const { id, type } = req.query;
// if (!id || !type) { return res.status(400).json({ error: 'ostype parameter is required' }) }
// let query;
// query = await getQueryModule('read', 'Read');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// try {
// const connection = await createDatabaseConnection({ ...dbConfig, database: 'jjungtable_db' });
// const [rows] = await connection.query(query.login_code, [id, type]);
// await connection.end();
// if (rows.length === 0) {
// return res.status(404).json({ error: 'No data found' });
// }
// res.json(rows);
// } catch (err) {
// console.error(err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
// ///////////////////////////////////////////////////////////////
// // POST
// ///////////////////////////////////////////////////////////////
// async function insertLog(table, connection, query, body) {
// if (table === 'version') {
// const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = body;
// await connection.query( query, [date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log] );
// }
// }
// app.post(`${db}/version/update`, async (req, res) => {
// const { date, managerId, os_type, final_ver, force_ver, update_ver, update_choice, log } = req.body;
// try {
// const connection = await createDatabaseConnection({ ...dbConfig});
// let query;
// // 버전 정보 업데이트
// query = await getQueryModule('update', 'Update');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// await connection.query( query.version,[final_ver, force_ver, update_ver, update_choice, os_type]);
// // 로그 생성
// query = await getQueryModule('create', 'Log');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// await insertLog('version', connection, query.version_Log, req.body);
// await connection.end();
// res.json({ message: 'Update successful' });
// } catch (err) {
// res.status(500).json({ error: err.message });
// }
// });
// ///////////////////////////////////////////////////////////////
// // LOG 관련
// // DB Log 읽어들이기
// app.get(`${db}/log/read`, async (req, res) => {
// console.log('[JJ]Log: Version');
// const { param } = req.query;
// try {
// const connection = await createDatabaseConnection({ ...dbConfig });
// let query, rows;
// // let rows;
// if (param === 'version') {
// query = await getQueryModule('read', 'Log');
// if (!query) { return res.status(400).json({ error: 'Invalid parameter' }); }
// [rows] = await connection.query(query.version_Log);
// }
// await connection.end();
// res.json(rows);
// } catch (err) {
// console.error(err);
// res.status(500).json({ error: 'Internal server error' });
// }
// });
export default app;

45
apps/push.js Normal file
View File

@ -0,0 +1,45 @@
import { admin } from '../firebaseConfig.js';
export function HandlePush(req, res) {
const { fcmToken, title, body, parameter, sound, badge } = req.body;
if (!fcmToken || !title || !body) {
res.status(400).json({ error: 'fcmToken, title, body, and parameter are required' });
return;
}
const message = {
token: fcmToken,
notification: {
title: title,
body: body,
},
apns: {
payload: {
aps: {
alert: {
title: title,
body: body,
parameter: parameter,
},
sound: sound || 'default',
badge: badge ? Number(badge) : 0,
},
},
},
};
// const jjApp = admin.app('jjungtable');
admin.app('jjungtable').messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
res.status(200).json({ message: 'Successfully sent message' });
})
.catch((error) => {
console.error('Error sending message:', error);
res.status(500).json({ error: `Error sending message: ${error.message}` });
});
}

16
db/database.js Normal file
View File

@ -0,0 +1,16 @@
import mysql from 'mysql2/promise';
// 데이터베이스 연결 설정 함수
export const createDatabaseConnection = (config) => {
return mysql.createConnection({
host: config.host,
user: config.user,
password: config.password,
database: config.database,
port: config.port,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
};

21
firebaseConfig.js Normal file
View File

@ -0,0 +1,21 @@
import { initializeApp as initializeClientApp, getApps, getApp } from 'firebase/app';
import admin from 'firebase-admin';
import jjungtable_AccKey from './private/jjungtable-AccountKey.json' assert { type: 'json' };
import { fbURL, jj_firebaseConfig } from './private/config.js';
// 파이어베이스 초기화
if (!admin.apps.find(app => app.name === 'jjungtable')) {
admin.initializeApp({
credential: admin.credential.cert(jjungtable_AccKey),
databaseURL: fbURL,
}, 'jjungtable');
}
let jj_ClientApp;
if (!getApps().some(app => app.name === 'jjungtableClientApp')) {
jj_ClientApp = initializeClientApp(jj_firebaseConfig, 'jjungtableClientApp');
} else {
jj_ClientApp = getApp('jjungtableClientApp');
}
export { admin, jj_ClientApp };

View File

@ -1,15 +1,76 @@
// index.js
import express from 'express';
import bodyParser from 'body-parser';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import path from 'path';
import { fileURLToPath } from 'url';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import { serverURL, serverPort } from './private/config.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log(__dirname);
console.log(process.cwd());
const app = express();
const port = 7002;
app.get('/', (req, res) => {
res.send('Hello World!123123123123');
app.use(cors());
app.use(bodyParser.json()); // For parsing application/json
app.use(cookieParser()); // For parsing cookies
app.use(express.static(path.join(__dirname, 'Front'))); // Serve static files from Pront directory
// Swagger setup
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
description: 'API documentation',
},
servers: [
{
url: serverURL,
description: 'JJ server',
},
],
};
const options = {
swaggerDefinition,
apis: ['./back/apps/push.js'],
};
const swaggerSpec = swaggerJSDoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use((req, res, next) => {
const host = req.headers.host;
console.log(`Client connected to host: ${host}`);
next();
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
// Start the server
//JJUNGTABLE 서버 연결
const jjungTable_server = express();
jjungTable_server.use(jjungTableApp);
jjungTable_server.listen(serverPort, () => {
console.log(`Server running`);
});
// const sManagement_server = express();
// sManagement_server.use(sManagementApp);
// sManagement_server.listen(6004, () => {
// console.log(`Server running at https://localhost:6004/`);
// });