Skip to main content

No project description provided

Project description

lika

✨ 简易 Python ASGI Web 框架 ✨

python license

💿 安装

使用 pip 安装已发布的最新版本

pip install lika

可以克隆 Git 仓库后手动安装

git clone https://github.com/KarisAya/lika.git

或者选择任意你喜欢的方式

🎉 使用

使用 uvicorn 运行服务

import uvicorn
from lika.server import Server
if __name__ == "__main__":
    server = Server()
    uvicorn.run(server, host="127.0.0.1", port=8080)

这样你就已经运行了一个 Web 服务器

添加路由

当然你的 web 服务器里面要有内容 下面是一个示例。添加一个路由返回随机图。

import uvicorn
from pathlib import Path
from lika.server import Server
from lika.response import Response, Headers

server = Server()
root = server.router_map

# root是你的web服务器根目录路由图

image_src=list(Path("./src/image").iterdir())
@root.router("/image")
async def _(scope, receive):
    image = random.choice(image_src)
    with open(image, "rb") as f:
        return Response(200, Headers.from_ext(image.suffix), [f.read()])

# /重定向到/image
root.redirect(301, "/", "/image/")

使用子目录

# 添加子目录
hello = root.set_map("/hello")
@hello.router("/world")
async def _(scope, receive):
    return Response(200, [(b"Content-type", b"text/plain")], [b"hello world"])

等效于

@root.router("/hello/world")
async def _(scope, receive):
    return Response(200, [(b"Content-type", b"text/plain")], [b"hello world"])

hello = root.get_map("/hello") # 如果在@root.router之前执行这行代码会导致 hello == None

使用本地资源

你可以添加一些本地资源

root.directory("./src", True)

src_path Path,本地资源路径

html bool,访问文件夹路径是否视为访问文件夹下 index.html 文件

for_router set,此内扩展名的文件在每次访问时读取,其他文件放进内存

for_response set,此内扩展名的文件存放进内存,其他在每次访问时读取

for_routerfor_response二选一

在根目录添加你在本地./src目录的资源

假设这是你的文件结构

  favicon.ico
├─image
│      07c438ee01fc3bbeb21a116f2ad1e440.png
│      10de76b884dde180b52b20bc198f9851.jpeg
├─index
│      bundle.js
│      index.html
└─home
        bundle.js
        index.html

访问 /image/07c438ee01fc3bbeb21a116f2ad1e440.png 你将会看到这张图片。

(即便你已经把 /image/ 做成了随机图。)

ps:

访问 http://127.0.0.1:8080/index/

你将会看到 /index/index.html

除非你用 root.mount("./src", False)

地址占位符

形如"{id}"的地址占位符

@root.router("/test/{code}/{other}")
async def _(scope, receive, code:str, other:str):
    return Response(
        int(code),
        [(b"Content-type", b"text/plain")],
        [other.encode()],
        )

现在你可以正常访问 /test/418/hello 或者 /test/200/world

请不要访问/test/hello/world,因为你不能 int("hello")

路由图

server = Server()
root = server.router_map
test = root.set_map("/test")

root 实际上是服务器根路径 "/" 的路由图(RouterMap)

test 实际上也是服务器路径 "/test" 的路由图

路由图(RouterMap)并不依托于服务器,你也可以先有 RouterMap,再把 RouterMap 添加到服务器上

import uvicorn
from lika.server import Server
from lika.router import RouterMap
from lika.response import Response

router_map = RouterMap()  # 创建一个路由图
router_map.directory("./src", True)  # 向路由图里面添加本地资源

# 向路由图里面添加响应
@router_map.router("/test")
async def _(scope, receive):
    # Do ...
    return Response(200, [(b"Content-type", b"text/plain")], [b"hello world"])

# 使用路由图的子路由图
hello = router_map.set_map("/hello")

@hello.router("/world")
async def _(scope, receive):
    # Do ...
    return Response(200, [(b"Content-type", b"text/plain")], [b"hello world"])

new_rtmp = RouterMap()  # 创建一个新的路由图
new_rtmp.set_map("/index", router_map)  # 把整个router_map放new_rtmp的/index/目录下

if __name__ == "__main__":
    server = Server()
    server.router_map.set_map("/index", new_rtmp)  # 把整个new_rtmp放进服务器根路由图的/index/目录下
    print(server.router_map)  # 了解一下发生了什么(
    uvicorn.run(server, host="127.0.0.1", port=8080)

📖 介绍

📝 更新日志

Project details


Download files

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

Source Distribution

lika-0.1.1.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

lika-0.1.1-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

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