Skip to content

Commit

Permalink
Merge pull request #294 from caorushizi/dev-web
Browse files Browse the repository at this point in the history
feat: ✨  web
  • Loading branch information
caorushizi authored Sep 30, 2024
2 parents fc4ee51 + 0d740c6 commit 89bec2c
Show file tree
Hide file tree
Showing 39 changed files with 1,777 additions and 231 deletions.
5 changes: 4 additions & 1 deletion .cspell/custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ aplus
behaviour
bili
bilibili
bodyparser
bodys
cancle
cliqz
Expand All @@ -17,6 +18,8 @@ consola
conventionalcommits
datetime
DDTHH
deeplink
esmfile
execa
idtype
immer
Expand All @@ -29,6 +32,7 @@ mediago
metas
nocheck
optimizelegibility
outdir
pinia
Qrcode
redownload
Expand All @@ -53,4 +57,3 @@ watting
Watting
xgplayer
zustand
deeplink
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "module",
"scripts": {
"dev": "tsx scripts/dev.ts && pnpm --parallel -F \"./packages/*\" run dev",
"dev:web": "pnpm -F backend run mysql && pnpm --parallel -F \"./packages/*\" run start",
"dev:web": "pnpm -F backend run mysql && pnpm --parallel -F \"./packages/*\" run dev:web",
"build": "tsx scripts/build.ts",
"build:plugin": "pnpm -F plugin run build",
"build:main": "pnpm -F main run build",
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist
bin/Logs
release
types
log
bin
4 changes: 4 additions & 0 deletions packages/backend/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"watch": ["dist"],
"ext": "js"
}
18 changes: 12 additions & 6 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "backend",
"version": "0.1.0",
"description": "在线视频下载器",
"type": "commonjs",
"type": "module",
"main": "dist/index.js",
"scripts": {
"start": "nodemon dist/index.js",
"dev": "cross-env NODE_ENV=development gulp dev",
"dev:web": "cross-env NODE_ENV=development gulp dev",
"build": "cross-env NODE_ENV=production gulp build",
"mysql": "docker-compose up -d mysql",
"mysql:stop": "docker-compose stop mysql",
Expand All @@ -27,19 +27,20 @@
"@types/glob": "^8.1.0",
"@types/gulp": "^4.0.17",
"@types/koa": "^2.15.0",
"@types/koa-bodyparser": "^4.3.12",
"@types/koa__cors": "^5.0.0",
"@types/koa__router": "^12.0.4",
"@types/lodash": "^4.17.4",
"@types/mime-types": "^2.1.4",
"@types/node": "^20.12.12",
"@types/node-fetch": "^2.6.11",
"@types/semver": "^7.5.8",
"@types/serve-handler": "^6.1.4",
"@types/undertaker": "^1.2.11",
"consola": "^3.2.3",
"cross-env": "^7.0.3",
"del": "^7.1.0",
"dotenv": "^16.4.5",
"esbuild": "^0.21.4",
"esbuild": "0.21.4",
"esbuild-register": "^3.5.0",
"eslint": "^8.57.0",
"glob": "^10.4.1",
Expand All @@ -51,22 +52,27 @@
"typescript-eslint": "^7.10.0"
},
"dependencies": {
"@koa/cors": "^5.0.0",
"@koa/router": "^12.0.1",
"axios": "^1.7.2",
"cheerio": "1.0.0-rc.12",
"cors": "^2.8.5",
"dayjs": "^1.11.11",
"execa": "^9.1.0",
"fs-extra": "^11.2.0",
"i18next": "^23.11.5",
"inversify": "^6.0.2",
"koa": "^2.15.3",
"koa-bodyparser": "^4.4.1",
"lint-staged": "^15.2.5",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"mysql2": "^3.11.0",
"nanoid": "^5.0.7",
"node-pty": "^1.0.0",
"reflect-metadata": "^0.2.2",
"typeorm": "0.3.20"
"strip-ansi": "^7.1.0",
"typeorm": "0.3.20",
"winston": "^3.14.2"
},
"keywords": []
}
40 changes: 7 additions & 33 deletions packages/backend/scripts/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { Env, isDev, mainResolve } from "./utils";
import esbuild from "esbuild";

