From a03907d331db958a0194a7a371cbcab51c504ade Mon Sep 17 00:00:00 2001 From: Ou Date: Sat, 5 Oct 2024 23:55:30 +0800 Subject: [PATCH] pref: server --- server/cache.ts | 9 +++----- server/config.ts | 10 --------- server/routes/[id].ts | 39 +++++++++++----------------------- server/sources/36kr-quick.ts | 3 --- server/sources/36kr.ts | 5 ----- server/sources/fallback.ts | 33 ---------------------------- server/sources/index.ts | 14 +++++------- server/sources/peopledaily.ts | 3 --- server/sources/wallstreetcn.ts | 3 --- server/sources/weibo.ts | 2 -- server/sources/zaobao.ts | 2 -- server/utils/index.ts | 34 ++++++++++++++++++++++++++++- server/utils/logger.ts | 11 ++++++++++ 13 files changed, 64 insertions(+), 104 deletions(-) delete mode 100644 server/config.ts delete mode 100644 server/sources/36kr-quick.ts delete mode 100644 server/sources/36kr.ts delete mode 100644 server/sources/fallback.ts delete mode 100644 server/sources/peopledaily.ts delete mode 100644 server/sources/wallstreetcn.ts create mode 100644 server/utils/logger.ts diff --git a/server/cache.ts b/server/cache.ts index 8a4d135..8a4fecf 100644 --- a/server/cache.ts +++ b/server/cache.ts @@ -8,7 +8,6 @@ export class Cache { } async init() { - const last = performance.now() await this.db.prepare(` CREATE TABLE IF NOT EXISTS cache ( id TEXT PRIMARY KEY, @@ -16,20 +15,18 @@ export class Cache { data TEXT ); `).run() - console.log(`init: `, performance.now() - last) + logger.success(`init cache table`) } async set(key: string, value: NewsItem[]) { const now = Date.now() - const last = performance.now() await this.db.prepare( `INSERT OR REPLACE INTO cache (id, data, updated) VALUES (?, ?, ?)`, ).run(key, JSON.stringify(value), now) - console.log(`set ${key}: `, performance.now() - last) + logger.success(`set ${key} cache`) } async get(key: string): Promise { - const last = performance.now() const row: any = await this.db.prepare(`SELECT id, data, updated FROM cache WHERE id = ?`).get(key) const r = row ? { @@ -37,7 +34,7 @@ export class Cache { data: JSON.parse(row.data), } : undefined - console.log(`get ${key}: `, performance.now() - last) + logger.success(`get ${key} cache`) return r } diff --git a/server/config.ts b/server/config.ts deleted file mode 100644 index 521e1fe..0000000 --- a/server/config.ts +++ /dev/null @@ -1,10 +0,0 @@ -// 创建配置对象 -export const config = { - PORT: 6688, - DISALLOW_ROBOT: true, - CACHE_TTL: 3600, - REQUEST_TIMEOUT: 6000, - ALLOWED_DOMAIN: "*", - USE_LOG_FILE: true, - RSS_MODE: false, -} diff --git a/server/routes/[id].ts b/server/routes/[id].ts index 318ae85..e169558 100644 --- a/server/routes/[id].ts +++ b/server/routes/[id].ts @@ -1,7 +1,7 @@ import { Interval, TTL } from "@shared/consts" import type { SourceResponse } from "@shared/types" import { sources } from "@shared/data" -import { fallback, sourcesFn } from "#/sources" +import { sourcesFn } from "#/sources" import { Cache } from "#/cache" export default defineEventHandler(async (event): Promise => { @@ -10,7 +10,7 @@ export default defineEventHandler(async (event): Promise => { const query = getQuery(event) const latest = query.latest !== undefined && query.latest !== "false" - if (!id || !sources[id]) throw new Error("Invalid source id") + if (!id || !sources[id] || !sourcesFn[id]) throw new Error("Invalid source id") const db = useDatabase() const cacheStore = db ? new Cache(db) : undefined const now = Date.now() @@ -42,33 +42,18 @@ export default defineEventHandler(async (event): Promise => { } } - if (sourcesFn[id]) { - const last = performance.now() - const data = await sourcesFn[id]() - console.log(`fetch: ${id}`, performance.now() - last) - if (cacheStore) event.waitUntil(cacheStore.set(id, data)) - return { - status: "success", - data: { - updatedTime: now, - items: data, - }, - } - } else { - const last = performance.now() - const data = await fallback(id) - console.log(`fetch: ${id}`, performance.now() - last) - if (cacheStore) event.waitUntil(cacheStore.set(id, data)) - return { - status: "success", - data: { - updatedTime: now, - items: data, - }, - } + const data = await sourcesFn[id]() + logger.success(`fetch ${id} latest`) + if (cacheStore) event.waitUntil(cacheStore.set(id, data)) + return { + status: "success", + data: { + updatedTime: now, + items: data, + }, } } catch (e: any) { - console.error(e) + logger.error(e) return { status: "error", message: e.message ?? e, diff --git a/server/sources/36kr-quick.ts b/server/sources/36kr-quick.ts deleted file mode 100644 index 7ad63ca..0000000 --- a/server/sources/36kr-quick.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { defineRSSHubSource } from "#/utils" - -export default defineRSSHubSource("/36kr/newsflashes") diff --git a/server/sources/36kr.ts b/server/sources/36kr.ts deleted file mode 100644 index 782ad10..0000000 --- a/server/sources/36kr.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { defineRSSHubSource } from "#/utils" - -export default defineRSSHubSource("/36kr/hot-list", { - sorted: false, -}) diff --git a/server/sources/fallback.ts b/server/sources/fallback.ts deleted file mode 100644 index 8ba571e..0000000 --- a/server/sources/fallback.ts +++ /dev/null @@ -1,33 +0,0 @@ -import type { NewsItem } from "@shared/types" - -export interface Res { - code: number - message: string - name: string - title: string - subtitle: string - total: number - updateTime: string - data: { - title: string - desc: string - time?: string - url: string - mobileUrl: string - }[] -} - -export async function fallback(id: string): Promise { - const url = `https://smzdk.top/api/${id}/new` - const res: Res = await $fetch(url) - if (res.code !== 200 || !res.data) throw new Error(res.message) - return res.data.map(item => ({ - extra: { - date: item.time, - }, - id: item.url, - title: item.title, - url: item.url, - mobileUrl: item.mobileUrl, - })) -} diff --git a/server/sources/index.ts b/server/sources/index.ts index ff4d113..6529b9a 100644 --- a/server/sources/index.ts +++ b/server/sources/index.ts @@ -1,18 +1,14 @@ import type { NewsItem, SourceID } from "@shared/types" -import peopledaily from "./peopledaily" import weibo from "./weibo" import zaobao from "./zaobao" -import krQ from "./36kr-quick" -import wallstreetcn from "./wallstreetcn" -// import kr from "./36kr" - -export { fallback } from "./fallback" export const sourcesFn = { - peopledaily, + "peopledaily": defineRSSSource("https://feedx.net/rss/people.xml"), weibo, + "douyin": defineFallbackSource("douyin"), zaobao, - wallstreetcn, - "36kr-quick": krQ, + "toutiao": defineFallbackSource("toutiao"), + "wallstreetcn": defineRSSHubSource("/wallstreetcn/live"), + "36kr-quick": defineRSSHubSource("/36kr/newsflashes"), // "36kr": kr, } as Record Promise> diff --git a/server/sources/peopledaily.ts b/server/sources/peopledaily.ts deleted file mode 100644 index 378a8c1..0000000 --- a/server/sources/peopledaily.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { defineRSSSource } from "#/utils" - -export default defineRSSSource("https://feedx.net/rss/people.xml") diff --git a/server/sources/wallstreetcn.ts b/server/sources/wallstreetcn.ts deleted file mode 100644 index da04998..0000000 --- a/server/sources/wallstreetcn.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { defineRSSHubSource } from "#/utils" - -export default defineRSSHubSource("/wallstreetcn/live") diff --git a/server/sources/weibo.ts b/server/sources/weibo.ts index 58a7ddd..909896d 100644 --- a/server/sources/weibo.ts +++ b/server/sources/weibo.ts @@ -1,5 +1,3 @@ -import { defineSource } from "#/utils" - interface Res { ok: number // 1 is ok data: { diff --git a/server/sources/zaobao.ts b/server/sources/zaobao.ts index 60bddef..720a6e4 100644 --- a/server/sources/zaobao.ts +++ b/server/sources/zaobao.ts @@ -3,8 +3,6 @@ import * as cheerio from "cheerio" import iconv from "iconv-lite" import type { NewsItem } from "@shared/types" import { $fetch } from "ofetch" -import { tranformToUTC } from "#/utils/date" -import { defineSource } from "#/utils" export default defineSource(async () => { const response: ArrayBuffer = await $fetch("https://www.kzaobao.com/top.html", { diff --git a/server/utils/index.ts b/server/utils/index.ts index d2399e3..6bf5806 100644 --- a/server/utils/index.ts +++ b/server/utils/index.ts @@ -1,10 +1,42 @@ import { RSSHubBase } from "@shared/consts" -import type { NewsItem, RSSHubInfo } from "@shared/types" +import type { NewsItem, RSSHubInfo, SourceID } from "@shared/types" export function defineSource(source: () => Promise): () => Promise { return source } +interface FallbackRes { + code: number + message: string + name: string + title: string + subtitle: string + total: number + updateTime: string + data: { + title: string + desc: string + time?: string + url: string + mobileUrl: string + }[] +} +export function defineFallbackSource(id: SourceID): () => Promise { + return async () => { + const url = `https://smzdk.top/api/${id}/new` + const res: FallbackRes = await $fetch(url) + if (res.code !== 200 || !res.data) throw new Error(res.message) + return res.data.map(item => ({ + extra: { + date: item.time, + }, + id: item.url, + title: item.title, + url: item.url, + mobileUrl: item.mobileUrl, + })) + } +} export function defineRSSSource(url: string): () => Promise { return async () => { const data = await rss2json(url) diff --git a/server/utils/logger.ts b/server/utils/logger.ts new file mode 100644 index 0000000..4466415 --- /dev/null +++ b/server/utils/logger.ts @@ -0,0 +1,11 @@ +import { createConsola } from "consola" + +export const logger = createConsola({ + level: 4, + formatOptions: { + columns: 80, + colors: true, + compact: false, + date: true, + }, +})