Skip to content

Commit

Permalink
Merge pull request #58 from rexwangcc/master
Browse files Browse the repository at this point in the history
Refactor the code base with a lot of breaking changes.
  • Loading branch information
LiuChangFreeman authored Jan 31, 2020
2 parents 3a8a109 + c3ecb06 commit 0b6fea1
Show file tree
Hide file tree
Showing 32 changed files with 425 additions and 153 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
README.md
README-en.md
CONTRIBUTING.md
4 changes: 2 additions & 2 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Python application
name: Tests on Pull Requests and Master

on:
push:
Expand Down Expand Up @@ -35,4 +35,4 @@ jobs:
- name: Test with pytest
run: |
pip install pytest
pytest test.py
pytest -v src/tests
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contributing Guide

Under Construction...
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Align with the CI/CD YML
FROM python:3.6-alpine
# We cannot use alpine since
# we need to call bash in entrypoint
FROM python:3.6

# Setup workdir
WORKDIR /api-server
Expand All @@ -10,11 +12,8 @@ COPY . .
# Install deps
RUN pip install -r requirements.txt

# Temporary hack until PR https://github.com/wuhan2020/api-server/pull/27 is merged
RUN mkdir -p /home/wuhan2020/wuhan2020

# Expose port 5000
EXPOSE 5000
EXPOSE 9000

