From 4c4ddca82b7e344ed1b9248ca72e5602379e57e4 Mon Sep 17 00:00:00 2001 From: hole-thu Date: Sun, 10 Jul 2022 01:20:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81Docker=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 6 +++ Dockerfile | 19 +++++++ README.md | 94 ++++++++++++++++++++++++--------- docker-compose.yml | 27 ++++++++++ psql-docker-init/add_pg_trgm.sh | 5 ++ 5 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 psql-docker-init/add_pg_trgm.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c2bf7ee --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +/target +Cargo.lock +rust-toolchain +/user_files +*.db +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..feed0db --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM rust:1-bullseye as builder +WORKDIR /usr/src/ +RUN cargo new myapp --vcs none +WORKDIR /usr/src/myapp +COPY Cargo.toml ./ +RUN cargo build --release + +# 为了充分利用docker的缓存 +COPY src ./src +COPY migrations ./migrations +RUN touch src/main.rs && cargo build --release + + +FROM debian:bullseye-slim +RUN apt-get update && apt-get install libpq5 -y +COPY --from=builder /usr/src/myapp/target/release/hole-thu /usr/local/bin/hole-thu +COPY Rocket.toml /usr/local/bin/ + +CMD ["hole-thu"] diff --git a/README.md b/README.md index 935f93f..0e614a4 100644 --- a/README.md +++ b/README.md @@ -3,23 +3,72 @@ ## 部署 +### 使用docker + ++ 安装docker-compose + ++ 执行 +```shell +mkdir hole +cd hole + +# 下载docker-compose.yml +wget https://git.thu.monster/newthuhole/hole-backend-rust/raw/branch/master/docker-compose.yml + +# 下载add_pg_trgm.sh +mkdir psql-docker-init +wget https://git.thu.monster/newthuhole/hole-backend-rust/raw/branch/master/psql-docker-init/add_pg_trgm.sh -O psql-docker-init/add_pg_trgm.sh + + +#下载镜像 +docker-compose pull +# 初始化postgres +docker-compose up -d postgres + +#建表 +docker-compose run --rm hole-thu hole-thu --init-database + +# 全部跑起来 +docker-compose up -d +``` + +现在树洞后端应该已经运行在8000端口了 + +停止运行: + +```shell +docker-compose stop +``` + +可能需要修改`docker-compose.yml`的情况: + ++ 如果希望使用其他端口而非8000,编辑services.hole-thu.ports + ++ 你可能需要映射postgres的5432端口用于在宿主机上连接以创建管理员账号,但也可以在`add_pg_trgm.sh`中添加创建管理员账号的sql语句或新增加一个`create_admin.sh` + ++ 如果需要使用闭社登陆,请在services.hole-thu.environment中添加需要用到的更多环境变量(参考`.env.sample`) + +### 使用源码编译 + *以下内容假设你使用 Ubuntu 20.04* -目前只支持postgresql,对支持sqlite的追踪见 issue #1 +安装rust与cargo环境 (略) + +clone 代码 (略) 安装postgresql (略) 安装redis (略) -### 准备数据库 +#### 准备数据库 进入: -``` +```shell sudo -u postgres psql ``` -执行 (替换`'hole_pass'`为实际希望使用的密码): +执行 (替换`hole_pass`为实际希望使用的密码): ```postgresql postgres=# CREATE USER hole WITH PASSWORD 'hole_pass'; @@ -32,39 +81,36 @@ hole_v2=# CREATE EXTENSION pg_trgm; CREATE EXTENSION hole_v2=# \q ``` -### 运行 +#### 编译&运行 创建 .env 文件,写入必要的环境变量。可参考 .env.sample。 -#### 基于二进制文件 - -从[release](https://git.thu.monster/newthuhole/hole-backend-rust/releases)直接下载二进制文件 - -``` -./hole-thu --init-database -./hole-thu -``` - -#### 基于源码 - -安装rust与cargo环境 (略) - -clone 代码 (略) - -``` +```shell cargo run --release -- --init-database cargo run --release ``` 或安装`diesel_cli`后 -``` +```shell diesel migration run cargo run --release ``` -### 关于账号系统 +### 基于二进制文件 + +安装与准备数据库同 + +从[release](https://git.thu.monster/newthuhole/hole-backend-rust/releases)直接下载二进制文件 + +```shell +./hole-thu --init-database +./hole-thu +``` + + +## 关于账号系统 -+ 如果你希望使用自己的登录系统,将 `/_login/` 路径交由另外的后端处理,只需最终将用户名和token写入users表,并跳转到 `/?token=`。 ++ 如果你希望使用自己的登录系统,在Nginx或Apache中将 `/_login/` 路径交由另外的后端处理,只需最终将用户名和token写入users表,并跳转到 `/###token=`。 + 如果你希望也使用闭社提供的授权来维护账号系统,使用 `https://thu.closed.social/api/v1/apps` 接口创建应用,并在.env或环境变量中填入client与secret。此操作不需要闭社账号。详情见[文档](https://docs.joinmastodon.org/client/token/#app)。编译运行时,增加`--features mastlogin`: `cargo run --release --features mastlogin` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..51d50df --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +version: '1' + +services: + postgres: + image: postgres:14.3 + restart: unless-stopped + volumes: + - "./data/postgres:/var/lib/postgresql/data" + - "./psql-docker-init:/docker-entrypoint-initdb.d" + environment: + POSTGRES_PASSWORD: hole_pass + POSTGRES_USER: hole + POSTGRES_DB: hole_v2 + redis: + image: redis:7.0.2 + restart: unless-stopped + hole-thu: + image: holethu/hole-backend-rust:1.2.0 + restart: unless-stopped + ports: + - "127.0.0.1:8000:8863" + environment: + DATABASE_URL: "postgres://hole:hole_pass@postgres/hole_v2" + REDIS_URL: "redis://redis:6379" + ROCKET_DATABASES: '{pg_v2={url="postgres://hole:hole_pass@postgres/hole_v2"}}' + ROCKET_ADDRESS: "0.0.0.0" + ROCKET_PORT: 8863 diff --git a/psql-docker-init/add_pg_trgm.sh b/psql-docker-init/add_pg_trgm.sh new file mode 100644 index 0000000..01115ad --- /dev/null +++ b/psql-docker-init/add_pg_trgm.sh @@ -0,0 +1,5 @@ +#!/bin/bash +psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL +create extension pg_trgm; +EOSQL +