Skip to content

Commit

Permalink
Merge pull request #53 from Jonnyan404/main
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticit committed May 22, 2022
2 parents 9fd00a2 + 6d8d1e7 commit 6d56147
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 94 deletions.
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM node:18-alpine as builder
COPY . /app/
WORKDIR /app
ARG EIP=mrdoc.fun
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN npm config set registry http://registry.npm.taobao.org/
RUN npm i -g pm2 @nestjs/cli pnpm
RUN apk --no-cache add bash
RUN sed -i "s/localhost/$EIP/g" /app/docker/prod-sample.yaml
RUN cp -f /app/docker/prod-sample.yaml /app/config/prod.yaml
RUN bash build-output.sh


FROM node:18-alpine as prod
LABEL maintainer="www.mrdoc.fun"
ENV TZ=Asia/Shanghai
COPY --from=builder /app/docker/* /app/docker/
COPY --from=builder /app/output/ /app/

WORKDIR /app
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
RUN npm config set registry http://registry.npm.taobao.org/
RUN set -x \
&& apk update \
&& apk add --no-cache tzdata redis \
&& chmod +x /app/docker/start.sh \
&& npm i -g pm2 @nestjs/cli pnpm \
&& rm -rf /var/cache/apk/*

ENTRYPOINT sh /app/docker/start.sh
144 changes: 50 additions & 94 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Think 是一款开源知识管理工具。通过独立的知识库空间,结
![协作](http://wipi.oss-cn-shanghai.aliyuncs.com/2022-02-20/YN67GM4VQMBTZFZ88TYQ8X/image.png)
![收藏](http://wipi.oss-cn-shanghai.aliyuncs.com/2022-02-20/YN67GM4VQMBTZFZ88TYPHX/image.png)

## 项目运行
## 项目结构

本项目依赖 pnpm 使用 monorepo 形式进行代码组织,分包如下:

Expand All @@ -35,26 +35,25 @@ Think 是一款开源知识管理工具。通过独立的知识库空间,结
- `@think/server`:服务端
- `@think/client`:客户端

### pnpm
## 项目依赖

项目依赖 pnpm,请安装后运行(`npm i -g pnpm`)。
- nodejs ≥ 16.5
- pnpm
- pm2
- mysql ≥ 5.7
- redis (可选)

### 数据库
依赖安装命令: `npm i -g pm2 @nestjs/cli pnpm`


#### 数据库

首先安装 `MySQL`,推荐使用 docker 进行安装。

```bash
docker image pull mysql:5.7
# m1 的 mac 可以用:docker image pull --platform linux/x86_64 mysql:5.7
docker run -d --restart=always --name think -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.7
```

然后在 `MySQL` 中创建数据库。

```bash
docker container exec -it think bash;
mysql -u root -p;
CREATE DATABASE `think` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
docker run -d --restart=always --name think -p 3306:3306 -e MYSQL_DATABASE=think -e MYSQL_ROOT_PASSWORD=root mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
```

#### 可选:Redis
Expand All @@ -66,102 +65,59 @@ docker pull redis:latest
docker run --name think-redis -p 6379:6379 -d redis --appendonly yes --requirepass "root"
```

### 本地运行
## Docker-compose 一键构建安装

首先,clone 项目。
- 实测腾讯轻量云 2C4G 机器构建需 8 分钟左右

```bash
git clone --depth=1 https://github.com/fantasticit/think.git your-project-name
```
**请注意修改 `docker-compose.yml` 中的 `EIP` 参数,否则无法正常使用!!!**

然后,安装项目依赖。

```bash
pnpm install
```
# 首次安装
git clone https://github.com/fantasticit/think.git
cd think
docker-compose up -d
#二次更新升级
cd think
git pull
docker-compose build
docker-compose up -d
```

- 启动项目
然后访问 `http://ip:5001` 即可.

```bash
pnpm run dev
```

- 前台页面地址:`http://localhost:5001`
- 服务接口地址:`http://localhost:5002`
- 协作接口地址:`http://localhost:5003`

如需修改配置,可在 `config/dev.yaml` 中进行配置。

### 配置文件

默认加载 `config/dev.yaml` 中的配置(生产环境使用 `config/prod.yaml` )。

```yaml
# 开发环境配置
# 开发环境配置
client:
port: 5001
assetPrefix: '/'
apiUrl: 'http://localhost:5002/api'
collaborationUrl: 'ws://localhost:5003'
# 以下为页面 meta 配置
seoAppName: '云策文档'
seoDescription: '云策文档是一款开源知识管理工具。通过独立的知识库空间,结构化地组织在线协作文档,实现知识的积累与沉淀,促进知识的复用与流通。'
seoKeywords: '云策文档,协作,文档,前端面试题,fantasticit,https://github.com/fantasticit/think'
# 预先连接的来源,空格分割(比如图片存储服务器)
dnsPrefetch: '//wipi.oss-cn-shanghai.aliyuncs.com'

server:
prefix: '/api'
port: 5002
collaborationPort: 5003
maxDocumentVersion: 20 # 最大版本记录数
logRetainDays: 3 # 日志保留天数,比如只保留近三天日志

# 数据库配置
db:
mysql:
host: '127.0.0.1'
username: 'root'
password: 'root'
database: 'think'
port: 3306
charset: 'utf8mb4'
timezone: '+08:00'
synchronize: true
redis:
host: '127.0.0.1'
port: '6379'
password: 'root'

# oss 文件存储服务
oss:
aliyun:
accessKeyId: ''
accessKeySecret: ''
bucket: ''
https: true
region: ''

# jwt 配置
jwt:
secretkey: 'zA_Think+KNOWLEDGE+WIKI+DOCUMENTS@2022'
expiresIn: '6h'
```

### 项目部署
## 手动安装教程

- 前台页面地址:`http://localhost:5001`
- 服务接口地址:`http://localhost:5002`
- 协作接口地址:`http://localhost:5003`

如需修改配置,开发环境编辑 `config/dev.yaml`。生产环境编辑 `config/prod.yaml` (如没有,可复制开发环境的配置修改即可.)

### 本地源代码运行(开发环境)

生产环境部署的脚本如下:

```bash
git clone https://github.com/fantasticit/think.git
cd think
pnpm install
pnpm run dev
```

然后访问 `http://ip:5001` 即可.

node -v
npm -v

npm config set registry http://registry.npmjs.org

npm i -g pm2 @nestjs/cli pnpm
### 本地源代码运行(生产环境)

生产环境部署的脚本如下:

```bash
git clone https://github.com/fantasticit/think.git
cd think
pnpm install
pnpm run build
pnpm run pm2
Expand All @@ -170,7 +126,7 @@ pm2 startup
pm2 save
```

### nginx 配置
### nginx 配置参考

采用反向代理进行 `nginx` 配置,**同时设置 `proxy_set_header X-Real-IP $remote_addr;` 以便服务端获取到真实 ip 地址**

Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "3"
services:
thinkdoc:
build:
context: .
args:
EIP: x.x.x.x # api接口IP,必须设置,可以是 IP 或者域名.
image: think
container_name: thinkdoc
#restart: always
volumes:
- /path/to/you/dir/config:/app/config # 请注意修改 /path/to/you/dir 为云策文档配置文件目录.
environment:
- TZ=Asia/Shanghai
ports:
- "5001-5003:5001-5003"
depends_on:
- mysql
mysql:
image: mysql:5.7
container_name: mysql-with-think
#restart: always
volumes:
- /path/to/you/dir/mysql:/var/lib/mysql # 请注意修改 /path/to/you/dir 为您要存储mysql数据的目录绝对路径.
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=Jonnyan404!
- MYSQL_DATABASE=think
- MYSQL_USER=jonnyan404
- MYSQL_PASSWORD=www.mrdoc.fun
expose:
- "3306"
ports:
- "63306:3306" # 如果不需要外部连接mysql,可注释此行+上一行.
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
# Volumes for persisting data, see https://docs.docker.com/engine/admin/volumes/volumes/
#volumes:
# thinkdoc-data:
# driver: local
# mysql-data:
# driver: local
49 changes: 49 additions & 0 deletions docker/prod-sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 生产环境docker示例配置
client:
port: 5001
assetPrefix: '/'
apiUrl: 'http://localhost:5002/api'
collaborationUrl: 'ws://localhost:5003'
# 以下为页面 meta 配置
seoAppName: '云策文档'
seoDescription: '云策文档是一款开源知识管理工具。通过独立的知识库空间,结构化地组织在线协作文档,实现知识的积累与沉淀,促进知识的复用与流通。'
seoKeywords: '云策文档,协作,文档,前端面试题,fantasticit,https://github.com/fantasticit/think'
# 预先连接的来源,空格分割(比如图片存储服务器)
dnsPrefetch: '//wipi.oss-cn-shanghai.aliyuncs.com'

server:
prefix: '/api'
port: 5002
collaborationPort: 5003
maxDocumentVersion: 20 # 最大版本记录数
logRetainDays: 3 # 日志保留天数,比如只保留近三天日志

# 数据库配置
db:
mysql:
host: 'mysql-with-think'
username: 'jonnyan404'
password: 'www.mrdoc.fun'
database: 'think'
port: 3306
charset: 'utf8mb4'
timezone: '+08:00'
synchronize: true
redis:
host: '127.0.0.1'
port: '6379'
password: ''

# oss 文件存储服务
oss:
aliyun:
accessKeyId: ''
accessKeySecret: ''
bucket: ''
https: true
region: ''

# jwt 配置
jwt:
secretkey: 'zA_Think+KNOWLEDGE+WIKI+DOCUMENTS@2022'
expiresIn: '6h'
14 changes: 14 additions & 0 deletions docker/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
### Author:jonnyan404
### date:2022年5月22日
CONFIG_FILE='/app/config/prod.yaml'

if [ ! -f $CONFIG_FILE ]; then
echo "#####Generating configuration file#####"
cp /app/docker/prod-sample.yaml $CONFIG_FILE
else
echo "#####Configuration file already exists#####"
fi
redis-server --daemonize yes
pnpm run pm2
pm2 logs

0 comments on commit 6d56147

Please sign in to comment.