diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..95adf9c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,19 @@ +# See https://help.github.com/ignore-files/ for more about ignoring files. + +# dependencies +/node_modules + +# testing +/coverage + +# misc +.DS_Store +.env +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# css +src/**/*.css +build +build.zip diff --git a/Dockerfile b/Dockerfile index 23f7d3d..a5ae15e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,37 @@ -FROM node:8.11.1-alpine +# BUILDING +FROM node:lts-alpine AS builder +# base on work of llitfkitfk@gmail.com +LABEL maintainer="chibing.fy@alibaba-inc.com" + +WORKDIR /app + +# cache dependencies +COPY package.json ./ + +# 在国内打开下面一行加速 +#RUN npm config set registry https://registry.npm.taobao.org/ + +# instal dependencies +RUN npm install typescript -g && \ + npm install + +# build +COPY . ./ +RUN npm run build + +# RUNNING +FROM node:lts-alpine + +# base on work of llitfkitfk@gmail.com +LABEL maintainer="chibing.fy@alibaba-inc.com" +# use China mirror of: https://github.com/jgm/pandoc/releases/download/2.7.3/pandoc-2.7.3-linux.tar.gz +RUN wget http://q08gwzg9o.bkt.clouddn.com/pandoc-2.7.3-linux.tar.gz && \ + tar -xf pandoc-2.7.3-linux.tar.gz && \ + cp pandoc-2.7.3/bin/* /usr/bin/ && \ + pandoc -v && \ + rm -rf pandoc-2.7.3-linux.tar.gz pandoc-2.7.3 + WORKDIR /app -ADD . /tmp -RUN /bin/sh -c 'cd /tmp && npm install && npm install -g typescript && npm run build && mv ./dist/* /app/ && mv ./node_modules /app/ && rm -rf /tmp' \ No newline at end of file +COPY --from=builder /app/dist . +COPY --from=builder /app/node_modules ./node_modules \ No newline at end of file diff --git a/README.md b/README.md index 4f00c7a..b52a7b0 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,88 @@ -# RAP2-DELOS 开源社区版本 (后端API服务器) +# RAP2-DELOS 开源社区版本 (后端 API 服务器) [![Build Status](https://travis-ci.org/thx/rap2-delos.svg?branch=master)](https://travis-ci.org/thx/rap2-delos) -RAP2 包含两个组件(对应两个Github Repository)。 +RAP2 是在 RAP1 基础上重做的新项目,它能给你提供方便的接口文档管理、Mock、导出等功能,包含两个组件(对应两个 Github Repository)。 -* rap2-delos: 后端数据API服务器,基于Koa + MySQL[link](http://github.com/thx/rap2-delos) -* rap2-dolores: 前端静态资源,基于React [link](http://github.com/thx/rap2-dolores) +- rap2-delos: 后端数据 API 服务器,基于 Koa + MySQL[link](http://github.com/thx/rap2-delos) +- rap2-dolores: 前端静态资源,基于 React [link](http://github.com/thx/rap2-dolores) + + +**Rap 官方服务站点,无需安装直接体验: [rap2.taobao.org](http://rap2.taobao.org)** + +**有急事来官方钉钉群,响应更迅速: 11789704** + +***2019-10-31:现已支持 Docker 一键部署,欢迎大家体验&反馈*** ***2019-09-27:更新的用户请注意按照下面指引安装 pandoc 以启用文档导出功能*** -### Resources -* [Official Site 官网: rap2.taobao.org](http://rap2.taobao.org) -* 钉钉群ID: 11789704 -* [热心网友提供的部署文档,供参考](https://github.com/thx/rap2-delos/issues/119) +## 推荐使用 Docker 快速部署 + +### 安装 Docker + +国内用户可参考 [https://get.daocloud.io/](https://get.daocloud.io/) 安装 Docker 以及 Docker Compose (Linux 用户需要单独安装),建议按照链接指引配置 Docker Hub 的国内镜像提高加载速度。 + +### 配置项目 + +在任意地方建立目录 rap -## 部署 +把本仓库中的 [docker-compose.yml](https://raw.githubusercontent.com/thx/rap2-delos/master/docker-compose.yml) 放到 rap 目录中 + +Rap 前端服务的端口号默认为 3000,你可以在 docker-compose.yml 中按照注释自定义 + +在 rap 目录下执行下面的命令: + +```sh +# 拉取镜像并启动 +docker-compose up -d + +# 启动后,第一次运行需要手动初始化mysql数据库 +# ⚠️注意: 只有第一次该这样做 +docker-compose exec delos node scripts/init + +# 部署成功后 访问 +http://localhost:3000 # 前端(可自定义端口号) +http://localhost:38080 # 后端 + +# 如果访问不了可能是数据库没有链接上,关闭 rap 服务 +docker-compose down +# 再重新运行 +docker-compose up -d +``` + +**⚠️注意:第一次运行后 rap 目录下会被自动创建一个 docker 目录,里面存有 rap 的数据库数据,可千万不要删除。** + +#### 镜像升级 + +Rap 经常会进行 bugfix 和功能升级,用 Docker 可以很方便地跟随主项目升级 + +```sh +# 拉取一下最新的镜像 +docker-compose pull +# 暂停当前应用 +docker-compose down +# 重新构建并启动 +docker-compose up -d --build +# 清空不被使用的虚悬镜像 +docker image prune -f +``` + +## 手动部署 ### 环境要求 -* Node.js 8.9.4+ -* MySQL 5.7+ -* Redis 4.0+ -* pandoc 2.73 (供文档生成使用) + +- Node.js 8.9.4+ +- MySQL 5.7+ +- Redis 4.0+ +- pandoc 2.73 (供文档生成使用) ### 开发模式 -#### 安装MySQL和Redis服务器 +#### 安装 MySQL 和 Redis 服务器 -请自行查找搭建方法,mysql/redis配置在config.*.ts文件中,在不修改任何配置的情况下, -redis会通过默认端口 + 本机即可正常访问,确保redis-server打开即可。 +请自行查找搭建方法,mysql/redis 配置在 config.\*.ts 文件中,在不修改任何配置的情况下, +redis 会通过默认端口 + 本机即可正常访问,确保 redis-server 打开即可。 注意:修改 cofig 文件后需要重新 `npm run build` 才能生效 @@ -46,7 +100,7 @@ redis会通过默认端口 + 本机即可正常访问,确保redis-server打开 redis-server ``` -后台执行可以使用nohup或pm2,这里推荐使用pm2,下面命令会安装pm2,并通过pm2来启动redis缓存服务 +后台执行可以使用 nohup 或 pm2,这里推荐使用 pm2,下面命令会安装 pm2,并通过 pm2 来启动 redis 缓存服务 ```bash npm install -g pm2 @@ -65,9 +119,9 @@ mysql -e 'CREATE DATABASE IF NOT EXISTS RAP2_DELOS_APP DEFAULT CHARSET utf8 COLL npm install ``` -confirm configurations in /config/config.dev.js (used in development mode),确认/config/config.dev.js中的配置(.dev.js后缀表示用于开发模式)。 +confirm configurations in /config/config.dev.js (used in development mode),确认/config/config.dev.js 中的配置(.dev.js 后缀表示用于开发模式)。 -#### 安装 && TypeScript编译 +#### 安装 && TypeScript 编译 ```bash npm install -g typescript @@ -80,12 +134,14 @@ npm run build npm run create-db ``` -#### 执行mocha测试用例和js代码规范检查 +#### 执行 mocha 测试用例和 js 代码规范检查 + ```bash npm run check ``` #### 启动开发模式的服务器 监视并在发生代码变更时自动重启 + ```bash npm run dev ``` @@ -99,44 +155,26 @@ npm start ``` -## Dockerfile (本地源码通过docker运行) -```sh -# 1. 安装docker -# 2. 修改docker-compose.xml中的配置。默认使用mysql和redis的镜像。可修改为自己的配置 -# 3. 通过源码运行。 - docker-compose up -d -# 4. 第一次运行需要手动初始化mysql数据库。分别执行以下命令: - docker exec -it rap2-delos sh - // 登录成功以后执行: - node scripts/init - // 执行完毕后退出 - exit - // 如果仍然有问题,重新启动 - docker-compose down - // 重新运行 - docker-compose up -d -``` - - ## Author -* 版权: 阿里妈妈前端团队 -* 作者: - * RAP2 2017/10前版本作者为[墨智(@Nuysoft)](https://github.com/nuysoft/), [mockjs](mockjs.com)的作者。 - * 2017/10之后版本开发者 - * [霍雍(Bosn)](https://github.com/bosn/),[RAP1](https://github.com/thx/RAP)作者,RAP最早的创始人。 - * [承虎(alvarto)](https://github.com/alvarto/) - * [池冰(bigfengyu)](https://github.com/bigfengyu) + +- 版权: 阿里妈妈前端团队 +- 作者: + - RAP2 2017/10 前版本作者为[墨智(@Nuysoft)](https://github.com/nuysoft/), [mockjs](mockjs.com)的作者。 + - 2017/10 之后版本开发者 + - [霍雍(Bosn)](http://github.com/bosn/),[RAP1](http://github.com/thx/RAP)作者,RAP 最早的创始人。 + - [承虎(alvarto)](http://github.com/alvarto/) + - [池冰(bigfengyu)](https://github.com/bigfengyu) ### Tech Arch -* 前端架构(rap2-dolores) - * React / Redux / Saga / Router - * Mock.js - * SASS / Bootstrap 4 beta - * server: nginx -* 后端架构(rap2-delos) - * Koa - * Sequelize - * MySQL - * Server - * server: node +- 前端架构(rap2-dolores) + - React / Redux / Saga / Router + - Mock.js + - SASS / Bootstrap 4 beta + - server: nginx +- 后端架构(rap2-delos) + - Koa + - Sequelize + - MySQL + - Server + - server: node diff --git a/docker-compose.yml b/docker-compose.yml index 6e04484..69f94cb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,54 +1,55 @@ # mail@dongguochao.com +# llitfkitfk@gmail.com +# chibing.fy@alibaba-inc.com -version: '2.2' +version: "3" services: + # frontend + dolores: + image: rapteam/rap2-dolores:latest + ports: + #冒号前可以自定义前端端口号,冒号后不要动 + - 3000:38081 + + # backend delos: - container_name: rap2-delos - # build from ./Dockerfile -# build: . - # build from images - image: blackdog1987/rap2-delos:2.6.aa3be03 + image: rapteam/rap2-delos:latest + ports: + # 这里的配置不要改哦 + - 38080:38080 environment: + - SERVE_PORT=38080 # if you have your own mysql, config it here, and disable the 'mysql' config blow - - MYSQL_URL=rap2-mysql # links will maintain /etc/hosts, just use 'container_name' + - MYSQL_URL=mysql # links will maintain /etc/hosts, just use 'container_name' - MYSQL_PORT=3306 - MYSQL_USERNAME=root - MYSQL_PASSWD= - MYSQL_SCHEMA=rap2 # redis config - - REDIS_URL=rap2-redis + - REDIS_URL=redis - REDIS_PORT=6379 # production / development - NODE_ENV=production - working_dir: /app - privileged: true ###### 'sleep 30 && node scripts/init' will drop the tables ###### RUN ONLY ONCE THEN REMOVE 'sleep 30 && node scripts/init' - command: /bin/sh -c 'sleep 30; node scripts/init; node dispatch.js' + command: /bin/sh -c 'node dispatch.js' # init the databases -# command: sleep 30 && node scripts/init && node dispatch.js + # command: sleep 30 && node scripts/init && node dispatch.js # without init -# command: node dispatch.js - links: - - redis - - mysql + # command: node dispatch.js depends_on: - redis - mysql - ports: - - "38080:8080" # expose 38080 redis: - container_name: rap2-redis - image: redis:4.0.9 + image: redis:4 # disable this if you have your own mysql mysql: - container_name: rap2-mysql - image: mysql:5.7.22 + image: mysql:5.7 # expose 33306 to client (navicat) #ports: # - 33306:3306 diff --git a/package.json b/package.json index e52a152..bcf5b0a 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "start": "cross-env NODE_ENV=production pm2 start dist/dispatch.js --name=rap-server-delos", "lint": "echo \"TSLint checking...\" && tslint -c tslint.json 'src/**/*.ts' 'src/**/*.tsx'", "start:redis": "pm2 start redis-server --name redis-server", - "clean": "pm2 delete all" + "clean": "pm2 delete all", + "build-docker": "docker build --rm -f \"Dockerfile\" -t rapteam/rap2-delos:latest ." }, "author": "bosn, nuysoft", "license": "ISC", diff --git a/src/config/config.dev.ts b/src/config/config.dev.ts index facf669..8519047 100644 --- a/src/config/config.dev.ts +++ b/src/config/config.dev.ts @@ -3,7 +3,7 @@ import { IConfigOptions } from "../types" let config: IConfigOptions = { version: 'v2.7.0', serve: { - port: 8080, + port: (process.env.SERVE_PORT && parseInt(process.env.SERVE_PORT)) || 8080, path: '', }, keys: ['some secret hurr'], diff --git a/src/config/config.local.ts b/src/config/config.local.ts index 7af4172..00d5516 100644 --- a/src/config/config.local.ts +++ b/src/config/config.local.ts @@ -3,7 +3,7 @@ import { IConfigOptions } from "../types" let config: IConfigOptions = { version: '2.7.0', serve: { - port: 8080, + port: (process.env.SERVE_PORT && parseInt(process.env.SERVE_PORT)) || 8080, path: '', }, keys: ['some secret hurr'], diff --git a/src/config/config.prod.ts b/src/config/config.prod.ts index 36252a7..2459717 100644 --- a/src/config/config.prod.ts +++ b/src/config/config.prod.ts @@ -4,7 +4,7 @@ import { IConfigOptions } from "../types" let config: IConfigOptions = { version: '2.7.0', serve: { - port: (process.env.EXPOSE_PORT && parseInt(process.env.EXPOSE_PORT)) || 8080, + port: (process.env.SERVE_PORT && parseInt(process.env.SERVE_PORT)) || 8080, path: '', }, keys: ['some secret hurr'],