const external = [
"electron",
"nock",
"aws-sdk",
"mock-aws-s3",
"@cliqz/adblocker-electron-preload",
"node-pty",
];

function getConfig(): esbuild.BuildOptions {
export function buildOptions(): esbuild.BuildOptions {
const getDefine = (): Record<string, string> => {
if (isDev) {
return {
__bin__: `"${mainResolve("app/bin").replace(/\\/g, "\\\\")}"`,
__bin__: `"${mainResolve("bin").replace(/\\/g, "\\\\")}"`,
};
}

Expand All @@ -25,33 +16,16 @@ function getConfig(): esbuild.BuildOptions {
};

return {
entryPoints: [mainResolve("src/index.ts")],
bundle: true,
sourcemap: process.env.NODE_ENV === "development",
external,
define: getDefine(),
outdir: mainResolve("dist"),
loader: { ".png": "file" },
minify: process.env.NODE_ENV === "production",
packages: "external",
format: "esm",
target: ["node20"],
platform: "node",
};
}

function buildOptions(
entry: string,
platform: esbuild.Platform,
target: string,
): esbuild.BuildOptions {
return {
...getConfig(),
entryPoints: [mainResolve(entry)],
platform: platform,
target: [target],
};
}

export function browserOptions(entry: string): esbuild.BuildOptions {
return buildOptions(entry, "browser", "chrome89");
}

export function nodeOptions(entry: string): esbuild.BuildOptions {
return buildOptions(entry, "node", "node16.13");
}
10 changes: 5 additions & 5 deletions packages/backend/scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { mainResolve, Env } from "./utils";
import gulp from "gulp";
import * as esbuild from "esbuild";
import consola from "consola";
import { nodeOptions } from "./config";
import { buildOptions } from "./config";
// import fs from "fs";

const env = Env.getInstance();
env.loadDotEnvRuntime();

async function clean() {
return deleteSync([mainResolve("app/dist")]);
return deleteSync([mainResolve("dist")]);
}

// async function copyBin() {
Expand All @@ -22,7 +22,7 @@ async function clean() {
// const copy = gulp.parallel(copyBin);

async function watchTask() {
const main = await esbuild.context(nodeOptions("src/index.ts"));
const main = await esbuild.context(buildOptions());

const watcher = gulp.watch(["./src"]);
watcher
Expand All @@ -39,11 +39,11 @@ async function watchTask() {
}

async function buildTask() {
await esbuild.build(nodeOptions("src/index.ts"));
await esbuild.build(buildOptions());
}

// 开发环境
// TODO 暂时不拷贝 bin 文件夹
export const dev = gulp.series(watchTask);
export const dev = gulp.series(clean, watchTask);
// 构建打包
export const build = gulp.series(clean, buildTask);
9 changes: 8 additions & 1 deletion packages/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { TYPES } from "./types.ts";
import TypeORM from "./vendor/TypeORM.ts";
import RouterHandlerService from "./core/router.ts";
import Koa from "koa";
import cors from "@koa/cors";
import bodyParser from "koa-bodyparser";
import Logger from "./vendor/Logger.ts";

@injectable()
export default class ElectronApp extends Koa {
Expand All @@ -11,6 +14,8 @@ export default class ElectronApp extends Koa {
private readonly router: RouterHandlerService,
@inject(TYPES.TypeORM)
private readonly db: TypeORM,
@inject(TYPES.Logger)
private readonly logger: Logger,
) {
super();
}
Expand All @@ -25,10 +30,12 @@ export default class ElectronApp extends Koa {
// vendor
await this.vendorInit();

this.use(cors());
this.use(bodyParser());
this.use(this.router.routes()).use(this.router.allowedMethods());

this.listen(3000, () => {
console.log("Server running on port 3000");
this.logger.info("Server running on port 3000");
});
}
}
1 change: 1 addition & 0 deletions packages/backend/src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_PREFIX = "/api";
81 changes: 81 additions & 0 deletions packages/backend/src/controller/DownloadController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { inject, injectable } from "inversify";
import {
DownloadItem,
DownloadItemPagination,
DownloadStatus,
Task,
type Controller,
} from "../interfaces.ts";
import { TYPES } from "../types.ts";
import FavoriteRepository from "../repository/FavoriteRepository.ts";
import { get, post } from "../helper/index.ts";
import Logger from "../vendor/Logger.ts";
import VideoRepository from "../repository/VideoRepository.ts";
import { Context } from "koa";
import ConfigService from "../services/ConfigService.ts";
import DownloadService from "../services/DownloadService.ts";

@injectable()
export default class DownloadController implements Controller {
constructor(
@inject(TYPES.FavoriteRepository)
private readonly favoriteRepository: FavoriteRepository,
@inject(TYPES.Logger)
private readonly logger: Logger,
@inject(TYPES.VideoRepository)
private readonly videoRepository: VideoRepository,
@inject(TYPES.ConfigService)
private readonly store: ConfigService,
@inject(TYPES.DownloadService)
private readonly downloadService: DownloadService,
) {}

@get("/")
async getFavorites() {
return false;
}

@post("add-download-item")
async addDownloadItem(ctx: Context) {
const video = ctx.request.body as DownloadItem;
const item = await this.videoRepository.addVideo(video);
return item;
}

@post("add-download-items")
async addDownloadItems(ctx: Context) {
const videos = ctx.request.body as DownloadItem[];
const items = await this.videoRepository.addVideos(videos);
return items;
}

@post("get-download-items")
async getDownloadItems(ctx: Context) {
const pagination = ctx.request.body as DownloadItemPagination;
const videos = await this.videoRepository.findVideos(pagination);
return videos;
}

@post("start-download")
async startDownload(ctx: Context) {
const { vid } = ctx.request.body as { vid: number };
// 查找将要下载的视频
const video = await this.videoRepository.findVideo(vid);
const { name, url, headers, type } = video;
const { local, deleteSegments } = await this.store.getConfig();

const task: Task = {
id: vid,
params: {
url,
type,
local,
name,
headers,
deleteSegments,
},
};
await this.videoRepository.changeVideoStatus(vid, DownloadStatus.Watting);
this.downloadService.addTask(task);
}
}
25 changes: 23 additions & 2 deletions packages/backend/src/controller/HomeController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import { inject, injectable } from "inversify";
import { type Controller } from "../interfaces.ts";
import { TYPES } from "../types.ts";
import { ConfigParams, TYPES } from "../types.ts";
import FavoriteRepository from "../repository/FavoriteRepository.ts";
import { get } from "../helper/decorator.ts";
import { get, post } from "../helper/index.ts";
import Logger from "../vendor/Logger.ts";
import ConfigService from "../services/ConfigService.ts";
import { Context } from "koa";

@injectable()
export default class HomeController implements Controller {
constructor(
@inject(TYPES.FavoriteRepository)
private readonly favoriteRepository: FavoriteRepository,
@inject(TYPES.Logger)
private readonly logger: Logger,
@inject(TYPES.ConfigService)
private readonly config: ConfigService,
) {}

@get("/")
async getFavorites() {
return false;
}

@post("get-app-store")
async getAppStore() {
const store = await this.config.getConfig();
return store;
}

@post("set-app-store")
async setAppStore(ctx: Context) {
const params = ctx.request.body as ConfigParams;
this.config.setConfig(params);
this.logger.info("set app store");
return false;
}
}
Loading

0 comments on commit 89bec2c

Please sign in to comment.