From 826f11719afb80807c4cbb2d8fee7fef9edf01b0 Mon Sep 17 00:00:00 2001 From: "seonkyu.kim" Date: Thu, 25 Jul 2024 13:37:22 +0900 Subject: [PATCH] =?UTF-8?q?[=E2=9C=A8]=20firebase=20=EC=97=B0=EB=8F=99=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B0=8F=20=EA=B0=81=EC=A2=85=20=ED=82=A4?= =?UTF-8?q?=20=EA=B0=92=20=EC=A0=84=EB=B6=80=20=EB=B9=84=EA=B3=B5=EA=B0=9C?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/app.js | 215 ++++++++++++++++++++++++++++++++++++++++++++++ firebaseConfig.js | 21 +++++ index.js | 75 ++++++++++++++-- 3 files changed, 304 insertions(+), 7 deletions(-) create mode 100644 apps/app.js create mode 100644 firebaseConfig.js diff --git a/apps/app.js b/apps/app.js new file mode 100644 index 0000000..58dc4ef --- /dev/null +++ b/apps/app.js @@ -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; \ No newline at end of file diff --git a/firebaseConfig.js b/firebaseConfig.js new file mode 100644 index 0000000..2b68ac8 --- /dev/null +++ b/firebaseConfig.js @@ -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 }; diff --git a/index.js b/index.js index e715334..9c06f48 100755 --- a/index.js +++ b/index.js @@ -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}`); -}); \ No newline at end of file + + +// 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/`); +// }); \ No newline at end of file