mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-02-15 12:24:44 +08:00
Update
This commit is contained in:
28
backend/src/index.docker.ts
Normal file
28
backend/src/index.docker.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import express, { Express, Request, Response } from "express";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app: Express = express();
|
||||
const port = process.env.PORT || 3001;
|
||||
|
||||
// Middlewares
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Health check route
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
res.send("MyTV Backend Service is running!");
|
||||
});
|
||||
|
||||
import apiRouter from "./routes";
|
||||
|
||||
// API routes
|
||||
app.use("/api", apiRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is running on port ${port}`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -21,8 +21,4 @@ import apiRouter from "./routes";
|
||||
// API routes
|
||||
app.use("/api", apiRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`[server]: Server is running at http://localhost:${port}`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -2,8 +2,6 @@ import { Router } from "express";
|
||||
import searchRouter from "./search";
|
||||
import detailRouter from "./detail";
|
||||
import doubanRouter from "./douban";
|
||||
import loginRouter from "./login";
|
||||
import playRecordsRouter from "./playrecords";
|
||||
import imageProxyRouter from "./image-proxy";
|
||||
|
||||
const router = Router();
|
||||
@@ -11,8 +9,6 @@ const router = Router();
|
||||
router.use("/search", searchRouter);
|
||||
router.use("/detail", detailRouter);
|
||||
router.use("/douban", doubanRouter);
|
||||
router.use("/login", loginRouter);
|
||||
router.use("/playrecords", playRecordsRouter);
|
||||
router.use("/image-proxy", imageProxyRouter);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const masterPassword = process.env.PASSWORD;
|
||||
|
||||
// If no password is set in the environment, allow access.
|
||||
if (!masterPassword) {
|
||||
return res.json({ ok: true });
|
||||
}
|
||||
|
||||
const { password } = req.body;
|
||||
if (typeof password !== "string") {
|
||||
return res.status(400).json({ error: "密码不能为空" });
|
||||
}
|
||||
|
||||
const matched = password === masterPassword;
|
||||
|
||||
if (!matched) {
|
||||
return res.status(401).json({ ok: false, error: "密码错误" });
|
||||
}
|
||||
|
||||
return res.json({ ok: true });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ error: "服务器错误" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,49 +0,0 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { getAllPlayRecords, savePlayRecord } from "../services/db";
|
||||
import { PlayRecord } from "../types";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// GET all play records
|
||||
router.get("/", async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const records = await getAllPlayRecords();
|
||||
res.json(records);
|
||||
} catch (err) {
|
||||
console.error("获取播放记录失败", err);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
|
||||
// POST a new play record
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { key, record }: { key: string; record: PlayRecord } = req.body;
|
||||
|
||||
if (!key || !record) {
|
||||
return res.status(400).json({ error: "Missing key or record" });
|
||||
}
|
||||
|
||||
// Basic validation
|
||||
if (!record.title || !record.source_name || record.index < 0) {
|
||||
return res.status(400).json({ error: "Invalid record data" });
|
||||
}
|
||||
|
||||
const [source, id] = key.split("+");
|
||||
if (!source || !id) {
|
||||
return res.status(400).json({ error: "Invalid key format" });
|
||||
}
|
||||
|
||||
// The user_id will be stripped and re-added in the service to ensure it's always 0
|
||||
const recordToSave: Omit<PlayRecord, "user_id"> = record;
|
||||
|
||||
await savePlayRecord(source, id, recordToSave);
|
||||
|
||||
res.status(201).json({ success: true });
|
||||
} catch (err) {
|
||||
console.error("保存播放记录失败", err);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,73 +0,0 @@
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
import { PlayRecord } from "../types";
|
||||
|
||||
const DATA_DIR = path.join(process.cwd(), "data");
|
||||
const PLAY_RECORDS_FILE = path.join(DATA_DIR, "playrecords.json");
|
||||
|
||||
type DbData = {
|
||||
[key: string]: PlayRecord;
|
||||
};
|
||||
|
||||
// Ensure data directory exists
|
||||
async function ensureDataDir(): Promise<void> {
|
||||
try {
|
||||
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||
} catch (error) {
|
||||
console.error("Error creating data directory:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Read the entire DB file
|
||||
async function readDb(): Promise<DbData> {
|
||||
await ensureDataDir();
|
||||
try {
|
||||
const data = await fs.readFile(PLAY_RECORDS_FILE, "utf-8");
|
||||
return JSON.parse(data) as DbData;
|
||||
} catch (error: any) {
|
||||
// If file does not exist, return empty object
|
||||
if (error.code === "ENOENT") {
|
||||
return {};
|
||||
}
|
||||
console.error("Error reading database file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Write the entire DB file
|
||||
async function writeDb(data: DbData): Promise<void> {
|
||||
await ensureDataDir();
|
||||
try {
|
||||
await fs.writeFile(
|
||||
PLAY_RECORDS_FILE,
|
||||
JSON.stringify(data, null, 2),
|
||||
"utf-8"
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error writing to database file:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Public DB Methods ---
|
||||
|
||||
export function generateStorageKey(source: string, id: string): string {
|
||||
return `${source}+${id}`;
|
||||
}
|
||||
|
||||
export async function getAllPlayRecords(): Promise<DbData> {
|
||||
return readDb();
|
||||
}
|
||||
|
||||
export async function savePlayRecord(
|
||||
source: string,
|
||||
id: string,
|
||||
record: Omit<PlayRecord, "user_id">
|
||||
): Promise<void> {
|
||||
const db = await readDb();
|
||||
const key = generateStorageKey(source, id);
|
||||
const fullRecord: PlayRecord = { ...record, user_id: 0 }; // user_id is always 0 for now
|
||||
db[key] = fullRecord;
|
||||
await writeDb(db);
|
||||
}
|
||||
Reference in New Issue
Block a user