Compare commits
No commits in common. "main" and "dev.sean" have entirely different histories.
30
apps/app.js
30
apps/app.js
|
@ -2,40 +2,26 @@ import express, { query } from 'express';
|
|||
import { createDatabaseConnection } from '../db/database.js'; // db 연결 파일
|
||||
import { dbConfig } from '../private/config.js';
|
||||
|
||||
import { HandlePush } from './push.js';
|
||||
import { HandleUser } from './user.js';
|
||||
// import { HandlePush } from './push.js';
|
||||
// const cmcd = "/JJ";
|
||||
// const db = "/JJ/db";
|
||||
// const app = express();
|
||||
// app.use(express.json());
|
||||
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/version', async (req, res) => {
|
||||
const {os_type} = req.query;
|
||||
|
||||
router.get(`/version`, async (req, res) => {
|
||||
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);
|
||||
const [rows] = await connection.query('SELECT * FROM version');
|
||||
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;
|
||||
|
||||
|
||||
|
|
82
apps/push.js
82
apps/push.js
|
@ -1,53 +1,45 @@
|
|||
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);
|
||||
import { admin } from '../firebaseConfig.js';
|
||||
|
||||
export function HandlePush(req, res) {
|
||||
// 쿼리 파라미터에서 값 추출
|
||||
const { id: deviceToken, title, body, parameter, sound = "default", badge = 0 } = req.query;
|
||||
const { fcmToken, title, body, parameter, sound, badge } = req.body;
|
||||
|
||||
if (!deviceToken || !title || !body) {
|
||||
res.status(400).json({ error: 'deviceToken, title, and body are required' });
|
||||
if (!fcmToken || !title || !body) {
|
||||
res.status(400).json({ error: 'fcmToken, title, body, and parameter are required' });
|
||||
return;
|
||||
}
|
||||
|
||||
const notification = new apn.Notification();
|
||||
notification.topic = bundleID;
|
||||
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
|
||||
notification.badge = Number(badge);
|
||||
notification.sound = sound;
|
||||
notification.alert = { title, body };
|
||||
notification.payload = { parameter };
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
apnProvider.send(notification, deviceToken).then((response) => {
|
||||
if (response.sent.length > 0) {
|
||||
console.log('Successfully sent message:', response.sent);
|
||||
|
||||
// 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' });
|
||||
} else {
|
||||
console.error('Failed to send message:', response.failed);
|
||||
res.status(500).json({ error: 'Failed to send message', details: response.failed });
|
||||
}
|
||||
}).catch((error) => {
|
||||
console.error('Error sending message:', error);
|
||||
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
||||
}).finally(() => {
|
||||
apnProvider.shutdown();
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error sending message:', error);
|
||||
res.status(500).json({ error: `Error sending message: ${error.message}` });
|
||||
});
|
||||
}
|
||||
|
||||
|
|
58
apps/user.js
58
apps/user.js
|
@ -1,58 +0,0 @@
|
|||
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');
|
||||
}
|
||||
};
|
48
index.js
48
index.js
|
@ -2,8 +2,8 @@
|
|||
|
||||
import express from 'express';
|
||||
import bodyParser from 'body-parser';
|
||||
// import swaggerJSDoc from 'swagger-jsdoc';
|
||||
// import swaggerUi from 'swagger-ui-express';
|
||||
import swaggerJSDoc from 'swagger-jsdoc';
|
||||
import swaggerUi from 'swagger-ui-express';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import cookieParser from 'cookie-parser';
|
||||
|
@ -28,28 +28,29 @@ 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/app.js'],
|
||||
// };
|
||||
const swaggerDefinition = {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'My API',
|
||||
version: '1.0.0',
|
||||
description: 'API documentation',
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: serverURL,
|
||||
description: 'JJ server',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// const swaggerSpec = swaggerJSDoc(options);
|
||||
const options = {
|
||||
swaggerDefinition,
|
||||
apis: ['./back/apps/app.js'],
|
||||
};
|
||||
|
||||
// app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
const swaggerSpec = swaggerJSDoc(options);
|
||||
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const host = req.headers.host;
|
||||
|
@ -57,7 +58,8 @@ app.use((req, res, next) => {
|
|||
next();
|
||||
});
|
||||
|
||||
app.use('/', routes);
|
||||
|
||||
app.use('/JJ', routes);
|
||||
|
||||
app.listen(serverPort, () => {
|
||||
console.log(`Server running`);
|
||||
|
|
|
@ -13,11 +13,6 @@
|
|||
"author": "Team.Stein",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"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"
|
||||
"express": "^4.19.2"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user