GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ansionfor/python_docker: 基于flask web框架通过接口申请mysql、redis的docker实例

BranchesTags
Open more actions menu

Latest commit

 

History

18 Commits
18 Commits

Folders and files

NameName
Last commit message
Last commit date
api
api
 
 
common
common
 
 
config
config
 
 
service
service
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
desc.png
desc.png
 
 
init.sql
init.sql
 
 
main.py
main.py
 
 
requirements.txt
requirements.txt
 
 

Repository files navigation

基于Flask Web + Docker的数据库资源申请项目

通过Restful Api申请Mysql、Redis资源

目录

  • 上手指南
    • 配置要求
    • Docker镜像
    • Python库依赖
  • 安装步骤
  • 流程图
  • 文件目录说明
  • 项目特性
  • 接口文档
    • 接口鉴权
    • 创建mysql实例
    • 创建redis实例
    • 获取实例列表
    • 获取实例详情
  • 代办
  • 作者

上手指南

测试环境配置

  1. Centos 64位
  2. Python 3.6
  3. PIP 3
  4. Docker 19.03.4
  5. Mysql 5.7

Docker镜像依赖

  1. Mysql 8.0
  2. Redis 5.0

Python库依赖

  1. Flask 2.0.1
  2. Flask-RESTful 0.3.9
  3. PyMySQL 0.9.3

安装步骤

  1. 安装依赖库
# 安装python依赖(推荐使用pyenv虚拟环境)
pip3 install -r requirements.txt

# 安装docker 镜像(mysql、redis)
docker pull mysql:8.0
docker pull redis:5.0
  1. 克隆仓库
git clone https://github.com/ansionfor/python_docker.git

3、修改mysql配置(config/mysq.ini)

[mysql]
host=127.0.0.1
user=root
pwd=root
db=docker_demo

4、导入sql文件

source init.sql

5、启动

python3 main.py

流程图

image


文件目录说明

├── api                                        # 接口目录
│   ├── Machine.py
│   ├── MysqlMachine.py
│   └── RedisMachine.py
├── common                                     # 公共库
│   ├── Auth.py                                # 鉴权类
│   ├── MysqlHandle.py                         # mysql操作类
├── config                                     # 配置目录 
│   └── mysql.ini
|── service                                    # 逻辑层
|    └── DockerSvr.py
├── init.sql                                   # 初始化sql
├── main.py                                    # 入口文件

项目特性

  1. 自动生成端口、端口冲突检测
  2. 自动生成账号密码
  3. 并发安全创建实例
  4. 代码分层,架构清晰

接口文档


接口鉴权

每个请求需要在header带上Authorization信息,现有三个测试用户user1,user2,user3

eg.

curl -X GET -H "Authorization:user1" 127.0.0.1:5000/list/1

创建mysql实例

  • Method: POST
  • URL: /mysql
  • Headers: Content-Type:application/json
  • Body:
{
    "charset":"utf8",
    "collection":"utf8_general_ci"
}

Response

  • Body
{
    "code":200,
    "msg":"success",
    "data":{
        "host":"127.0.0.1",
        "user":"root",
        "pwd":56903401,
        "port":2001,
        "dockerId": "6aecdcdf980b"
    }
}

eg.

curl -X POST -H "Authorization:user1" -H "Content-Type:application/json" 127.0.0.1:5000/mysql -d '{"charset":"utf8"}'

创建redis实例

  • Method: POST
  • URL: /redis
  • Headers: Content-Type:application/json
  • Body:
{
    "memory":500     # 容器最大内存(M)
}

Response

  • Body
{
    "code":200,
    "msg":"success",
    "data":{
        "host":"127.0.0.1",
        "pwd":56903401,
        "port":2002,
        "dockerId": "6aecdcdf980b"
    }
}

eg.

curl -X POST -H "Authorization:user1" -H "Content-Type:application/json" 127.0.0.1:5000/redis -d '{"memory":"400"}'

获取用户的实例列表

  • Method: GET
  • URL: /list/<int:dockerType> # dockerType:mysql 1,redis 2

Response

  • Body
{
    "code":200,
    "msg":"success",
    "data":[
        {
            "pwd":71880651,
            "host":"127.0.0.1",
            "port":2006,
            "user":"root",
            "dockerId":"6aecdcdf980b"
        }
    ]
}

eg.

curl -X GET -H "Authorization:user1" 127.0.0.1:5000/list/1

获取用户单个实例详情

  • Method: GET
  • URL: /detail/<string:dockerId>

Response

  • Body
{
    "code":200,
    "msg":"success",
    "data":{
        "pwd":71880651,
        "host":"127.0.0.1",
        "port":2006,
        "user":"root",
        "dockerId":"6aecdcdf980b"
    }
}

eg.

curl -X GET -H "Authorization:user1" 127.0.0.1:5000/detail/6aecdcdf980b

代办

  1. 容器内的数据跟配置映射到宿主机,容器内只存放运行环境
  2. 使用gunicorn替换flask自带的wsgi,支持高并发
  3. 增加supervisor管理常驻进程

作者

https://github.com/ansionfor

About

基于flask web框架通过接口申请mysql、redis的docker实例

Resources

Readme
Apache-2.0 license
Activity

Stars

2 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.