forked from JJUNGTABLE/Back
Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
4dc0105060 | |||
13c8fd0ee7 | |||
f74de8256e | |||
83c60a7456 | |||
359e2a1802 | |||
2f9b72a041 | |||
360e01ffb8 | |||
e9a41233b5 | |||
d2835a39de | |||
21762a2d27 | |||
b43643ee16 | |||
880062e136 | |||
38161e67ae | |||
5a32a040a6 | |||
68f0925b5f | |||
9c7dba92c0 | |||
7cc458fceb | |||
15eef31cc8 | |||
7fac4e8d1e | |||
28c241105f | |||
64aab40748 |
61
apps/app.js
61
apps/app.js
|
@ -1,13 +1,42 @@
|
||||||
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 { dbConfig } from '../private/config.js';
|
import { dbConfig } from '../private/config.js';
|
||||||
|
|
||||||
// const cmcd = "/JJ";
|
import { HandlePush } from './push.js';
|
||||||
// const db = "/JJ/db";
|
import { HandleUser } from './user.js';
|
||||||
|
|
||||||
const app = express();
|
const router = express.Router();
|
||||||
app.use(express.json());
|
|
||||||
|
router.get('/version', async (req, res) => {
|
||||||
|
const {os_type} = req.query;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const connection = await createDatabaseConnection(dbConfig);
|
||||||
|
|
||||||
|
let query = 'SELECT * FROM version';
|
||||||
|
const queryParams = [];
|
||||||
|
|
||||||
|
if (os_type) {
|
||||||
|
query += ' ';
|
||||||
|
query += 'WHERE os_type = ?';
|
||||||
|
queryParams.push(os_type);
|
||||||
|
}
|
||||||
|
// const [rows] = await connection.query('SELECT * FROM version');
|
||||||
|
const [rows] = await connection.query(query, queryParams);
|
||||||
|
await connection.end();
|
||||||
|
|
||||||
|
res.json(rows);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
router.get('/push', HandlePush);
|
||||||
|
|
||||||
|
router.get('/user/*', HandleUser);
|
||||||
|
|
||||||
|
export default router;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,16 +102,16 @@ app.use(express.json());
|
||||||
|
|
||||||
|
|
||||||
// DB 연결 테스트
|
// DB 연결 테스트
|
||||||
app.get(`${db}/test`, async (req, res) => {
|
// app.get(`${db}/test`, async (req, res) => {
|
||||||
try {
|
// try {
|
||||||
const connection = await createDatabaseConnection(dbConfig);
|
// const connection = await createDatabaseConnection(dbConfig);
|
||||||
const [rows] = await connection.query('SELECT 1 + 1 AS solution');
|
// const [rows] = await connection.query('SELECT 1 + 1 AS solution');
|
||||||
await connection.end();
|
// await connection.end();
|
||||||
res.json({ result: rows[0].solution });
|
// res.json({ result: rows[0].solution });
|
||||||
} catch (err) {
|
// } catch (err) {
|
||||||
res.status(500).json({ error: err.message });
|
// res.status(500).json({ error: err.message });
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
|
|
||||||
// ///////////////////////////////////////////////////////////////
|
// ///////////////////////////////////////////////////////////////
|
||||||
|
@ -212,4 +241,4 @@ app.get(`${db}/test`, async (req, res) => {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default app;
|
// export default app;
|
82
apps/push.js
82
apps/push.js
|
@ -1,45 +1,53 @@
|
||||||
import { admin } from '../firebaseConfig.js';
|
import apn from 'apn';
|
||||||
|
import path from 'path'; // path 모듈을 가져옵니다.
|
||||||
|
import { fileURLToPath } from 'url'; // ES 모듈에서 파일 경로를 얻기 위해 필요
|
||||||
|
import { bundleID, apnKey, apnKeyID, apnTeamID } from '../private/config.js';
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
|
||||||
|
// APNs 옵션 설정
|
||||||
|
const options = {
|
||||||
|
token: {
|
||||||
|
// key: `../private/${apnKey}`, // 다운로드한 .p8 파일 경로
|
||||||
|
key: path.join(__dirname, '../private', apnKey), // 절대 경로로 변경
|
||||||
|
keyId: apnKeyID, // Apple Developer Console에서 얻은 키 ID
|
||||||
|
teamId: apnTeamID, // Apple Developer Console에서 얻은 팀 ID
|
||||||
|
},
|
||||||
|
production: false, // 프로덕션(실제 배포 환경)인 경우 true로 설정
|
||||||
|
};
|
||||||
|
|
||||||
|
const apnProvider = new apn.Provider(options);
|
||||||
|
|
||||||
export function HandlePush(req, res) {
|
export function HandlePush(req, res) {
|
||||||
const { fcmToken, title, body, parameter, sound, badge } = req.body;
|
// 쿼리 파라미터에서 값 추출
|
||||||
|
const { id: deviceToken, title, body, parameter, sound = "default", badge = 0 } = req.query;
|
||||||
|
|
||||||
if (!fcmToken || !title || !body) {
|
if (!deviceToken || !title || !body) {
|
||||||
res.status(400).json({ error: 'fcmToken, title, body, and parameter are required' });
|
res.status(400).json({ error: 'deviceToken, title, and body are required' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = {
|
const notification = new apn.Notification();
|
||||||
token: fcmToken,
|
notification.topic = bundleID;
|
||||||
notification: {
|
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
|
||||||
title: title,
|
notification.badge = Number(badge);
|
||||||
body: body,
|
notification.sound = sound;
|
||||||
},
|
notification.alert = { title, body };
|
||||||
apns: {
|
notification.payload = { parameter };
|
||||||
payload: {
|
|
||||||
aps: {
|
|
||||||
alert: {
|
|
||||||
title: title,
|
|
||||||
body: body,
|
|
||||||
parameter: parameter,
|
|
||||||
},
|
|
||||||
sound: sound || 'default',
|
|
||||||
badge: badge ? Number(badge) : 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
apnProvider.send(notification, deviceToken).then((response) => {
|
||||||
// const jjApp = admin.app('jjungtable');
|
if (response.sent.length > 0) {
|
||||||
|
console.log('Successfully sent message:', response.sent);
|
||||||
admin.app('jjungtable').messaging().send(message)
|
|
||||||
.then((response) => {
|
|
||||||
console.log('Successfully sent message:', response);
|
|
||||||
res.status(200).json({ message: 'Successfully sent message' });
|
res.status(200).json({ message: 'Successfully sent message' });
|
||||||
})
|
} else {
|
||||||
.catch((error) => {
|
console.error('Failed to send message:', response.failed);
|
||||||
console.error('Error sending message:', error);
|
res.status(500).json({ error: 'Failed to send message', details: response.failed });
|
||||||
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
}
|
||||||
});
|
}).catch((error) => {
|
||||||
}
|
console.error('Error sending message:', error);
|
||||||
|
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
||||||
|
}).finally(() => {
|
||||||
|
apnProvider.shutdown();
|
||||||
|
});
|
||||||
|
}
|
58
apps/user.js
Normal file
58
apps/user.js
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import express from 'express';
|
||||||
|
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일을 import
|
||||||
|
import { dbConfig } from '../private/config.js';
|
||||||
|
|
||||||
|
export const HandleUser = async (req, res) => {
|
||||||
|
const actionPath = req.params[0]; // 'userRead/sns'와 같은 나머지 경로를 잡아냄
|
||||||
|
const actionParts = actionPath.split('/'); // 슬래시로 나누어 배열로 변환
|
||||||
|
const action = actionParts[0]; // 첫 번째 부분 ('userRead')
|
||||||
|
|
||||||
|
if (action === 'userRead') {
|
||||||
|
// res.send(`Action: ${action}, ${actionParts}`); // 이 줄은 테스트용이므로 실제 응답 전에 사용 금지
|
||||||
|
|
||||||
|
let query;
|
||||||
|
let queryParams;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const connection = await createDatabaseConnection(dbConfig);
|
||||||
|
|
||||||
|
if (actionParts[1] === 'sns') {
|
||||||
|
const { sns_id } = req.query; // 쿼리 파라미터에서 sns_id를 받아옴
|
||||||
|
if (!sns_id) {
|
||||||
|
await connection.end();
|
||||||
|
return res.status(400).json({ error: 'sns_id is required' });
|
||||||
|
}
|
||||||
|
query = 'SELECT * FROM user_info WHERE sns_id = ?';
|
||||||
|
queryParams = [sns_id];
|
||||||
|
}
|
||||||
|
else if (actionParts[1] === 'app') {
|
||||||
|
const { app_id } = req.query;
|
||||||
|
if (!app_id) {
|
||||||
|
await connection.end();
|
||||||
|
return res.status(400).json({ error: 'app_id is required' });
|
||||||
|
}
|
||||||
|
query = 'SELECT * FROM user_info WHERE app_id = ?';
|
||||||
|
queryParams = [app_id];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
await connection.end();
|
||||||
|
return res.status(400).json({ error: 'Invalid sub-action' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const [rows] = await connection.query(query, queryParams);
|
||||||
|
|
||||||
|
await connection.end();
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
return res.status(404).json({ message: 'User not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json(rows); // 사용자의 첫 번째 정보를 반환
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
return res.status(500).json({ error: err.message });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return res.status(400).send('Invalid action');
|
||||||
|
}
|
||||||
|
};
|
|
@ -18,4 +18,4 @@ if (!getApps().some(app => app.name === 'jjungtableClientApp')) {
|
||||||
jj_ClientApp = getApp('jjungtableClientApp');
|
jj_ClientApp = getApp('jjungtableClientApp');
|
||||||
}
|
}
|
||||||
|
|
||||||
export { admin, jj_ClientApp };
|
export { admin, jj_ClientApp };
|
65
index.js
65
index.js
|
@ -2,13 +2,16 @@
|
||||||
|
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import swaggerJSDoc from 'swagger-jsdoc';
|
// import swaggerJSDoc from 'swagger-jsdoc';
|
||||||
import swaggerUi from 'swagger-ui-express';
|
// import swaggerUi from 'swagger-ui-express';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
import cookieParser from 'cookie-parser';
|
import cookieParser from 'cookie-parser';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
|
|
||||||
|
import session from 'express-session'; // express-session 추가
|
||||||
|
import routes from './apps/app.js'; // ./apps/app.js에서 라우트를 가져옴
|
||||||
|
|
||||||
import { serverURL, serverPort } from './private/config.js';
|
import { serverURL, serverPort } from './private/config.js';
|
||||||
|
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
@ -19,38 +22,34 @@ 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
|
||||||
app.use(express.static(path.join(__dirname, 'Front'))); // Serve static files from Pront directory
|
app.use(express.static(path.join(__dirname, 'Front'))); // Serve static files from Pront directory
|
||||||
|
|
||||||
// Swagger setup
|
// Swagger setup
|
||||||
const swaggerDefinition = {
|
// const swaggerDefinition = {
|
||||||
openapi: '3.0.0',
|
// openapi: '3.0.0',
|
||||||
info: {
|
// info: {
|
||||||
title: 'My API',
|
// title: 'My API',
|
||||||
version: '1.0.0',
|
// version: '1.0.0',
|
||||||
description: 'API documentation',
|
// description: 'API documentation',
|
||||||
},
|
// },
|
||||||
servers: [
|
// servers: [
|
||||||
{
|
// {
|
||||||
url: serverURL,
|
// url: serverURL,
|
||||||
description: 'JJ server',
|
// description: 'JJ server',
|
||||||
},
|
// },
|
||||||
],
|
// ],
|
||||||
};
|
// };
|
||||||
|
// const options = {
|
||||||
|
// swaggerDefinition,
|
||||||
|
// apis: ['./back/apps/app.js'],
|
||||||
|
// };
|
||||||
|
|
||||||
const options = {
|
// const swaggerSpec = swaggerJSDoc(options);
|
||||||
swaggerDefinition,
|
|
||||||
apis: ['./back/apps/push.js'],
|
|
||||||
};
|
|
||||||
|
|
||||||
const swaggerSpec = swaggerJSDoc(options);
|
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||||
|
|
||||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
const host = req.headers.host;
|
const host = req.headers.host;
|
||||||
|
@ -58,16 +57,20 @@ app.use((req, res, next) => {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.use('/', routes);
|
||||||
|
|
||||||
|
app.listen(serverPort, () => {
|
||||||
|
console.log(`Server running`);
|
||||||
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
|
|
||||||
//JJUNGTABLE 서버 연결
|
//JJUNGTABLE 서버 연결
|
||||||
const jjungTable_server = express();
|
// const jjungTable_server = express();
|
||||||
jjungTable_server.use(jjungTableApp);
|
// jjungTable_server.use(jjungTableApp);
|
||||||
jjungTable_server.listen(serverPort, () => {
|
// jjungTable_server.listen(serverPort, () => {
|
||||||
console.log(`Server running`);
|
// console.log(`Server running`);
|
||||||
});
|
// });
|
||||||
|
|
||||||
// const sManagement_server = express();
|
// const sManagement_server = express();
|
||||||
// sManagement_server.use(sManagementApp);
|
// sManagement_server.use(sManagementApp);
|
||||||
|
|
|
@ -13,6 +13,11 @@
|
||||||
"author": "Team.Stein",
|
"author": "Team.Stein",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.19.2"
|
"express": "^4.19.2",
|
||||||
|
"mysql2": "^3.11.0",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
|
"body-parser": "^1.19.2",
|
||||||
|
"express-session": "^1.17.3",
|
||||||
|
"cors": "^2.8.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user