cloudoll 2.3.2__tar.gz → 3.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cloudoll-2.3.2 → cloudoll-3.0.0}/LICENSE +1 -1
- cloudoll-3.0.0/PKG-INFO +372 -0
- cloudoll-3.0.0/README.md +336 -0
- cloudoll-3.0.0/cloudoll/__init__.py +1 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/__main__.py +1 -1
- cloudoll-3.0.0/cloudoll/cli.py +156 -0
- cloudoll-3.0.0/cloudoll/clitool/cli_main.py +134 -0
- {cloudoll-2.3.2/cloudoll/utils → cloudoll-3.0.0/cloudoll/clitool}/m2d.py +25 -24
- cloudoll-3.0.0/cloudoll/clitool/process.py +242 -0
- cloudoll-3.0.0/cloudoll/clitool/spinner.py +12 -0
- {cloudoll-2.3.2/cloudoll/utils → cloudoll-3.0.0/cloudoll/clitool}/watch.py +26 -14
- cloudoll-3.0.0/cloudoll/logging/__init__.py +142 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/mail/smtp.py +12 -10
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/__init__.py +18 -24
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/base.py +1 -1
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/model.py +5 -5
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/mysql.py +4 -4
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/parse.py +1 -1
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/postgres.py +18 -19
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/utils/common.py +9 -3
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/__init__.py +4 -4
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/core.py +86 -71
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/html.py +4 -4
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/jwt.py +6 -5
- cloudoll-3.0.0/cloudoll/web/requests.py +161 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/settings.py +4 -4
- cloudoll-3.0.0/cloudoll.egg-info/PKG-INFO +372 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll.egg-info/SOURCES.txt +10 -11
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll.egg-info/requires.txt +3 -1
- cloudoll-3.0.0/pyproject.toml +46 -0
- cloudoll-3.0.0/setup.cfg +22 -0
- cloudoll-3.0.0/setup.py +4 -0
- cloudoll-2.3.2/PKG-INFO +0 -415
- cloudoll-2.3.2/README.md +0 -371
- cloudoll-2.3.2/cloudoll/__init__.py +0 -1
- cloudoll-2.3.2/cloudoll/cli.py +0 -76
- cloudoll-2.3.2/cloudoll/error/errors.py +0 -48
- cloudoll-2.3.2/cloudoll/logging/__init__.py +0 -188
- cloudoll-2.3.2/cloudoll/robot/dingtalk.py +0 -209
- cloudoll-2.3.2/cloudoll/robot/feishu.py +0 -102
- cloudoll-2.3.2/cloudoll/robot/slack.py +0 -115
- cloudoll-2.3.2/cloudoll/utils/cli_main.py +0 -72
- cloudoll-2.3.2/cloudoll/utils/patch_loop.py +0 -222
- cloudoll-2.3.2/cloudoll/web/http.py +0 -79
- cloudoll-2.3.2/cloudoll.egg-info/PKG-INFO +0 -415
- cloudoll-2.3.2/setup.cfg +0 -4
- cloudoll-2.3.2/setup.py +0 -151
- {cloudoll-2.3.2/cloudoll/error → cloudoll-3.0.0/cloudoll/clitool}/__init__.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/mail/__init__.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/err.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/orm/field.py +0 -0
- {cloudoll-2.3.2/cloudoll/robot → cloudoll-3.0.0/cloudoll/template}/__init__.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/utils/__init__.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/utils/snowflake.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll/web/exception.py +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll.egg-info/dependency_links.txt +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll.egg-info/entry_points.txt +0 -0
- {cloudoll-2.3.2 → cloudoll-3.0.0}/cloudoll.egg-info/top_level.txt +0 -0
cloudoll-3.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cloudoll
|
|
3
|
+
Version: 3.0.0
|
|
4
|
+
Summary: Assist in quickly creating distributable microservices based on aiohttp, with a focus on simplicity and ease of use.
|
|
5
|
+
Home-page: https://gitee.com/chuchur/cloudoll-py
|
|
6
|
+
Author: Qiu
|
|
7
|
+
Author-email: Qiu <smallerqiu@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Project-URL: Homepage, https://gitee.com/chuchur/cloudoll-py
|
|
10
|
+
Project-URL: Repository, https://gitee.com/chuchur/cloudoll-py
|
|
11
|
+
Keywords: Microservices,aiohttp,cloudoll
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
15
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
16
|
+
Requires-Python: >=3.6.0
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: watchfiles
|
|
20
|
+
Requires-Dist: colorlog
|
|
21
|
+
Requires-Dist: aiomysql
|
|
22
|
+
Requires-Dist: aiopg
|
|
23
|
+
Requires-Dist: aiohttp
|
|
24
|
+
Requires-Dist: redis[hiredis]
|
|
25
|
+
Requires-Dist: aiomcache
|
|
26
|
+
Requires-Dist: jinja2
|
|
27
|
+
Requires-Dist: pyjwt
|
|
28
|
+
Requires-Dist: aiohttp_session[secure]
|
|
29
|
+
Requires-Dist: PyYAML
|
|
30
|
+
Requires-Dist: click
|
|
31
|
+
Requires-Dist: envyaml
|
|
32
|
+
Requires-Dist: psutil
|
|
33
|
+
Requires-Dist: tabulate
|
|
34
|
+
Requires-Dist: concurrent_log_handler
|
|
35
|
+
Dynamic: license-file
|
|
36
|
+
|
|
37
|
+
# 🔥🔥🔥 Cloudoll
|
|
38
|
+
Quickly create web applications based on Python.
|
|
39
|
+
|
|
40
|
+
## Documentation
|
|
41
|
+
|
|
42
|
+
[Docs](https://cloudoll.chuchur.com)
|
|
43
|
+
|
|
44
|
+
[Structure](https://cloudoll.chuchur.com/structure)
|
|
45
|
+
|
|
46
|
+
[Config](https://cloudoll.chuchur.com/config)
|
|
47
|
+
|
|
48
|
+
[Middleware](https://cloudoll.chuchur.com/middleware)
|
|
49
|
+
|
|
50
|
+
[Router](https://cloudoll.chuchur.com/router)
|
|
51
|
+
|
|
52
|
+
[View](https://cloudoll.chuchur.com/view)
|
|
53
|
+
|
|
54
|
+
[Cookie and Session](https://cloudoll.chuchur.com/session-cookie)
|
|
55
|
+
|
|
56
|
+
[Upload file](https://cloudoll.chuchur.com/file-uploads)
|
|
57
|
+
|
|
58
|
+
[WebSocket](https://cloudoll.chuchur.com/websockets)
|
|
59
|
+
|
|
60
|
+
[Mysql](https://cloudoll.chuchur.com/mysql)
|
|
61
|
+
|
|
62
|
+
[Deployment](https://cloudoll.chuchur.com/deployment)
|
|
63
|
+
|
|
64
|
+
## Install
|
|
65
|
+
```sh
|
|
66
|
+
pip install cloudoll
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Environment
|
|
71
|
+
|
|
72
|
+
- Operating System: Supports macOS, Linux, Windows
|
|
73
|
+
- Runtime Environment: Minimum requirement 3.6.0.
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
Now you can use `cloudoll create myapp` to create a new project, 3 steps to start:
|
|
78
|
+
|
|
79
|
+
1. create a new project
|
|
80
|
+
```sh
|
|
81
|
+
$ cloudoll create myapp
|
|
82
|
+
```
|
|
83
|
+
2. cd to project directory
|
|
84
|
+
```sh
|
|
85
|
+
$ cd myapp
|
|
86
|
+
```
|
|
87
|
+
3. start dev server
|
|
88
|
+
```sh
|
|
89
|
+
cloudoll start -n myapp
|
|
90
|
+
```
|
|
91
|
+
You can also manually create a project step by step.
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
$ mkdir cloudoll-demo && cd cloudoll-demo
|
|
95
|
+
$ pip3 install cloudoll
|
|
96
|
+
$ vi app.py
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`app.py` content:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
## /app.py
|
|
103
|
+
from cloudoll.web import app
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if __name__ == "__main__":
|
|
107
|
+
app.create().run()
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Controller
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
$ mkdir -p controllers/home
|
|
114
|
+
$ touch controllers/home/__init__.py
|
|
115
|
+
$ vi controllers/home/index.py
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`controllers/home/index.py` content:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
# /controllers/home/index.py
|
|
122
|
+
from cloudoll.web import get
|
|
123
|
+
|
|
124
|
+
@get('/')
|
|
125
|
+
async def home():
|
|
126
|
+
return {"name": "cloudoll" ,"msg": "ok"}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
run:
|
|
130
|
+
```sh
|
|
131
|
+
$ python3 app.py
|
|
132
|
+
$ open http://localhost:9001
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Open in browser [http://127.0.0.1:9001/](http://127.0.0.1:9001/)
|
|
136
|
+
|
|
137
|
+
you can see the result:
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"name": "cloudoll" ,
|
|
142
|
+
"msg": "ok" ,
|
|
143
|
+
"timestamp": 1681993906410
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Congratulations, you have successfully written one `Restful API` with `cloudoll`.
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
### Template rendering
|
|
151
|
+
|
|
152
|
+
In most cases, we need to read the data, render the template, and then present it to the user. Therefore, we need to introduce the corresponding template engine.
|
|
153
|
+
|
|
154
|
+
in this demo,we using [Nunjucks](https://mozilla.github.io/nunjucks/) to render template.
|
|
155
|
+
```sh
|
|
156
|
+
$ mkdir templates
|
|
157
|
+
$ vi templates/index.html
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
`index.html` contents:
|
|
161
|
+
|
|
162
|
+
```html
|
|
163
|
+
<!-- /templates/index.html -->
|
|
164
|
+
|
|
165
|
+
<!DOCTYPE html>
|
|
166
|
+
<html lang="zh">
|
|
167
|
+
<head>
|
|
168
|
+
<meta charset="UTF-8">
|
|
169
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
170
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
171
|
+
<title>Home</title>
|
|
172
|
+
</head>
|
|
173
|
+
<body>
|
|
174
|
+
<p>Hello {{name}}</p>
|
|
175
|
+
</body>
|
|
176
|
+
</html>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
edit `/controllers/home/index.py` contents:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
# /controllers/home/index.py
|
|
184
|
+
from cloudoll.web import get, render_view
|
|
185
|
+
|
|
186
|
+
@get('/')
|
|
187
|
+
async def home():
|
|
188
|
+
data = {"name": "cloudoll" ,"msg": "ok"}
|
|
189
|
+
return render_view("index.html",data)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
at this time ,we can see `“Hello cloudoll.`
|
|
193
|
+
|
|
194
|
+
ok , we wrotten a view page.
|
|
195
|
+
|
|
196
|
+
### static files
|
|
197
|
+
|
|
198
|
+
We want to embed static resources such as images, JS, and CSS in the template, which requires the use of static resources. We place these `js`, `css`, and `image` files in the `static` directory.
|
|
199
|
+
|
|
200
|
+
For online environments, it is recommended to deploy to a CDN or use servers like `nginx`.
|
|
201
|
+
|
|
202
|
+
```sh
|
|
203
|
+
$ mkdir -p static/img
|
|
204
|
+
$ mkdir -p static/js
|
|
205
|
+
$ mkdir -p static/css
|
|
206
|
+
```
|
|
207
|
+
in `img` directory ,we can put images resources.
|
|
208
|
+
|
|
209
|
+
make a new js file `index.js` ,contents:
|
|
210
|
+
|
|
211
|
+
clike the body , we can see the alert tip "hello world"
|
|
212
|
+
```js
|
|
213
|
+
// /static/js/index.js
|
|
214
|
+
document.addEventListener('DOMContentLoaded',function(){
|
|
215
|
+
document.body.addEventListener('click',function(){
|
|
216
|
+
alert('hello cloudoll.')
|
|
217
|
+
})
|
|
218
|
+
})
|
|
219
|
+
```
|
|
220
|
+
make a new css file `index.css` ,contents:
|
|
221
|
+
```css
|
|
222
|
+
/* /static/css/index.css */
|
|
223
|
+
html,
|
|
224
|
+
body {
|
|
225
|
+
width: 100%;
|
|
226
|
+
height: 100%;
|
|
227
|
+
color: rgb(0, 229, 255);
|
|
228
|
+
margin: 0;
|
|
229
|
+
padding: 0;
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
edit the view page `/templates/index.html` ,in `head` we import the css file, like this:
|
|
234
|
+
|
|
235
|
+
```html
|
|
236
|
+
<!-- /templates/index.html -->
|
|
237
|
+
<!DOCTYPE html>
|
|
238
|
+
<html lang="zh">
|
|
239
|
+
<head>
|
|
240
|
+
<meta charset="UTF-8">
|
|
241
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
242
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
243
|
+
<link rel="icon" type="image/png" href="/static/img/logo.png"/>
|
|
244
|
+
<link rel="stylesheet" href="/static/css/index.css">
|
|
245
|
+
<script src="/static/js/index.js"></script>
|
|
246
|
+
<title>Home</title>
|
|
247
|
+
</head>
|
|
248
|
+
<body>
|
|
249
|
+
<p>My name is {{name}}</p>
|
|
250
|
+
</body>
|
|
251
|
+
</html>
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
we create a config file, and config static resource.
|
|
255
|
+
```sh
|
|
256
|
+
$ mkdir config
|
|
257
|
+
$ vi config/conf.local.yaml
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
`/config/conf.local.yaml` contents:
|
|
261
|
+
|
|
262
|
+
```yaml
|
|
263
|
+
server:
|
|
264
|
+
static:
|
|
265
|
+
prefix: /static
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
after reload the page , our changes will be reflected.
|
|
269
|
+
|
|
270
|
+
### Middleware
|
|
271
|
+
|
|
272
|
+
Suppose there is a requirement: our news site prohibits access by Baidu crawlers.
|
|
273
|
+
|
|
274
|
+
so we need to write a middleware to check User-Agent. like this:
|
|
275
|
+
|
|
276
|
+
```sh
|
|
277
|
+
$ mkdir middlewares
|
|
278
|
+
$ vi middlewares/robot.py
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
edit `middlewares/robot.py`, contents:
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
# /middlewares/robot.py
|
|
285
|
+
from cloudoll.web import middleware, render_json
|
|
286
|
+
import re
|
|
287
|
+
|
|
288
|
+
@middleware()
|
|
289
|
+
def mid_robot():
|
|
290
|
+
async def robot(request, handler):
|
|
291
|
+
ua = request.headers.get('user-agent')
|
|
292
|
+
if re.match('Baiduspider', ua):
|
|
293
|
+
return render_json(status=403, text="Go away , robot.")
|
|
294
|
+
return await handler(request)
|
|
295
|
+
|
|
296
|
+
return robot
|
|
297
|
+
```
|
|
298
|
+
after restart the server, you can use `curl http://localhost:9001/news -A "Baiduspider"` to see the effect.
|
|
299
|
+
we can see more information in [Middleware](https://cloudoll.chuchur.com/middleware)
|
|
300
|
+
|
|
301
|
+
### Configuration
|
|
302
|
+
When writing business logic, it is inevitable to have configuration files. Managing configurations through code involves adding configurations for multiple environments within the code, and passing the parameter of the current environment during startup.
|
|
303
|
+
|
|
304
|
+
cloudoll support loading configurations based on the environment, defining configuration files for multiple environments
|
|
305
|
+
|
|
306
|
+
```ini
|
|
307
|
+
config
|
|
308
|
+
|- conf.local.yaml
|
|
309
|
+
|- conf.prod.yaml
|
|
310
|
+
`- conf.test.yaml
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
now, we create a config file:
|
|
314
|
+
```sh
|
|
315
|
+
$ mkdir -p config/conf.local.yaml
|
|
316
|
+
$ vi config/conf.local.yaml
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
the flollowing is mysql and server's configuration:
|
|
320
|
+
```yaml
|
|
321
|
+
server:
|
|
322
|
+
host: 192.168.0.1
|
|
323
|
+
port: 9001
|
|
324
|
+
static: false
|
|
325
|
+
client_max_size: 1024000
|
|
326
|
+
static:
|
|
327
|
+
prefix: /static
|
|
328
|
+
show_index: true
|
|
329
|
+
append_version: true
|
|
330
|
+
follow_symlinks: true
|
|
331
|
+
database:
|
|
332
|
+
mysql:
|
|
333
|
+
host: 127.0.0.1
|
|
334
|
+
port: 3306
|
|
335
|
+
user: root
|
|
336
|
+
password: abcd
|
|
337
|
+
db: blog
|
|
338
|
+
charset: utf8mb4
|
|
339
|
+
```
|
|
340
|
+
the `local` will be used as default configuration, when you start your application, it will load the `local` configuration. like `cloudoll start -n myapp -env local` will load the `conf.local.yaml` configuration.
|
|
341
|
+
|
|
342
|
+
# cli
|
|
343
|
+
|
|
344
|
+
## Create model
|
|
345
|
+
|
|
346
|
+
export model from database
|
|
347
|
+
```sh
|
|
348
|
+
cloudoll gen -t users
|
|
349
|
+
```
|
|
350
|
+
More parameters:
|
|
351
|
+
- -p (--path) the path to save the model
|
|
352
|
+
- -c (--create) to create model or create tables, default is create model
|
|
353
|
+
- -t (--table) The model or table name to be generated, separated by `,`, `ALL` for all tables
|
|
354
|
+
- -env (--environment) to load the configuration file , default is `local`
|
|
355
|
+
- -db (--database) Database instance name, depends on the configuration file, if there are multiple databases
|
|
356
|
+
- -h (--help) help
|
|
357
|
+
|
|
358
|
+
## Development and debugging
|
|
359
|
+
|
|
360
|
+
```sh
|
|
361
|
+
cloudoll start --name myapp -env local -m development
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## Production Environment
|
|
365
|
+
|
|
366
|
+
```sh
|
|
367
|
+
cloudoll start --name myapp -env prod --mode production
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
you can use `clodoll stop myapp` to stop your application ,
|
|
371
|
+
or use `cloudoll restart myapp` to restart your application.
|
|
372
|
+
`cloudoll list` to see all your applications.
|
cloudoll-3.0.0/README.md
ADDED
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
# 🔥🔥🔥 Cloudoll
|
|
2
|
+
Quickly create web applications based on Python.
|
|
3
|
+
|
|
4
|
+
## Documentation
|
|
5
|
+
|
|
6
|
+
[Docs](https://cloudoll.chuchur.com)
|
|
7
|
+
|
|
8
|
+
[Structure](https://cloudoll.chuchur.com/structure)
|
|
9
|
+
|
|
10
|
+
[Config](https://cloudoll.chuchur.com/config)
|
|
11
|
+
|
|
12
|
+
[Middleware](https://cloudoll.chuchur.com/middleware)
|
|
13
|
+
|
|
14
|
+
[Router](https://cloudoll.chuchur.com/router)
|
|
15
|
+
|
|
16
|
+
[View](https://cloudoll.chuchur.com/view)
|
|
17
|
+
|
|
18
|
+
[Cookie and Session](https://cloudoll.chuchur.com/session-cookie)
|
|
19
|
+
|
|
20
|
+
[Upload file](https://cloudoll.chuchur.com/file-uploads)
|
|
21
|
+
|
|
22
|
+
[WebSocket](https://cloudoll.chuchur.com/websockets)
|
|
23
|
+
|
|
24
|
+
[Mysql](https://cloudoll.chuchur.com/mysql)
|
|
25
|
+
|
|
26
|
+
[Deployment](https://cloudoll.chuchur.com/deployment)
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
```sh
|
|
30
|
+
pip install cloudoll
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Environment
|
|
35
|
+
|
|
36
|
+
- Operating System: Supports macOS, Linux, Windows
|
|
37
|
+
- Runtime Environment: Minimum requirement 3.6.0.
|
|
38
|
+
|
|
39
|
+
## Quick Start
|
|
40
|
+
|
|
41
|
+
Now you can use `cloudoll create myapp` to create a new project, 3 steps to start:
|
|
42
|
+
|
|
43
|
+
1. create a new project
|
|
44
|
+
```sh
|
|
45
|
+
$ cloudoll create myapp
|
|
46
|
+
```
|
|
47
|
+
2. cd to project directory
|
|
48
|
+
```sh
|
|
49
|
+
$ cd myapp
|
|
50
|
+
```
|
|
51
|
+
3. start dev server
|
|
52
|
+
```sh
|
|
53
|
+
cloudoll start -n myapp
|
|
54
|
+
```
|
|
55
|
+
You can also manually create a project step by step.
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
$ mkdir cloudoll-demo && cd cloudoll-demo
|
|
59
|
+
$ pip3 install cloudoll
|
|
60
|
+
$ vi app.py
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`app.py` content:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
## /app.py
|
|
67
|
+
from cloudoll.web import app
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__":
|
|
71
|
+
app.create().run()
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Controller
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
$ mkdir -p controllers/home
|
|
78
|
+
$ touch controllers/home/__init__.py
|
|
79
|
+
$ vi controllers/home/index.py
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`controllers/home/index.py` content:
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
# /controllers/home/index.py
|
|
86
|
+
from cloudoll.web import get
|
|
87
|
+
|
|
88
|
+
@get('/')
|
|
89
|
+
async def home():
|
|
90
|
+
return {"name": "cloudoll" ,"msg": "ok"}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
run:
|
|
94
|
+
```sh
|
|
95
|
+
$ python3 app.py
|
|
96
|
+
$ open http://localhost:9001
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Open in browser [http://127.0.0.1:9001/](http://127.0.0.1:9001/)
|
|
100
|
+
|
|
101
|
+
you can see the result:
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"name": "cloudoll" ,
|
|
106
|
+
"msg": "ok" ,
|
|
107
|
+
"timestamp": 1681993906410
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Congratulations, you have successfully written one `Restful API` with `cloudoll`.
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
### Template rendering
|
|
115
|
+
|
|
116
|
+
In most cases, we need to read the data, render the template, and then present it to the user. Therefore, we need to introduce the corresponding template engine.
|
|
117
|
+
|
|
118
|
+
in this demo,we using [Nunjucks](https://mozilla.github.io/nunjucks/) to render template.
|
|
119
|
+
```sh
|
|
120
|
+
$ mkdir templates
|
|
121
|
+
$ vi templates/index.html
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`index.html` contents:
|
|
125
|
+
|
|
126
|
+
```html
|
|
127
|
+
<!-- /templates/index.html -->
|
|
128
|
+
|
|
129
|
+
<!DOCTYPE html>
|
|
130
|
+
<html lang="zh">
|
|
131
|
+
<head>
|
|
132
|
+
<meta charset="UTF-8">
|
|
133
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
134
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
135
|
+
<title>Home</title>
|
|
136
|
+
</head>
|
|
137
|
+
<body>
|
|
138
|
+
<p>Hello {{name}}</p>
|
|
139
|
+
</body>
|
|
140
|
+
</html>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
edit `/controllers/home/index.py` contents:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
# /controllers/home/index.py
|
|
148
|
+
from cloudoll.web import get, render_view
|
|
149
|
+
|
|
150
|
+
@get('/')
|
|
151
|
+
async def home():
|
|
152
|
+
data = {"name": "cloudoll" ,"msg": "ok"}
|
|
153
|
+
return render_view("index.html",data)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
at this time ,we can see `“Hello cloudoll.`
|
|
157
|
+
|
|
158
|
+
ok , we wrotten a view page.
|
|
159
|
+
|
|
160
|
+
### static files
|
|
161
|
+
|
|
162
|
+
We want to embed static resources such as images, JS, and CSS in the template, which requires the use of static resources. We place these `js`, `css`, and `image` files in the `static` directory.
|
|
163
|
+
|
|
164
|
+
For online environments, it is recommended to deploy to a CDN or use servers like `nginx`.
|
|
165
|
+
|
|
166
|
+
```sh
|
|
167
|
+
$ mkdir -p static/img
|
|
168
|
+
$ mkdir -p static/js
|
|
169
|
+
$ mkdir -p static/css
|
|
170
|
+
```
|
|
171
|
+
in `img` directory ,we can put images resources.
|
|
172
|
+
|
|
173
|
+
make a new js file `index.js` ,contents:
|
|
174
|
+
|
|
175
|
+
clike the body , we can see the alert tip "hello world"
|
|
176
|
+
```js
|
|
177
|
+
// /static/js/index.js
|
|
178
|
+
document.addEventListener('DOMContentLoaded',function(){
|
|
179
|
+
document.body.addEventListener('click',function(){
|
|
180
|
+
alert('hello cloudoll.')
|
|
181
|
+
})
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
make a new css file `index.css` ,contents:
|
|
185
|
+
```css
|
|
186
|
+
/* /static/css/index.css */
|
|
187
|
+
html,
|
|
188
|
+
body {
|
|
189
|
+
width: 100%;
|
|
190
|
+
height: 100%;
|
|
191
|
+
color: rgb(0, 229, 255);
|
|
192
|
+
margin: 0;
|
|
193
|
+
padding: 0;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
edit the view page `/templates/index.html` ,in `head` we import the css file, like this:
|
|
198
|
+
|
|
199
|
+
```html
|
|
200
|
+
<!-- /templates/index.html -->
|
|
201
|
+
<!DOCTYPE html>
|
|
202
|
+
<html lang="zh">
|
|
203
|
+
<head>
|
|
204
|
+
<meta charset="UTF-8">
|
|
205
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
206
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
207
|
+
<link rel="icon" type="image/png" href="/static/img/logo.png"/>
|
|
208
|
+
<link rel="stylesheet" href="/static/css/index.css">
|
|
209
|
+
<script src="/static/js/index.js"></script>
|
|
210
|
+
<title>Home</title>
|
|
211
|
+
</head>
|
|
212
|
+
<body>
|
|
213
|
+
<p>My name is {{name}}</p>
|
|
214
|
+
</body>
|
|
215
|
+
</html>
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
we create a config file, and config static resource.
|
|
219
|
+
```sh
|
|
220
|
+
$ mkdir config
|
|
221
|
+
$ vi config/conf.local.yaml
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
`/config/conf.local.yaml` contents:
|
|
225
|
+
|
|
226
|
+
```yaml
|
|
227
|
+
server:
|
|
228
|
+
static:
|
|
229
|
+
prefix: /static
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
after reload the page , our changes will be reflected.
|
|
233
|
+
|
|
234
|
+
### Middleware
|
|
235
|
+
|
|
236
|
+
Suppose there is a requirement: our news site prohibits access by Baidu crawlers.
|
|
237
|
+
|
|
238
|
+
so we need to write a middleware to check User-Agent. like this:
|
|
239
|
+
|
|
240
|
+
```sh
|
|
241
|
+
$ mkdir middlewares
|
|
242
|
+
$ vi middlewares/robot.py
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
edit `middlewares/robot.py`, contents:
|
|
246
|
+
|
|
247
|
+
```python
|
|
248
|
+
# /middlewares/robot.py
|
|
249
|
+
from cloudoll.web import middleware, render_json
|
|
250
|
+
import re
|
|
251
|
+
|
|
252
|
+
@middleware()
|
|
253
|
+
def mid_robot():
|
|
254
|
+
async def robot(request, handler):
|
|
255
|
+
ua = request.headers.get('user-agent')
|
|
256
|
+
if re.match('Baiduspider', ua):
|
|
257
|
+
return render_json(status=403, text="Go away , robot.")
|
|
258
|
+
return await handler(request)
|
|
259
|
+
|
|
260
|
+
return robot
|
|
261
|
+
```
|
|
262
|
+
after restart the server, you can use `curl http://localhost:9001/news -A "Baiduspider"` to see the effect.
|
|
263
|
+
we can see more information in [Middleware](https://cloudoll.chuchur.com/middleware)
|
|
264
|
+
|
|
265
|
+
### Configuration
|
|
266
|
+
When writing business logic, it is inevitable to have configuration files. Managing configurations through code involves adding configurations for multiple environments within the code, and passing the parameter of the current environment during startup.
|
|
267
|
+
|
|
268
|
+
cloudoll support loading configurations based on the environment, defining configuration files for multiple environments
|
|
269
|
+
|
|
270
|
+
```ini
|
|
271
|
+
config
|
|
272
|
+
|- conf.local.yaml
|
|
273
|
+
|- conf.prod.yaml
|
|
274
|
+
`- conf.test.yaml
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
now, we create a config file:
|
|
278
|
+
```sh
|
|
279
|
+
$ mkdir -p config/conf.local.yaml
|
|
280
|
+
$ vi config/conf.local.yaml
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
the flollowing is mysql and server's configuration:
|
|
284
|
+
```yaml
|
|
285
|
+
server:
|
|
286
|
+
host: 192.168.0.1
|
|
287
|
+
port: 9001
|
|
288
|
+
static: false
|
|
289
|
+
client_max_size: 1024000
|
|
290
|
+
static:
|
|
291
|
+
prefix: /static
|
|
292
|
+
show_index: true
|
|
293
|
+
append_version: true
|
|
294
|
+
follow_symlinks: true
|
|
295
|
+
database:
|
|
296
|
+
mysql:
|
|
297
|
+
host: 127.0.0.1
|
|
298
|
+
port: 3306
|
|
299
|
+
user: root
|
|
300
|
+
password: abcd
|
|
301
|
+
db: blog
|
|
302
|
+
charset: utf8mb4
|
|
303
|
+
```
|
|
304
|
+
the `local` will be used as default configuration, when you start your application, it will load the `local` configuration. like `cloudoll start -n myapp -env local` will load the `conf.local.yaml` configuration.
|
|
305
|
+
|
|
306
|
+
# cli
|
|
307
|
+
|
|
308
|
+
## Create model
|
|
309
|
+
|
|
310
|
+
export model from database
|
|
311
|
+
```sh
|
|
312
|
+
cloudoll gen -t users
|
|
313
|
+
```
|
|
314
|
+
More parameters:
|
|
315
|
+
- -p (--path) the path to save the model
|
|
316
|
+
- -c (--create) to create model or create tables, default is create model
|
|
317
|
+
- -t (--table) The model or table name to be generated, separated by `,`, `ALL` for all tables
|
|
318
|
+
- -env (--environment) to load the configuration file , default is `local`
|
|
319
|
+
- -db (--database) Database instance name, depends on the configuration file, if there are multiple databases
|
|
320
|
+
- -h (--help) help
|
|
321
|
+
|
|
322
|
+
## Development and debugging
|
|
323
|
+
|
|
324
|
+
```sh
|
|
325
|
+
cloudoll start --name myapp -env local -m development
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
## Production Environment
|
|
329
|
+
|
|
330
|
+
```sh
|
|
331
|
+
cloudoll start --name myapp -env prod --mode production
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
you can use `clodoll stop myapp` to stop your application ,
|
|
335
|
+
or use `cloudoll restart myapp` to restart your application.
|
|
336
|
+
`cloudoll list` to see all your applications.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "3.0.0"
|