Refine project structure

This commit is contained in:
Peng-YM 2022-05-25 14:33:46 +08:00
parent bbe29a7fac
commit 6169ff5255
10 changed files with 86 additions and 88 deletions

View File

@ -1,4 +1,5 @@
{
"ignorePatterns": ["*.min.js", "src/vendor/*.js"],
"env": {
"browser": true,
"es2021": true

View File

@ -1,4 +1,4 @@
import { OpenAPI } from '../utils/open-api';
import { OpenAPI } from '../vendor/open-api';
const $ = new OpenAPI('sub-store');
export default $;

View File

@ -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);

View File

@ -1,4 +1,4 @@
import { HTTP } from './open-api';
import { HTTP } from '../vendor/open-api';
const cache = new Map();

View File

@ -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);
}

View File

@ -1,4 +1,4 @@
import { HTTP } from './open-api';
import { HTTP } from '../vendor/open-api';
/**
* Gist backup

View 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);
}

View File

@ -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,8 +211,9 @@ export default function express({ port } = { port: 3000 }) {
}
})();
}
}
function patternMatched(pattern, path) {
function patternMatched(pattern, path) {
if (pattern instanceof RegExp && pattern.test(path)) {
return true;
} else {
@ -235,9 +235,9 @@ export default function express({ port } = { port: 3000 }) {
}
}
return false;
}
}
function extractURL(url) {
function extractURL(url) {
// extract path
const match = url.match(/https?:\/\/[^/]+(\/[^?]*)/) || [];
const path = match[1] || '/';
@ -256,9 +256,9 @@ export default function express({ port } = { port: 3000 }) {
path,
query,
};
}
}
function extractPathParams(pattern, path) {
function extractPathParams(pattern, path) {
if (pattern.indexOf(':') === -1) {
return null;
} else {
@ -282,5 +282,4 @@ export default function express({ port } = { port: 3000 }) {
}
return params;
}
}
}

File diff suppressed because one or more lines are too long