未名树洞的Golang后端。
git clone https://github.com/thuhole/thuhole-go-backend
cd thuhole-go-backend
# (optional)upgrade all dependencies
go get -u ./...
# install packages
go install ./...
将example.config.yml
复制到config.yml
之后修改参数即可运行。
当后端程序运行时编辑config.yml
可以热加载,不需要重启程序。
执行go install ./...
时会在$GOROOT/bin
生成3个可执行文件:
treehole-services-api
:/contents/*
,/send/*
,/edit/*
的API服务treehole-login-api
:/security/*
的API服务treehole-fallback
: 服务宕机时的错误提示程序
除了编译出的3个可执行文件外,部署一个后端需要使用以下服务:
- MySQL数据库。
- Redis数据库。
- Nginx。使得
config.yml
中images_path
文件夹里的图片文件在网页上可访问到。 - 邮件服务。
- Google reCAPTCHA v3, 在https://www.google.com/recaptcha/about/ 注册。
- Google reCAPTCHA v2, 在https://www.google.com/recaptcha/about/ 注册。
⚠注意:出于安全考虑,此配置仅供参考,切勿用于生产环境。
server {
listen 80;
server_name img.thuhole.com;
index index.html index.htm;
root /path/to/images/folder/;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name api.thuhole.com;
index index.html index.htm;
root /path/to/webhole/folder/;
location / {
try_files $uri $uri/ =404;
}
location /contents/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8081;
error_page 502 = @fallback;
}
location /send/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8081;
error_page 502 = @fallback;
}
location /edit/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8081;
error_page 502 = @fallback;
}
location /security/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8080;
error_page 502 = @fallback;
}
location @fallback {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:1234;
}
}