Feat: CORS Headers

API 增加 CORS 头
This commit is contained in:
奇趣保罗 2022-04-16 23:50:57 +08:00
parent 0109edb2a5
commit 99c196afad
7 changed files with 30 additions and 10 deletions

View File

@ -11,6 +11,10 @@ export default Api(
async () => {
const ctx = useContext();
// 设置 Header
ctx.set("access-control-allow-origin", "*");
ctx.set("access-control-allow-headers", "x-requested-with");
// 增加使用数量
await client.incr("api-next:stat:acgm");

View File

@ -9,17 +9,23 @@ export default Api(
async () => {
const ctx = useContext();
// 设置 Header
ctx.set("access-control-allow-origin", "*");
ctx.set("access-control-allow-headers", "x-requested-with");
ctx.set("cache-control", "max-age=604800");
// 增加使用数量
await client.incr("api-next:stat:bing");
const cache = await client.lRange("api-next:bing", 0, 2);
// 尝试使用缓存
const cached = await client.lRange("api-next:bing", 0, 2);
let image;
if (cache.length) {
if (cached.length) {
image = {
url: cache[0],
copyright: cache[1],
url: cached[0],
copyright: cached[1],
};
}
else {
@ -60,7 +66,7 @@ export default Api(
code: 1,
msg: "Success",
data: image,
cached: cache.length ? true : false
cached: cached.length ? true : false
};
}
);

View File

@ -10,6 +10,10 @@ export default Api(
async () => {
const ctx = useContext();
// 设置 Header
ctx.set("access-control-allow-origin", "*");
ctx.set("access-control-allow-headers", "x-requested-with");
// 增加使用数量
await client.incr("api-next:stat:netease");
@ -30,6 +34,9 @@ export default Api(
return "";
}
// 成功获取,设置缓存头
ctx.set("cache-control", "max-age=604800");
// 尝试使用缓存
const cached = await client.lRange(`api-next:163:${id}`, 0, 7);

View File

@ -3,6 +3,7 @@ import React, { useEffect, useRef } from "react";
// UI
import { Link } from "react-router-dom";
import prism from "prismjs";
import ArticleHead from "@/components/Layout/ArticleHead";
@ -55,7 +56,7 @@ function ACGM() {
<article className="post">
<h3>使</h3>
<p> <code>https://api.paugram.com/api/acgm</code>,即可获得一段歌曲 <code>JSON</code> 信息。</p>
<p>使 API <a href="https://api.paugram.com/notice"></a></p>
<p>使 API <Link to="/notice"></Link></p>
<p> API </p>
<p>使 <code>Cookie</code> </p>

View File

@ -3,6 +3,7 @@ import React, { useEffect } from "react";
// UI
import { Link } from "react-router-dom";
import prism from "prismjs";
import ArticleHead from "@/components/Layout/ArticleHead";
@ -19,7 +20,7 @@ function Bing() {
<article className="post">
<h3>使</h3>
<p> <code>https://api.paugram.com/api/bing</code> 可自动跳转到对应的壁纸</p>
<p>使 API <a href="https://api.paugram.com/notice"></a></p>
<p>使 API <Link to="/notice"></Link></p>
<p> API </p>
<h3></h3>

View File

@ -3,6 +3,7 @@ import React, { useEffect } from "react";
// UI
import { Link } from "react-router-dom";
import prism from "prismjs";
import ArticleHead from "@/components/Layout/ArticleHead";
@ -19,7 +20,7 @@ function Netease() {
<article className="post">
<h3>使</h3>
<p> <code>https://api.paugram.com/netease</code> 并输入参数 <code>id</code> 或 <code>title</code>,即可获得一段歌曲 <code>JSON</code> 信息。</p>
<p>使 API <a href="https://api.paugram.com/notice"></a></p>
<p>使 API <Link to="/notice"></Link></p>
<p> API </p>
<h3></h3>

View File

@ -3,8 +3,8 @@ import React, { useEffect } from "react";
// UI
import { Link } from "react-router-dom";
import prism from "prismjs";
import ArticleHead from "@/components/Layout/ArticleHead";
import HelpExample from "../images/help/wallpaper-pr-example.jpg";
@ -42,7 +42,7 @@ function Wallpaper() {
<h3>使</h3>
<p> <code>https://api.paugram.com/api/wallpaper</code> 可自动跳转到对应的壁纸(由于运营成本限制,目前本 API 借助 图床/CDN 等平台托管图片。欢迎给予资源赞助~</p>
<p>使 API <a href="https://api.paugram.com/notice"></a></p>
<p>使 API <Link to="/notice"></Link></p>
<p> API </p>
<h3></h3>