mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-11 22:18:59 +08:00
Refine project structure
This commit is contained in:
parent
bbe29a7fac
commit
6169ff5255
@ -1,4 +1,5 @@
|
||||
{
|
||||
"ignorePatterns": ["*.min.js", "src/vendor/*.js"],
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { OpenAPI } from '../utils/open-api';
|
||||
import { OpenAPI } from '../vendor/open-api';
|
||||
|
||||
const $ = new OpenAPI('sub-store');
|
||||
export default $;
|
||||
|
@ -3,9 +3,9 @@ import {
|
||||
GIST_BACKUP_KEY,
|
||||
GIST_BACKUP_FILE_NAME,
|
||||
} from './constants';
|
||||
import { ENV } from '../utils/open-api';
|
||||
import express from '../utils/express';
|
||||
import { IP_API } from '../utils/geo';
|
||||
import { ENV } from '../vendor/open-api';
|
||||
import express from '../vendor/express';
|
||||
import IP_API from '../utils/ip-api';
|
||||
import Gist from '../utils/gist';
|
||||
import $ from '../core/app';
|
||||
|
||||
@ -15,7 +15,7 @@ import registerArtifactRoutes from './artifacts';
|
||||
import registerSettingRoutes from './settings';
|
||||
|
||||
export default function serve() {
|
||||
const $app = express();
|
||||
const $app = express({ substore: $ });
|
||||
|
||||
// register routes
|
||||
registerCollectionRoutes($app);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HTTP } from './open-api';
|
||||
import { HTTP } from '../vendor/open-api';
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
import { HTTP } from './open-api';
|
||||
|
||||
// get proxy flag according to its name
|
||||
export function getFlag(name) {
|
||||
// flags from @KOP-XIAO: https://github.com/KOP-XIAO/QuantumultX/blob/master/Scripts/resource-parser.js
|
||||
@ -307,13 +305,3 @@ export function getFlag(name) {
|
||||
) || [])[0];
|
||||
return oldFlag || '🏴☠️';
|
||||
}
|
||||
|
||||
// util API
|
||||
export async function IP_API(req, res) {
|
||||
const server = decodeURIComponent(req.params.server);
|
||||
const $http = HTTP();
|
||||
const result = await $http
|
||||
.get(`http://ip-api.com/json/${server}?lang=zh-CN`)
|
||||
.then((resp) => JSON.parse(resp.body));
|
||||
res.json(result);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { HTTP } from './open-api';
|
||||
import { HTTP } from '../vendor/open-api';
|
||||
|
||||
/**
|
||||
* Gist backup
|
||||
|
10
backend/src/utils/ip-api.js
Normal file
10
backend/src/utils/ip-api.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { HTTP } from '../vendor/open-api';
|
||||
|
||||
export default async function IP_API(req, res) {
|
||||
const server = decodeURIComponent(req.params.server);
|
||||
const $http = HTTP();
|
||||
const result = await $http
|
||||
.get(`http://ip-api.com/json/${server}?lang=zh-CN`)
|
||||
.then((resp) => JSON.parse(resp.body));
|
||||
res.json(result);
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
/* eslint-disable no-undef */
|
||||
import { ENV } from './open-api';
|
||||
import $ from '../core/app';
|
||||
|
||||
export default function express({ port } = { port: 3000 }) {
|
||||
export default function express({ substore: $, port } = { port: 3000 }) {
|
||||
const { isNode } = ENV();
|
||||
const DEFAULT_HEADERS = {
|
||||
'Content-Type': 'text/plain;charset=UTF-8',
|
||||
@ -212,75 +211,75 @@ export default function express({ port } = { port: 3000 }) {
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
function patternMatched(pattern, path) {
|
||||
if (pattern instanceof RegExp && pattern.test(path)) {
|
||||
return true;
|
||||
} else {
|
||||
// root pattern, match all
|
||||
if (pattern === '/') return true;
|
||||
// normal string pattern
|
||||
if (pattern.indexOf(':') === -1) {
|
||||
const spath = path.split('/');
|
||||
const spattern = pattern.split('/');
|
||||
for (let i = 0; i < spattern.length; i++) {
|
||||
if (spath[i] !== spattern[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else if (extractPathParams(pattern, path)) {
|
||||
// string pattern with path parameters
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function extractURL(url) {
|
||||
// extract path
|
||||
const match = url.match(/https?:\/\/[^/]+(\/[^?]*)/) || [];
|
||||
const path = match[1] || '/';
|
||||
|
||||
// extract query string
|
||||
const split = url.indexOf('?');
|
||||
const query = {};
|
||||
if (split !== -1) {
|
||||
let hashes = url.slice(url.indexOf('?') + 1).split('&');
|
||||
for (let i = 0; i < hashes.length; i++) {
|
||||
const hash = hashes[i].split('=');
|
||||
query[hash[0]] = hash[1];
|
||||
}
|
||||
}
|
||||
return {
|
||||
path,
|
||||
query,
|
||||
};
|
||||
}
|
||||
|
||||
function extractPathParams(pattern, path) {
|
||||
function patternMatched(pattern, path) {
|
||||
if (pattern instanceof RegExp && pattern.test(path)) {
|
||||
return true;
|
||||
} else {
|
||||
// root pattern, match all
|
||||
if (pattern === '/') return true;
|
||||
// normal string pattern
|
||||
if (pattern.indexOf(':') === -1) {
|
||||
return null;
|
||||
} else {
|
||||
const params = {};
|
||||
for (let i = 0, j = 0; i < pattern.length; i++, j++) {
|
||||
if (pattern[i] === ':') {
|
||||
let key = [];
|
||||
let val = [];
|
||||
while (pattern[++i] !== '/' && i < pattern.length) {
|
||||
key.push(pattern[i]);
|
||||
}
|
||||
while (path[j] !== '/' && j < path.length) {
|
||||
val.push(path[j++]);
|
||||
}
|
||||
params[key.join('')] = val.join('');
|
||||
} else {
|
||||
if (pattern[i] !== path[j]) {
|
||||
return null;
|
||||
}
|
||||
const spath = path.split('/');
|
||||
const spattern = pattern.split('/');
|
||||
for (let i = 0; i < spattern.length; i++) {
|
||||
if (spath[i] !== spattern[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return params;
|
||||
return true;
|
||||
} else if (extractPathParams(pattern, path)) {
|
||||
// string pattern with path parameters
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function extractURL(url) {
|
||||
// extract path
|
||||
const match = url.match(/https?:\/\/[^/]+(\/[^?]*)/) || [];
|
||||
const path = match[1] || '/';
|
||||
|
||||
// extract query string
|
||||
const split = url.indexOf('?');
|
||||
const query = {};
|
||||
if (split !== -1) {
|
||||
let hashes = url.slice(url.indexOf('?') + 1).split('&');
|
||||
for (let i = 0; i < hashes.length; i++) {
|
||||
const hash = hashes[i].split('=');
|
||||
query[hash[0]] = hash[1];
|
||||
}
|
||||
}
|
||||
return {
|
||||
path,
|
||||
query,
|
||||
};
|
||||
}
|
||||
|
||||
function extractPathParams(pattern, path) {
|
||||
if (pattern.indexOf(':') === -1) {
|
||||
return null;
|
||||
} else {
|
||||
const params = {};
|
||||
for (let i = 0, j = 0; i < pattern.length; i++, j++) {
|
||||
if (pattern[i] === ':') {
|
||||
let key = [];
|
||||
let val = [];
|
||||
while (pattern[++i] !== '/' && i < pattern.length) {
|
||||
key.push(pattern[i]);
|
||||
}
|
||||
while (path[j] !== '/' && j < path.length) {
|
||||
val.push(path[j++]);
|
||||
}
|
||||
params[key.join('')] = val.join('');
|
||||
} else {
|
||||
if (pattern[i] !== path[j]) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
}
|
4
backend/sub-store.min.js
vendored
4
backend/sub-store.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user