Skip to main content

辅助快速创建可分布的微服务。

Project description

cloudoll 云端玩具

更新日志

2.0.0 2023-06-09

  • 优化一系列问题
  • orm 执行更优雅

0.1.6 2022-11-03

  • orm 允许更新为空数据
  • server 文件上传加入大小限制
  • smtp 出错异常处理

0.1.5 2022-09-19

  • 修复logging level 错误的问题
  • 修正默认依赖

0.1.4 2022-09-12

  • 优化orm 超时的问题
  • 增加websocket 支持

Documentation

Docs Docs Structure Config Middleware Router View Cookie and Session Upload file WebSocket Mysql Deployment

环境准备

  • 操作系统:支持 macOS,Linux,Windows
  • 运行环境:最低要求 3.6.0。

快速开始

$ mkdir cloudoll-demo && cd cloudoll-demo
$ pip3 install cloudoll
$ vi app.py

app.py 内容如下:

## /app.py
from cloudoll.web import app


if __name__ == "__main__":
    app.create().run()

编写 Controller

$ mkdir -p controllers/home
$ touch controllers/home/__init__.py
$ vi controllers/home/index.py

controllers/home/index.py 内容如下:

# /controllers/home/index.py
from cloudoll.web import get, jsons

@get('/')
async def home():
    return jsons({"name": "chuchur" ,"msg": "ok"})

运行:

$ python3 app.py
$ open http://localhost:9001

在浏览器打开 http://127.0.0.1:9001/

就能看到:

{ 
    "name": "chuchur" ,
    "msg": "ok" ,
    "timestamp": 1681993906410 
}

恭喜, 你已经成功的写好了一个 Restful API接口.

模板渲染

绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。

在本例中,我们使用 Nunjucks 来渲染

$ mkdir templates
$ vi templates/index.html

index.html 内容如下:

<!-- /templates/index.html -->

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <p>My name is {{name} }</p>
</body>
</html>

修改 /controllers/home/index.py 内容如下:

# /controllers/home/index.py
from cloudoll.web import get, view

@get('/')
async def home():
    data = {"name": "chuchur" ,"msg": "ok"}
    return view("index.html",data)

这时 页面正常渲染 ,可以看到 “My name is chuchur”

恭喜, 你已经成功的写好了一个视图页面.

静态资源

我们想在模版里面嵌入静态资源,如图片,js ,css , 这个时候就得用到静态资源. 我们把这些js ,css ,image 都放到 static 目录

线上环境建议部署到 CDN,或者使用 nginx 等相关服务器

$ mkdir -p static/img
$ mkdir -p static/js
$ mkdir -p static/css

img目录 放入在张图 名logo.png

js 目录新建 index.js ,内容如下:

点击页面 弹出 "hello world"

// /static/js/index.js
document.addEventListener('DOMContentLoaded',function(){
    document.body.addEventListener('click',function(){
        alert('hello world.')
    })
})

css 目录新建 index.css ,内容如下:

 /* /static/css/index.css */
html,body {
    width: 100%;
    height: 100%;
    color: red;
}

修改视图 /templates/index.html ,在 head 引入 静态资源, 内容如下:

<!-- /templates/index.html -->
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/png" href="/static/img/logo.png"/>
    <link rel="stylesheet" href="/static/css/index.css">
    <script src="/static/js/index.js"></script>
    <title>Home</title>
</head>
<body>
    <p>My name is {{name} }</p>
</body>
</html>

我们新建一个配置文件, 在配置文件里面配置静态 资源.

$ mkdir config
$ vi config/conf.local.yaml

/config/conf.local.yaml 内容如下:

server:
  static:
    prefix: /static

刷新页面之后, 我们所改动即可呈现.

编写 Middleware

假设有个需求:我们的新闻站点,禁止百度爬虫访问。

所以可以通过 Middleware 判断 User-Agent,如下:

$ mkdir middlewares
$ vi middlewares/robot.py

修改 middlewares/robot.py, 内容如下:

# /middlewares/robot.py
from cloudoll.web import middleware, render
import re

@middleware()
def mid_robot():
    async def robot(request, handler):
        ua = request.headers.get('user-agent')
        if re.match('Baiduspider', ua):
            return render(status=403, text="Go away , robot.")
        return await handler(request)

    return robot

重新启动之后, 现在可以使用 curl http://localhost:7001/news -A "Baiduspider" 看看效果。

更多参见中间件文档。

配置文件

写业务的时候,不可避免的需要有配置文件,使用代码管理配置,在代码中添加多个环境的配置,在启动时传入当前环境的参数即可.

cloudoll 支持根据环境来加载配置,定义多个环境的配置文件

config
|- conf.local.yaml
|- conf.prod.yaml
`- conf.test.yaml

我们创建配置文件:

$ mkdir -p config/conf.local.yaml
$ vi config/conf.local.yaml

如下是 mysql 和 server 的配置:

server:
  host: 192.168.0.1
  port: 9001
  static: false
  client_max_size: 1024000
  static: 
    prefix: /static
    show_index: true
    append_version: true
    follow_symlinks: true
mysql:
  host: 127.0.0.1
  port: 3306
  user: root
  password: abcd
  db: blog
  charset: utf8mb4

默认开发会使用默认的local作为配置。 启动时 通过 env 加载对应的配置。 如 python3 app.py --env=prod 会加载 conf.prod.yaml

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudoll-2.0.0.tar.gz (28.8 kB view details)

Uploaded Source

Built Distribution

cloudoll-2.0.0-py2.py3-none-any.whl (28.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cloudoll-2.0.0.tar.gz.

File metadata

  • Download URL: cloudoll-2.0.0.tar.gz
  • Upload date:
  • Size: 28.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for cloudoll-2.0.0.tar.gz
Algorithm Hash digest
SHA256 e58b1ff65c216a448aefa9a1fea51d7e2b158026cfe576416aff9cac023de385
MD5 6d797059a7ec2cd1de536f3c703c275f
BLAKE2b-256 3414ab6b0d5aedb198f9cc6c7c30e1c4b61cf8f9900a2a154604b812d72ed50f

See more details on using hashes here.

File details

Details for the file cloudoll-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cloudoll-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for cloudoll-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8b61675d8f3cf771faa2b3961da0323c4d6022dc466a01f4d0aeb847a158dde1
MD5 233855d934cf16b880e4d592422bc6af
BLAKE2b-256 88b38fefc1603006da0ce47db9d229bd0be87cf8dd0fae67dc2557df4827351d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page