mirror of
https://github.com/Yourdaylight/CloudGame.git
synced 2026-07-28 01:25:17 +00:00
fixed: add api to the front
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
*.js.map
|
||||
*.ts
|
||||
.git*
|
||||
.vscode
|
||||
__azurite_db*__.json
|
||||
__blobstorage__
|
||||
__queuestorage__
|
||||
local.settings.json
|
||||
test
|
||||
tsconfig.json
|
||||
@@ -0,0 +1,99 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
.env.test
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
.cache
|
||||
|
||||
# next.js build output
|
||||
.next
|
||||
|
||||
# nuxt.js build output
|
||||
.nuxt
|
||||
|
||||
# vuepress build output
|
||||
.vuepress/dist
|
||||
|
||||
# Serverless directories
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
.dynamodb/
|
||||
|
||||
# TypeScript output
|
||||
dist
|
||||
out
|
||||
|
||||
# Azure Functions artifacts
|
||||
bin
|
||||
obj
|
||||
appsettings.json
|
||||
local.settings.json
|
||||
|
||||
# Azurite artifacts
|
||||
__blobstorage__
|
||||
__queuestorage__
|
||||
__azurite_db*__.json
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"version": "2.0",
|
||||
"logging": {
|
||||
"applicationInsights": {
|
||||
"samplingSettings": {
|
||||
"isEnabled": true,
|
||||
"excludedTypes": "Request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"extensionBundle": {
|
||||
"id": "Microsoft.Azure.Functions.ExtensionBundle",
|
||||
"version": "[3.*, 4.0.0)"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "api",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"start": "func start",
|
||||
"test": "echo \"No tests yet...\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@azure/functions": "^4.1.0",
|
||||
"mongodb": "^6.3.0"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"main": "src/functions/*.js"
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
const { app } = require('@azure/functions');
|
||||
const { MongoClient } = require('mongodb');
|
||||
var connectionString = "mongodb://games:oQ5bO7YMfpu99oZMpKs0fjjypybyIMwBHJh7TmK8FYj1J41StnByDp1vxZ0huSXxlYbNLiRtNdvZACDb9cByMQ%3D%3D@games.mongo.cosmos.azure.com:10255/?ssl=true&retrywrites=false&maxIdleTimeMS=120000&appName=@games@";
|
||||
const client = new MongoClient(connectionString);
|
||||
const DATABASE_NAME = "games";
|
||||
const COLLECTION = "games";
|
||||
app.http('games', {
|
||||
methods: ['POST'], // Only POST method is allowed
|
||||
authLevel: 'anonymous',
|
||||
handler: async (request, context) => {
|
||||
try {
|
||||
await client.connect();
|
||||
const db = client.db(DATABASE_NAME);
|
||||
const collection = db.collection(COLLECTION);
|
||||
|
||||
// Extracting data from the POST request body instead of query parameters
|
||||
const body = await request.json() || {};
|
||||
const page = body.page || 1;
|
||||
const size = body.size || 10;
|
||||
const original_price = body["Original Price"];
|
||||
const title = body["Title"];
|
||||
const developer = body.Developer;
|
||||
let query_conditions = {};
|
||||
if (original_price) {
|
||||
query_conditions["Original Price"] = { $regex: original_price, $options: "i" };
|
||||
}
|
||||
if (title) {
|
||||
query_conditions["Title"] = { $regex: title, $options: "i" };
|
||||
}
|
||||
if (developer) {
|
||||
query_conditions["Developer"] = { $regex: developer, $options: "i" };
|
||||
}
|
||||
|
||||
const data = await collection.find(query_conditions).skip((page - 1) * size).limit(size).toArray();
|
||||
const total = await collection.countDocuments(query_conditions);
|
||||
context.res = {
|
||||
status: 200,
|
||||
body: JSON.stringify({ code: 200, total: total, data: data, msg: "SUCCESS",body:query_conditions }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
} catch (e) {
|
||||
context.res = {
|
||||
status: 500,
|
||||
body: JSON.stringify({ code: 500, msg: e.message }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
} finally {
|
||||
if (client) {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
return context.res;
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
const { app } = require('@azure/functions');
|
||||
|
||||
const { MongoClient } = require('mongodb');
|
||||
// import jwt
|
||||
const jwt = require('jsonwebtoken');
|
||||
var connectionString = "mongodb://games:oQ5bO7YMfpu99oZMpKs0fjjypybyIMwBHJh7TmK8FYj1J41StnByDp1vxZ0huSXxlYbNLiRtNdvZACDb9cByMQ%3D%3D@games.mongo.cosmos.azure.com:10255/?ssl=true&retrywrites=false&maxIdleTimeMS=120000&appName=@games@";
|
||||
const client = new MongoClient(connectionString);
|
||||
|
||||
app.http('register', {
|
||||
methods: ['POST'],
|
||||
authLevel: 'anonymous',
|
||||
handler: async (request, context) => {
|
||||
try {
|
||||
await client.connect();
|
||||
const db = client.db("games");
|
||||
const { username, password } = await request.json();
|
||||
const existingUser = await db.collection("user").findOne({ username });
|
||||
|
||||
if (!existingUser) {
|
||||
await db.collection("user").insertOne({ username, password });
|
||||
context.res = {
|
||||
status: 200, // HTTP 状态码
|
||||
body: JSON.stringify({ code: 200, msg: "SUCCESS", data: { /* 返回的数据 */ } }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
} else {
|
||||
context.res = {
|
||||
status: 400, // 或其他合适的状态码
|
||||
body: JSON.stringify({ code: 400, msg: "Login or Registration failed" })
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
context.res = {
|
||||
status: 500,
|
||||
body: JSON.stringify({ code: 500, msg: e.message }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
} finally {
|
||||
if (client) {
|
||||
client.close();
|
||||
}
|
||||
return context.res;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.http('login', {
|
||||
methods: ['POST'],
|
||||
authLevel: 'anonymous',
|
||||
handler: async (request, context) => {
|
||||
try {
|
||||
const { username, password } = await request.json();
|
||||
await client.connect();
|
||||
const db = client.db("games");
|
||||
const user = await db.collection("user").findOne({ username, password });
|
||||
|
||||
if (user) {
|
||||
const token = jwt.sign({ username }, "123456" ,{ expiresIn: '1h' });
|
||||
context.res = {
|
||||
status: 200, // HTTP 状态码
|
||||
body: JSON.stringify({ code: 200, msg: "SUCCESS", data: { token } }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
};
|
||||
|
||||
} else {
|
||||
context.res = {
|
||||
status: 400, // 或其他合适的状态码
|
||||
body: JSON.stringify({ code: 400, msg: "Login or Registration failed" })
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
context.res = {
|
||||
status: 500,
|
||||
body: JSON.stringify({ code: 500, msg: e.message }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
}
|
||||
} finally {
|
||||
if (client) {
|
||||
client.close();
|
||||
}
|
||||
return context.res;
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user