CloudFlare 出了个网页功能,于是便计划从 GitLab Pages 迁移到 CloudFlare Pages。主要是因为本站的 301 跳转以及 HSTS 都是通过 CloudFlare 来实现的,这样也就少了回源的开销。
功能方面类似 Netlify,构建的功能很少,只能填一条命令,还会自动安装一大堆不需要的软件,功能上也远少于 GitLab CI。不过有需要可以先用其他的 CI 构建好,再部署到 CloudFlare Pages。
当然,也有一些不错的功能,比如预览部署,每一次部署都有一个域名用于预览,这样就可以很方便地 review pull requests,还可以用 CloudFlare Access 管理预览部署的访问权限。
还有一个很坑的特性,会把链接末尾的 .html 通过 308 跳转掉,我只好修改 permalink 了。
---------------------------
cloudflare pages(静态网站服务)
https://pages.cloudflare.com/
demo site: https://zhao-v1z.pages.dev/
---------------------------------------------------------------------------------------
使用 Cloudflare Pages 进行反向代理
注意:域名中不要加https,http等字符。
export default {
async fetch(request, env) {
let url = new URL(request.url);
if (url.pathname.startsWith('/')) {
url.hostname = 'www.hicairo.com'
let new_request = new Request(url, request);
return fetch(new_request);
}
return env.ASSETS.fetch(request);
},
};1234567891011本文链接:https://www.hicairo.com/post/59.html
------------------------------------------------------
一个使用 Cloudflare Pages 创建的 URL 缩短器。A URL Shortener created using Cloudflare Pages.
一个使用 Cloudflare Pages 创建的 URL 缩短器
Demo : https://d.131213.xyz/
利用Cloudflare pages部署
- fork本项目
- 登录到Cloudflare控制台.
- 在帐户主页中,选择
pages>Create a project>Connect to Git - 选择你创建的项目存储库,在
Set up builds and deployments部分中,全部默认即可。 - 点击
Save and Deploy,稍等片刻,你的网站就部署好了。 - 创建D1数据库参考这里
- 执行sql命令创建表(在控制台输入框粘贴下面语句执行即可)
DROP TABLE IF EXISTS links;
CREATE TABLE IF NOT EXISTS links (
`id` integer PRIMARY KEY NOT NULL,
`url` text,
`slug` text,
`ua` text,
`ip` text,
`status` int,
`create_time` DATE
);
DROP TABLE IF EXISTS logs;
CREATE TABLE IF NOT EXISTS logs (
`id` integer PRIMARY KEY NOT NULL,
`url` text ,
`slug` text,
`referer` text,
`ua` text ,
`ip` text ,
`create_time` DATE
);
-
选择部署完成short项目,前往后台依次点击
设置->函数->D1 数据库绑定->编辑绑定->变量名称填写:DB命名空间选择你提前创建好的D1数据库绑定 -
重新部署项目,完成。
API
短链生成
# POST /create curl -X POST -H "Content-Type: application/json" -d '{"url":"https://131213.xyz"}' https://d.131213.xyz/create # 指定slug curl -X POST -H "Content-Type: application/json" -d '{"url":"https://131213.xyz","slug":"scxs"}' https://d.131213.xyz/create
response:
{
"slug": "<slug>",
"link": "http://d.131213.xyz/<slug>"
}from https://github.com/x-dr/short
(https://github.com/luckypoem/short-by-Cloudflare-Pages)
example: https://d.131213.xyz/lnzv















No comments:
Post a Comment