隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。 隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。 ——《万叶集》
前言
很多前端框架都会加载 Google Fonts 字体导致在国内打开页面速度缓慢。而 Google Fonts 中文版使用的镜像 fonts.font.im主机也并不在国内,速度也十分不理想。同时,这段时间在学习 GO 并准备做些啥练练手,于是便有了 GoMyFonts 这个简单的小工具。
项目介绍
GoMyFonts 是一个用于搭建 Google Fonts 镜像的小工具,项目可一键部署并支持自定义与本地缓存功能。
GitHub 仓库:https://github.com/MoeMion/GoMyFonts
环境需求
- 一台能快速访问到 Google Fonts 的主机
- Golang 1.15+
- 可选配置 Nginx、Caddy 等前置代理
下载与编译
Release 中已经包含有预编译的可执行文件。当以上文件不可运行时,您可以选择按照以下方法自行编译,相关的依赖会自动解决。
# 将项目克隆至本地
git clone https://github.com/MoeMion/GoMyFonts.git
cd ./GoMyFonts/
# 使用 Go 编译为可执行文件并运行
go build
chmod +x ./GoMyFonts
./Go MyFonts
# 或者使用 Go Run 直接运行(不推荐)
go run
使用举例
使用./GoMyFonts -help
指令可以查看 GoMyFonts 所有支持的参数:
./gomyfonts -help
Usage: gomyfonts [-h] [-p :port] [-l link] [-t title] [-c timeout of cache]
Github: https://github.com/MoeMion/GoMyFonts
Author: Mion
Options:
-c int
Expiration of cache,Unit:Minute. (default 10)
-l string
The url of your mirror site. (default "http://127.0.0.1/")
-p string
Bind TCP Port. (default ":2333")
-t string
The title of your mirror site. (default "GoMyFonts")
-p
:用于指定 GoMyFonts 监听的 TCP 端口,默认为2333
。-l
:用于指定站点的链接,GoMyFonts 会替换模板中的链接为指定的参数,如您的镜像站点为fonts.qwq.cc
并使用 https 进行访问,则该参数应当填入https://fonts.qwq.cc/
,注意最后的/
不可省略。默认值为http://127.0.0.1/
-c
:指定缓存的到期时间,单位为分钟。GoMyFonts 会将结果缓存至内存来减少回源并加速响应。默认为 10 分钟。-t
:用于指定站定标题。默认为GoMyFont
(在写的时候手滑,后来也懒得改了)。
GoMyFonts 配置完成后只需要将前端项目中的https://fonts.googleapis.com/css?family=xxx
替换为https://gomyfontsmirror/css?family=
即可实现加速效果。
持久化运行
GoMyFonts 暂不直接提供后台持久化运行支持,但可以使用Systemd
与Supervisor
等方式持久化运行 GoMyFonts,以下提供两种参考配置。
Syetemd
将 GoMyFonts 的可执行文件复制到/usr/local/bin
目录:
cp GoMyfonts /usr/local/bin
创建配置文件/etc/systemd/system/gomyfonts.service
如下:
[Unit]
Description=System service for GoMyFonts.
After=network.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/GoMyFonts #you can specify parameters here.
[Install]
WantedBy=multi-user.target
以下为相关的管理命令:
# 将 GoMyFonts 设置为开机自启
systemctl enable gomyfonts
# 启动/重启/停止 GoMyFonts
systemctl start/restart/stop gomyfonts
# 查看 GoMyfonts 运行日志
systemctl status gomyfonts
journalctl -xe
Supervisor
可以参考的 Supervisor 配置文件:
[program:gomyfonts]
command=/path/to/gomyfonts #you can specify parameters here.
directory=/path/to/
autorestart=true
startsecs=3
startretries=3
user=root
priority=999
numprocs=1
使用 HTTPS 或 TLS
GoMyFonts 暂不提供 HTTPS/TLS 支持,但是可以配置 Nginx/Caddy 等前置代理来实现相关功能。
性能测试
待更新,目前来看很好用。
总结
如果只从实现 Google Fonts 镜像单一需求来说,使用 Nginx /Caddy 等工具配置反向代理会是更好的解决方案,创建这个项目的初衷其实是为了巩固一下所学的 Go 语言知识,软件的功能暂时没有经过严格的测试,若您发现了 Bug 可以在 Issue 中提出,当然,也可以提出批评。
补充
GoMyFonts 搭配 CDN 服务可以实现更好的加速效果。