一个微型web框架
klar的Python项目详细描述
#klar
一个为好玩而构建的微型web框架
*参数注释
*jsonschema integration
``py
来自klar import app
app=app()
@app.get('/hello/<;name>;')
def hello(name:str,times:int=1):
返回"hello"*times+name
```
times=2'
hello klar
```
"additionalproperties":false,
}
@app.post('/product')
def create(body:product,db):
db.products.insert(body)
return{"ok":true}
````
schemas可以也应该从json或yaml文件导入
`````
````--app.py
`--schemas.json
`````
`````
{…}
}
````
``python
from schemas import product
@app.post('/product')
def create(body:product):
pass
`````
get_db_connection():
conn=somedb(url="localhost:3349")
return conn
import redis
app.provide('cache',(redis.redis,{'host':'localhost')
````
')
def get_article(article廑id:int,db,cache):
传递
````
预定义组件:
*会话
*cookie
*路由器
rest
``python
来自资源导入产品,目录
app=app()
app.resources(产品,目录,前缀="/v1")
db):
返回db.products.insert(body)
shift=10&limit=10
定义查询(shift:int,limit:int,db):
返回db.products.find().skip(shift).limit(shift)
curl-x补丁$host/v1/product/$id-d@body
定义修改(body:product,product-id:str):
返回db.products.update({u id:product},{'$set':body})
curl-x放入$host/v1/product/$id-d@body
def replace(body:product,product-id:str):
返回db.products.update({u id:product-id},body)
curl-x delete$host/v1/product/$id
def destroy(product_id:str):
返回db.products.delete({u id:product_id})
`````
db.products.update({u id:product嫒id},{'$inc':{'likes':1})
curl-x补丁$host/v1/product/$id/like
``````
res):
打印('%s not found'%req.path)
res.body="%s not found on this server"%req.path
`````
{{'$inc'{'loginccount':1}})
`````
```python
``````python
def login(emitter):
emitter.emit('user-login,user id=id)
`````
``后处理
```python
````python
`````````````
>res.body="%s(%s)%(callback,json.dumps(body))
res.headers["content type"]="应用程序/javascript"
@app.get('/resource')->;jsonp:
返回{"key":"value"}
`````
``python
@app.get('/resource')->;(jsonp,etag):
return{"key":"value"}
````
templates.home:
return{"key":"value"}
```
`templates.home`接受一个可选的dict作为参数
它基本上等同于:
``python
@app.get('/'):
return templates.home({"key":"value"}
````
` pip安装pystache`
使用`.mustache`作为扩展名
`````
--模板
--home.mustache
`````
``python
``导入模板.home
```
`
会话
会话取决于"缓存",但是klar没有内置
{'host':'localhost'})
````
``python
@app.post('/login')
def login(body,session):
``检查body.username和body.password
如果找到用户:
session.set('userid',,userid)
@app.post('/login')
def logout(session):
session.destroy()
@app.get('/admin')
def admin(session):
if session.get('userid'):
pass
`````
cookies
``python
cookies.get(key,默认值)
cookies.set(key,值)
cookies.delete(key)
cookies.set(key,value,httponly=true)
cookies.set\u 30天(key,值)
```
'path/to/public/dir')
```
27017
}
````
``python
from pymongo import mongoclient
@app.provide('db')
def db(config):
返回mongoclient(**config.mongo)
````
')
def user(id:str):
pass
````
get a link to previous handler
````
def another_handler(router):
id=3221)
```
`
一个为好玩而构建的微型web框架
*参数注释
*jsonschema integration
``py
来自klar import app
app=app()
@app.get('/hello/<;name>;')
def hello(name:str,times:int=1):
返回"hello"*times+name
```
times=2'
hello klar
```
"additionalproperties":false,
}
@app.post('/product')
def create(body:product,db):
db.products.insert(body)
return{"ok":true}
````
schemas可以也应该从json或yaml文件导入
`````
````--app.py
`--schemas.json
`````
`````
{…}
}
````
``python
from schemas import product
@app.post('/product')
def create(body:product):
pass
`````
get_db_connection():
conn=somedb(url="localhost:3349")
return conn
import redis
app.provide('cache',(redis.redis,{'host':'localhost')
````
')
def get_article(article廑id:int,db,cache):
传递
````
预定义组件:
*cookie
*路由器
rest
``python
来自资源导入产品,目录
app=app()
app.resources(产品,目录,前缀="/v1")
db):
返回db.products.insert(body)
shift=10&limit=10
定义查询(shift:int,limit:int,db):
返回db.products.find().skip(shift).limit(shift)
curl-x补丁$host/v1/product/$id-d@body
定义修改(body:product,product-id:str):
返回db.products.update({u id:product},{'$set':body})
curl-x放入$host/v1/product/$id-d@body
def replace(body:product,product-id:str):
返回db.products.update({u id:product-id},body)
curl-x delete$host/v1/product/$id
def destroy(product_id:str):
返回db.products.delete({u id:product_id})
`````
db.products.update({u id:product嫒id},{'$inc':{'likes':1})
curl-x补丁$host/v1/product/$id/like
``````
res):
打印('%s not found'%req.path)
res.body="%s not found on this server"%req.path
`````
{{'$inc'{'loginccount':1}})
`````
```python
``````python
def login(emitter):
emitter.emit('user-login,user id=id)
`````
``后处理
```python
````python
`````````````
>res.body="%s(%s)%(callback,json.dumps(body))
res.headers["content type"]="应用程序/javascript"
@app.get('/resource')->;jsonp:
返回{"key":"value"}
`````
``python
@app.get('/resource')->;(jsonp,etag):
return{"key":"value"}
````
templates.home:
return{"key":"value"}
```
`templates.home`接受一个可选的dict作为参数
它基本上等同于:
``python
@app.get('/'):
return templates.home({"key":"value"}
````
` pip安装pystache`
使用`.mustache`作为扩展名
`````
--模板
--home.mustache
`````
``python
``导入模板.home
```
`
会话
会话取决于"缓存",但是klar没有内置
{'host':'localhost'})
````
``python
@app.post('/login')
def login(body,session):
``检查body.username和body.password
如果找到用户:
session.set('userid',,userid)
@app.post('/login')
def logout(session):
session.destroy()
@app.get('/admin')
def admin(session):
if session.get('userid'):
pass
`````
cookies
``python
cookies.get(key,默认值)
cookies.set(key,值)
cookies.delete(key)
cookies.set(key,value,httponly=true)
cookies.set\u 30天(key,值)
```
'path/to/public/dir')
```
27017
}
````
``python
from pymongo import mongoclient
@app.provide('db')
def db(config):
返回mongoclient(**config.mongo)
````
')
def user(id:str):
pass
````
get a link to previous handler
````
def another_handler(router):
id=3221)
```
`