紧急修复BUG

This commit is contained in:
Peng-YM 2020-11-24 22:11:05 +08:00
parent ff4790f12e
commit 22c8483f17
3 changed files with 23 additions and 42 deletions

View File

@ -13,11 +13,11 @@
const $ = API("sub-store"); const $ = API("sub-store");
const Base64 = new Base64Code(); const Base64 = new Base64Code();
startService(); service();
/****************************************** Service **********************************************************/ /****************************************** Service **********************************************************/
function startService() { function service() {
const welcome = heredoc(function () {/* const welcome = heredoc(function () {/*
_____ __ _____ __ _____ __ _____ __
/ ___/ __ __ / /_ / ___/ / /_ ____ _____ ___ / ___/ __ __ / /_ / ___/ / /_ ____ _____ ___
@ -27,7 +27,7 @@ function startService() {
*/ */
}); });
console.log(welcome); console.log(welcome);
const $app = express(); const $app = express({debug: true});
// Constants // Constants
const SETTINGS_KEY = "settings"; const SETTINGS_KEY = "settings";
const SUBS_KEY = "subs"; const SUBS_KEY = "subs";
@ -38,8 +38,8 @@ function startService() {
if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY); if (!$.read(SETTINGS_KEY)) $.write({}, SETTINGS_KEY);
// download // download
$app.get("/download/:name", downloadSubscription);
$app.get("/download/collection/:name", downloadCollection); $app.get("/download/collection/:name", downloadCollection);
$app.get("/download/:name", downloadSubscription);
// subscription API // subscription API
$app.route("/api/sub/:name") $app.route("/api/sub/:name")
@ -62,7 +62,7 @@ function startService() {
.post(createCollection); .post(createCollection);
// gist backup // gist backup
$app.get("/api/backup") $app.get("/api/backup");
$app.all("/", (req, res) => { $app.all("/", (req, res) => {
res.status(405).end(); res.status(405).end();
@ -103,6 +103,10 @@ function startService() {
}); });
} }
$app.all("/", (req, res) => {
res.send("Hello from sub-store, made with ❤️ by Peng-YM");
});
$app.start(); $app.start();
// subscriptions API // subscriptions API
@ -2947,8 +2951,8 @@ function Gist(backupKey, token) {
* Mini Express Framework * Mini Express Framework
* https://github.com/Peng-YM/QuanX/blob/master/Tools/OpenAPI/Express.js * https://github.com/Peng-YM/QuanX/blob/master/Tools/OpenAPI/Express.js
*/ */
function express({port, debug} = {port: 3000, debug: false}) { function express(port = 3000) {
const {isNode} = debug ? false : ENV(); const {isNode} = ENV();
const DEFAULT_HEADERS = { const DEFAULT_HEADERS = {
"Content-Type": "text/plain;charset=UTF-8", "Content-Type": "text/plain;charset=UTF-8",
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
@ -2997,36 +3001,25 @@ function express({port, debug} = {port: 3000, debug: false}) {
// dispatch url to route // dispatch url to route
const dispatch = (request, start = 0) => { const dispatch = (request, start = 0) => {
let {method, url, headers, body} = request; let {method, url, headers, body} = request;
if (debug) {
console.log("=================== Dispatching Request ===============================");
console.log(JSON.stringify(request, null, 2));
}
if (/json/i.test(headers["Content-Type"])) { if (/json/i.test(headers["Content-Type"])) {
body = JSON.parse(body); body = JSON.parse(body);
} }
method = method.toUpperCase(); method = method.toUpperCase();
const {path, query} = extractURL(url); const {path, query} = extractURL(url);
let handler = null;
let i; let i;
// path matching
let handler = null;
let longestMatchedPattern = 0;
for (i = start; i < handlers.length; i++) { for (i = start; i < handlers.length; i++) {
if (handlers[i].method === "ALL" || method === handlers[i].method) { if (handlers[i].method === "ALL" || method === handlers[i].method) {
const {pattern} = handlers[i]; const {pattern} = handlers[i];
if (patternMatched(pattern, path)) { if (patternMatched(pattern, path)) {
if (pattern.split("/").length > longestMatchedPattern) {
handler = handlers[i]; handler = handlers[i];
longestMatchedPattern = pattern.split("/").length; break;
}
} }
} }
} }
if (handler) { if (handler) {
if (debug) {
console.log(`Pattern: ${handler.pattern} matched`);
}
// dispatch to next handler // dispatch to next handler
const next = () => { const next = () => {
dispatch(method, url, i); dispatch(method, url, i);
@ -3041,22 +3034,12 @@ function express({port, debug} = {port: 3000, debug: false}) {
body, body,
}; };
const res = Response(); const res = Response();
const cb = handler.callback; handler.callback(req, res, next).catch((err) => {
const onError = (err) => {
res.status(500).json({ res.status(500).json({
status: "failed", status: "failed",
message: `Internal Server Error: ${err}`, message: `Internal Server Error: ${err}`,
}); });
}; });
if (cb.constructor.name === "AsyncFunction") {
handler.callback(req, res, next).catch(onError);
} else {
try {
handler.callback(req, res, next);
} catch (err) {
onError(err);
}
}
} else { } else {
// no route, return 404 // no route, return 404
const res = Response(); const res = Response();
@ -3117,7 +3100,6 @@ function express({port, debug} = {port: 3000, debug: false}) {
307: "HTTP/1.1 307 Temporary Redirect", 307: "HTTP/1.1 307 Temporary Redirect",
308: "HTTP/1.1 308 Permanent Redirect", 308: "HTTP/1.1 308 Permanent Redirect",
404: "HTTP/1.1 404 Not Found", 404: "HTTP/1.1 404 Not Found",
405: "HTTP/1.1 405 Method Not Allowed",
500: "HTTP/1.1 500 Internal Server Error", 500: "HTTP/1.1 500 Internal Server Error",
}; };
return new (class { return new (class {
@ -3152,7 +3134,7 @@ function express({port, debug} = {port: 3000, debug: false}) {
json(data) { json(data) {
this.set("Content-Type", "application/json;charset=UTF-8"); this.set("Content-Type", "application/json;charset=UTF-8");
this.send(JSON.stringify(data, null, 2)); this.send(JSON.stringify(data));
} }
set(key, val) { set(key, val) {
@ -3234,7 +3216,6 @@ function express({port, debug} = {port: 3000, debug: false}) {
} }
} }
} }
/****************************************** Third Party Libraries **********************************************************/ /****************************************** Third Party Libraries **********************************************************/
function heredoc(fn) { function heredoc(fn) {
return fn.toString().split('\n').slice(1, -2).join('\n') + '\n'; return fn.toString().split('\n').slice(1, -2).join('\n') + '\n';

File diff suppressed because one or more lines are too long

View File

@ -6,10 +6,10 @@
grow grow
v-model="activeItem" v-model="activeItem"
> >
<v-btn :to="{path:'/dashboard'}" value="dashboard"> <!-- <v-btn :to="{path:'/dashboard'}" value="dashboard">-->
<span>首页</span> <!-- <span>首页</span>-->
<v-icon>speed</v-icon> <!-- <v-icon>speed</v-icon>-->
</v-btn> <!-- </v-btn>-->
<v-btn :to="{path: '/'}" value="subscription"> <v-btn :to="{path: '/'}" value="subscription">
<span>订阅</span> <span>订阅</span>