# Start the server
ENTRYPOINT [ "bash", "bootstrap"]
152 changes: 152 additions & 0 deletions README-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# wuhan2020-api-server
武汉新型冠状病毒防疫信息收集平台后端([English](https://github.com/Elfsong/api-server#english))

## 快速上手
``` bash
git clone https://github.com/wuhan2020/api-server
cd api-server
git clone https://github.com/wuhan2020/wuhan2020
pip install -r requirements.txt
bash bootstrap
```

随后便可在 `http://{your-ip}:9000/wuhan2020/{list_path}` 调试 api

注意 `list_path``utils.py` 中被 `@data.route()` 中注册的 `path`, `your-ip` 默认是 `127.0.0.1`.

## 在 Docker 容器运行
需要安装[Docker客户端](https://www.docker.com/products/docker-desktop).

### Build Docker Image
在本项目根目录下执行 `docker build -t api-server:default .`.

* 注意:国内这一步可能会耗时较长.

### Run Docker Image
执行 `docker run --name api-server --publish 5000:5000 api-server:default ` 后可在本地浏览器中打开 http://localhost:5000/wuhan2020/{endpoint}. (使用 `-d` 进入detach模式)

_如果出现 `The container name "/api-server" is already in use` 报错可先执行 `docker rm api-server` 删除残留的同名容器._

### Stop Docker Container
执行 `docker stop api-server ` 停止运行中的容器.

## 项目文件说明

```
.
├── bootstrap (阿里云serverless启动脚本)
├── config (flask config dir)
├── swagger (swagger 暂时还不能用, 待适配.)
├── test (test 数据目录, 参照wuhan2020的[readme](https://github.com/wuhan2020/wuhan2020), 移除个人的联系方式和银行卡信息.)
├── wuhan2020 (submoudle, 用于获取data-sync同步过来的数据.)
├── index.py (flask应用默认配置脚本)
└── utils.py (flask蓝图, 目前有csv和json的接口, csv的加上了bearer token 认证, json的接口暂时不能用.)
```

## 配置说明

```
# Flask config
Config
# cache dir
CacheCfg
- csv;
- json;
```

## 测试

`dev url``http://127.0.0.1:9000/wuhan2020/xxx_list`

```sh
# dev
curl --location --request GET '127.0.0.1:9000/wuhan2020/logistical_list' \
--header 'Authorization: Bearer test-safe-wuhan'
# product
curl --location --request GET '127.0.0.1:9000/wuhan2020/logistical_list' \
--header 'Authorization: Bearer product-token'
```

## 遗留问题

- [ ] 添加swagger适配;
- [ ] csv转换出来都是拍平的字段, 里面有部分中文转英文需要帮助转成合适的英文, 实在找不到用拼音替代.


## 前端项目issues
https://github.com/wuhan2020/WebApp/issues

<h2 id="english"> wuhan2020-api-server </h2>
The Information Collection Backend for Wuhan 2019-nCoV epidemic prevention

## Quick Start
``` bash
git clone https://github.com/wuhan2020/api-server
cd api-server
git clone https://github.com/wuhan2020/wuhan2020
pip install -r requirements.txt
bash bootstrap
```

And then debug APIs at `http://your-ip:9000/wuhan2020/`.
Note that `list_path` is the registered path in `utils.py` by `data.route`,
`your-ip` is 127.0.0.1 by default.

## Run Docker Container
Install [Docker Client](https://www.docker.com/products/docker-desktop).

### Build Docker Image
Run `docker build -t api-server:default .` at the root dir.
Note that this step may spend a lot time in the Mainland China area.

### Run Docker Image
Run `docker run --name api-server --publish 5000:5000 api-server:default ` , and then open `http://localhost:5000/wuhan2020/{endpoint}` in the local browser. (Add `-d` into the detach mode)

_If you met this error `The container name "/api-server" is already in use`, run `docker rm api-server` to delete previous containers which have the same name._

### Stop Docker Container
Run `docker stop api-server `to stop running containers.

## Project Description

```
.
├── bootstrap (Serverless Launch Script)
├── index.py (Flask Application Default Configuration Script)
└── utils.py (Flask Blueprint Script)
```
**index.py**
```python
# -*- coding: utf-8 -*-
from flask import Flask,session,request,Blueprint
import os
import sys
import json
import platform
import datetime
from utils import data

app = Flask(__name__)
app.debug = True
# Debug mode is enabled by default
path_prefix= "/wuhan2020"
# Url request prefix, with /wuhan2020 by default
if platform.system()=="Linux":
path_home="/home/wuhan2020/wuhan2020"
else:
path_home=os.path.join(app.root_path,"wuhan2020")
# Serverless use remote NAS to save cache files
# Cache files will be saved at the root dir when you debug locally
if not os.path.exists(path_home):
os.mkdir(path_home)
app.register_blueprint(data, url_prefix=path_prefix)
# Use Flask blueprint to register http-router

if __name__ == '__main__':
# Use default port 9000
port = os.environ.get("FC_SERVER_PORT", "9000")
app.run(host='127.0.0.1', port=int(port))
```
## Front-end issues
https://github.com/wuhan2020/front-pages/issues
166 changes: 50 additions & 116 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,152 +1,86 @@
# wuhan2020-api-server
武汉新型冠状病毒防疫信息收集平台后端([English](https://github.com/Elfsong/api-server#english))
# API Server

## 快速上手
``` bash
git clone https://github.com/wuhan2020/api-server
cd api-server
git clone https://github.com/wuhan2020/wuhan2020
pip install -r requirements.txt
bash bootstrap
```
![Python3.6](https://img.shields.io/badge/python-3.6-green.svg?style=flat-square&logo=python&colorB=blue)
[![Slack Channel](https://img.shields.io/badge/Slack%20Channel-%23api--server-green.svg?style=flat-square&colorB=blue)](https://app.slack.com/client/TT5U1VCPQ/CT3V5CDKJ)
[![Built with love](https://img.shields.io/badge/BUILT%20WITH-LOVE-orange?style=flat-square)](https://img.shields.io/badge/BUILT%20WITH-LOVE-orange?style=flat-square&logo=love)
![Build Status](https://github.com/wuhan2020/api-server/workflows/Tests%20on%20Pull%20Requests%20and%20Master/badge.svg?branch=master&event=push)

随后便可在 `http://{your-ip}:9000/wuhan2020/{list_path}` 调试 api
[中文文档](README-cn.md)

注意 `list_path``utils.py` 中被 `@data.route()` 中注册的 `path`, `your-ip` 默认是 `127.0.0.1`.
This a backend API service of the voluntary information collection and sharing platform to fight against the 2019-nCoV outbreak in Wuhan and the world.

## 在 Docker 容器运行
需要安装[Docker客户端](https://www.docker.com/products/docker-desktop).
The API is designed to be thin and stateless. It relies on the data collected and validated by other sub-projects, transform and expose them through standard RESTful APIs. The service is written in Python and Flask.

### Build Docker Image
在本项目根目录下执行 `docker build -t api-server:default .`.
## Get Started

* 注意:国内这一步可能会耗时较长.
Please first clone this repository and the sub-module-repo by:

### Run Docker Image
执行 `docker run --name api-server --publish 5000:5000 api-server:default ` 后可在本地浏览器中打开 http://localhost:5000/wuhan2020/{endpoint}. (使用 `-d` 进入detach模式)
```
git clone https://github.com/wuhan2020/api-server
cd api-server
git clone https://github.com/wuhan2020/wuhan2020
```

_如果出现 `The container name "/api-server" is already in use` 报错可先执行 `docker rm api-server` 删除残留的同名容器._
### Running locally with Docker (Recommended)

### Stop Docker Container
执行 `docker stop api-server ` 停止运行中的容器.
**Pre-requisite: You have to have [Docker client](https://www.docker.com/products/docker-desktop) installed on your machine.**

## 项目文件说明
#### Build the Docker image

Run:
```
.
├── bootstrap (阿里云serverless启动脚本)
├── config (flask config dir)
├── swagger (swagger 暂时还不能用, 待适配.)
├── test (test 数据目录, 参照wuhan2020的[readme](https://github.com/wuhan2020/wuhan2020), 移除个人的联系方式和银行卡信息.)
├── wuhan2020 (submoudle, 用于获取data-sync同步过来的数据.)
├── index.py (flask应用默认配置脚本)
└── utils.py (flask蓝图, 目前有csv和json的接口, csv的加上了bearer token 认证, json的接口暂时不能用.)
docker build -t api-server:default .
```
from the root directory of the clone of this repo. Note this step could take a long time dependd on where you are located in.

## 配置说明
#### Run built Docker image

Run:
```
# Flask config
Config
# cache dir
CacheCfg
- csv;
- json;
docker run --name api-server --publish 9000:9000 api-server:default
```
and then open `http://localhost:9000` in your browser. _(Add `-d` to run the Docker container in detach/background mode)_

## 测试
You should see a Swagger page documents the available endpoints now.

`dev url``http://127.0.0.1:9000/wuhan2020/xxx_list`
_If you ran into error `The container name "/api-server" is already in use`, please run `docker rm api-server` to delete previous container which has the same name._

```sh
# dev
curl --location --request GET '127.0.0.1:9000/wuhan2020/logistical_list' \
--header 'Authorization: Bearer test-safe-wuhan'
# product
curl --location --request GET '127.0.0.1:9000/wuhan2020/logistical_list' \
--header 'Authorization: Bearer product-token'
```
#### Stop running Docker container

## 遗留问题
Run:
```
docker stop api-server
```
to stop the running container.

- [ ] 添加swagger适配;
- [ ] csv转换出来都是拍平的字段, 里面有部分中文转英文需要帮助转成合适的英文, 实在找不到用拼音替代.
### Running with your own Python environment

Please make sure you have **Python3.6** installed, (ideally you should be using a [VirtualEnv](https://docs.python.org/3.6/tutorial/venv.html)
or something like [PyEnv](https://github.com/pyenv/pyenv)). Then from the root directory of the cloned repo, run:

## 前端项目issues
https://github.com/wuhan2020/WebApp/issues
```
pip install -U -r requirements.txt
```

<h2 id="english"> wuhan2020-api-server </h2>
The Information Collection Backend for Wuhan 2019-nCoV epidemic prevention
and then start the server by:

## Quick Start
``` bash
git clone https://github.com/wuhan2020/api-server
cd api-server
git clone https://github.com/wuhan2020/wuhan2020
pip install -r requirements.txt
```
bash bootstrap
```
now if you open `http://localhost:9000` in your browser, you should see a Swagger page documents the available endpoints.

And then debug APIs at `http://your-ip:9000/wuhan2020/`.
Note that `list_path` is the registered path in `utils.py` by `data.route`,
`your-ip` is 127.0.0.1 by default.

## Run Docker Container
Install [Docker Client](https://www.docker.com/products/docker-desktop).
## Development

### Build Docker Image
Run `docker build -t api-server:default .` at the root dir.
Note that this step may spend a lot time in the Mainland China area.
Coming soon...

### Run Docker Image
Run `docker run --name api-server --publish 5000:5000 api-server:default ` , and then open `http://localhost:5000/wuhan2020/{endpoint}` in the local browser. (Add `-d` into the detach mode)
## Deployment

_If you met this error `The container name "/api-server" is already in use`, run `docker rm api-server` to delete previous containers which have the same name._
Coming soon...

### Stop Docker Container
Run `docker stop api-server `to stop running containers.
## Contributing

## Project Description
Please see [Conntributing Guide](CONTRIBUTING.md)

```
.
├── bootstrap (Serverless Launch Script)
├── index.py (Flask Application Default Configuration Script)
└── utils.py (Flask Blueprint Script)
```
**index.py**
```python
# -*- coding: utf-8 -*-
from flask import Flask,session,request,Blueprint
import os
import sys
import json
import platform
import datetime
from utils import data

app = Flask(__name__)
app.debug = True
# Debug mode is enabled by default
path_prefix= "/wuhan2020"
# Url request prefix, with /wuhan2020 by default
if platform.system()=="Linux":
path_home="/home/wuhan2020/wuhan2020"
else:
path_home=os.path.join(app.root_path,"wuhan2020")
# Serverless use remote NAS to save cache files
# Cache files will be saved at the root dir when you debug locally
if not os.path.exists(path_home):
os.mkdir(path_home)
app.register_blueprint(data, url_prefix=path_prefix)
# Use Flask blueprint to register http-router

if __name__ == '__main__':
# Use default port 9000
port = os.environ.get("FC_SERVER_PORT", "9000")
app.run(host='127.0.0.1', port=int(port))
```
## Front-end issues
https://github.com/wuhan2020/front-pages/issues

Plases check [here](https://github.com/wuhan2020/front-pages/issues)
Loading

0 comments on commit 0b6fea1

Please sign in to comment.