init
This commit is contained in:
72
connect.js
Normal file
72
connect.js
Normal file
@ -0,0 +1,72 @@
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
console.log('Starting server...');
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
function convertToGiteaFormat(originalData) {
|
||||
console.log('Converting data:', JSON.stringify(originalData));
|
||||
|
||||
return {
|
||||
id: originalData.id,
|
||||
login: originalData.username,
|
||||
full_name: originalData.name || originalData.username,
|
||||
email: `${originalData.username}@example.com`,
|
||||
avatar_url: "",
|
||||
language: "zh-CN",
|
||||
is_admin: false,
|
||||
last_login: new Date().toISOString(),
|
||||
created: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString(),
|
||||
restricted: false,
|
||||
active: originalData.active,
|
||||
prohibit_login: originalData.silenced,
|
||||
location: "",
|
||||
website: "",
|
||||
description: "",
|
||||
visibility: "public",
|
||||
followers_count: 0,
|
||||
following_count: 0,
|
||||
starred_repos_count: 0,
|
||||
username: originalData.username
|
||||
};
|
||||
}
|
||||
|
||||
app.get('/oauth/user', async (req, res) => {
|
||||
console.log('Received request to /oauth/user');
|
||||
try {
|
||||
const token = req.query.access_token ;
|
||||
console.log('Authorization header:', token);
|
||||
|
||||
const response = await axios.get('https://connect.linux.do/api/user', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
|
||||
console.log('Received response from original API:', JSON.stringify(response.data));
|
||||
|
||||
const format = convertToGiteaFormat(response.data);
|
||||
console.log('Converted:', JSON. stringify(format));
|
||||
|
||||
res.json(format);
|
||||
} catch (error) {
|
||||
console.error('Error:', error.message);
|
||||
res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`API transform service listening at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
// 添加未捕获异常处理
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught Exception:', error);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
});
|
||||
|
||||
console.log('Server setup completed');
|
Reference in New Issue
Block a user