[] firebase 연동 코드 및 각종 키 값 전부 비공개 처리

This commit is contained in:
김선규 2024-07-25 13:37:22 +09:00
parent 6291d5a83d
commit 0ddfa93c10
3 changed files with 155 additions and 203 deletions

View File

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

View File

@ -1,52 +1,16 @@
import { initializeApp as initializeClientApp, getApps, getApp } from 'firebase/app'; import { initializeApp as initializeClientApp, getApps, getApp } from 'firebase/app';
import admin from 'firebase-admin'; import admin from 'firebase-admin';
import jjungtable_AccKey from './private/jjungtable-AccountKey.json' assert { type: 'json' }; 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 === 'sManager')) {
// admin.initializeApp({
// credential: admin.credential.cert(sManager_AccKey),
// databaseURL: "https://checkanything-18bf9.firebaseio.com"
// }, 'sManager');
// }
// 파이어베이스 초기화 // 파이어베이스 초기화
if (!admin.apps.find(app => app.name === 'jjungtable')) { if (!admin.apps.find(app => app.name === 'jjungtable')) {
admin.initializeApp({ admin.initializeApp({
credential: admin.credential.cert(jjungtable_AccKey), credential: admin.credential.cert(jjungtable_AccKey),
databaseURL: "https://jjungtable-e5087-default-rtdb.firebaseio.com" databaseURL: fbURL,
}, 'jjungtable'); }, 'jjungtable');
} }
// export const sm_firebaseConfig = {
// apiKey: "AIzaSyCxLVOqbrnlDwfx6WMbmTGtCGrUNoL-CqI",
// authDomain: "checkanything-18bf9.firebaseapp.com",
// projectId: "checkanything-18bf9",
// storageBucket: "checkanything-18bf9.appspot.com",
// messagingSenderId: "928156583361",
// appId: "1:928156583361:web:d157abe34e637fb2395b40",
// measurementId: "G-NBSP3XCXX7"
// };
export const jj_firebaseConfig = {
apiKey: "AIzaSyCHJI2u1lbmHYgKRvPSO9-mPOGENBBPnsg",
authDomain: "jjungtable-e5087.firebaseapp.com",
databaseURL: "https://jjungtable-e5087-default-rtdb.firebaseio.com",
projectId: "jjungtable-e5087",
storageBucket: "jjungtable-e5087.appspot.com",
messagingSenderId: "480609810019",
appId: "1:480609810019:web:b66f72216fa17e1b462a56",
measurementId: "G-BQP2SDL40C"
};
// 클라이언트 앱 초기화 (이미 초기화된 경우 확인)
// let sm_ClientApp;
// if (!getApps().some(app => app.name === 'sManagerClientApp')) {
// sm_ClientApp = initializeClientApp(sm_firebaseConfig, 'sManagerClientApp');
// } else {
// sm_ClientApp = getApp('sManagerClientApp');
// }
let jj_ClientApp; let jj_ClientApp;
if (!getApps().some(app => app.name === 'jjungtableClientApp')) { if (!getApps().some(app => app.name === 'jjungtableClientApp')) {
jj_ClientApp = initializeClientApp(jj_firebaseConfig, 'jjungtableClientApp'); jj_ClientApp = initializeClientApp(jj_firebaseConfig, 'jjungtableClientApp');
@ -54,6 +18,4 @@ if (!getApps().some(app => app.name === 'jjungtableClientApp')) {
jj_ClientApp = getApp('jjungtableClientApp'); jj_ClientApp = getApp('jjungtableClientApp');
} }
// export { admin, sm_ClientApp, jj_ClientApp };
export { admin, jj_ClientApp }; export { admin, jj_ClientApp };

View File

@ -8,7 +8,7 @@ import { fileURLToPath } from 'url';
import cookieParser from 'cookie-parser'; import cookieParser from 'cookie-parser';
import cors from 'cors'; import cors from 'cors';
// import jjungTableApp from './back/apps/jjungTable_index.js'; import { serverURL, serverPort } from './private/config.js';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@ -18,6 +18,9 @@ console.log(process.cwd());
const app = express(); const app = express();
app.use(cors()); app.use(cors());
app.use(bodyParser.json()); // For parsing application/json app.use(bodyParser.json()); // For parsing application/json
app.use(cookieParser()); // For parsing cookies app.use(cookieParser()); // For parsing cookies
@ -33,8 +36,8 @@ const swaggerDefinition = {
}, },
servers: [ servers: [
{ {
url: `http://localhost:7002`, url: serverURL,
description: 'Local server', description: 'JJ server',
}, },
], ],
}; };
@ -61,8 +64,8 @@ app.use((req, res, next) => {
//JJUNGTABLE 서버 연결 //JJUNGTABLE 서버 연결
const jjungTable_server = express(); const jjungTable_server = express();
jjungTable_server.use(jjungTableApp); jjungTable_server.use(jjungTableApp);
jjungTable_server.listen(7002, () => { jjungTable_server.listen(serverPort, () => {
console.log(`Server running at http://localhost:7002/`); console.log(`Server running`);
}); });
// const sManagement_server = express(); // const sManagement_server = express();