-
-
Notifications
You must be signed in to change notification settings - Fork 254
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
docs_src/src/components/documentation/LanguageSelector.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useRouter } from 'next/router' | ||
import { Menu } from '@headlessui/react' | ||
import { motion } from 'framer-motion' | ||
|
||
const languages = [ | ||
{ code: 'en', name: 'English' }, | ||
{ code: 'zh', name: '中文' }, | ||
] | ||
|
||
function LanguageSelector() { | ||
const router = useRouter() | ||
const { pathname, asPath, query } = router | ||
const currentLanguage = router.locale || 'en' | ||
|
||
const changeLanguage = (locale) => { | ||
router.push({ pathname, query }, asPath, { locale }) | ||
} | ||
|
||
return ( | ||
<Menu as="div" className="relative"> | ||
<Menu.Button className="flex items-center gap-2 rounded-lg px-3 py-2 text-sm text-white hover:bg-white/10"> | ||
{languages.find(l => l.code === currentLanguage)?.name} | ||
<svg | ||
width="6" | ||
height="3" | ||
className="ml-2 stroke-current" | ||
fill="none" | ||
viewBox="0 0 6 3" | ||
> | ||
<path d="M0 0L3 3L6 0" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" /> | ||
</svg> | ||
</Menu.Button> | ||
<Menu.Items as={motion.div} | ||
initial={{ opacity: 0, y: -10 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
exit={{ opacity: 0, y: -10 }} | ||
className="absolute right-0 mt-2 w-40 origin-top-right rounded-lg bg-white/10 p-1 backdrop-blur-lg" | ||
> | ||
{languages.map((language) => ( | ||
<Menu.Item key={language.code}> | ||
{({ active }) => ( | ||
<button | ||
onClick={() => changeLanguage(language.code)} | ||
className={`${ | ||
active ? 'bg-white/10' : '' | ||
} ${ | ||
currentLanguage === language.code ? 'text-orange-500' : 'text-white' | ||
} group flex w-full items-center rounded-md px-2 py-2 text-sm`} | ||
> | ||
{language.name} | ||
</button> | ||
)} | ||
</Menu.Item> | ||
))} | ||
</Menu.Items> | ||
</Menu> | ||
) | ||
} | ||
|
||
export default LanguageSelector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
docs_src/src/pages/documentation/api_reference/zh/getting_started.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
export const description = | ||
'开始使用Robyn构建您的第一个应用程序。' | ||
|
||
# 入门指南 | ||
|
||
## 创建您的第一个Robyn应用程序 | ||
|
||
让我们从创建一个简单的Robyn应用程序开始。首先,使用pip安装Robyn: | ||
|
||
```bash | ||
pip install robyn | ||
``` | ||
|
||
然后,创建一个新的Python文件 `app.py`: | ||
|
||
```python | ||
from robyn import Robyn | ||
|
||
app = Robyn(__file__) | ||
|
||
@app.get("/") | ||
async def hello_world(): | ||
return "Hello, World!" | ||
|
||
if __name__ == "__main__": | ||
app.start() | ||
``` | ||
|
||
现在运行您的应用程序: | ||
|
||
```bash | ||
python app.py | ||
``` | ||
|
||
访问 `http://localhost:8080`,您应该能看到 "Hello, World!" 消息。 | ||
|
||
## 命令行选项 | ||
|
||
Robyn提供了多个命令行选项来配置您的应用程序: | ||
|
||
```bash | ||
python app.py --help | ||
``` | ||
|
||
输出将显示所有可用选项: | ||
|
||
```text | ||
options: | ||
-h, --help 显示帮助信息 | ||
--processes PROCESSES | ||
选择进程数量。[默认值: 1] | ||
--workers WORKERS 选择工作线程数量。[默认值: 1] | ||
--dev 开发模式。根据文件更改自动重启服务器。 | ||
--log-level LOG_LEVEL | ||
设置日志级别 | ||
--create 创建新项目模板。 | ||
--docs 打开Robyn文档。 | ||
--open-browser 成功启动时打开浏览器。 | ||
--version 显示Robyn版本。 | ||
--compile-rust-path COMPILE_RUST_PATH | ||
编译指定路径中的rust文件。 | ||
--create-rust-file CREATE_RUST_FILE | ||
创建指定名称的rust文件。 | ||
--disable-openapi 禁用OpenAPI文档。 | ||
--fast 快速模式。设置最优的进程、工作线程和日志级别。但您可以覆盖这些设置。 | ||
``` | ||
|
||
## 路由装饰器 | ||
|
||
Robyn支持所有标准的HTTP方法: | ||
|
||
```python | ||
@app.get("/users") | ||
async def get_users(): | ||
return {"users": ["user1", "user2"]} | ||
|
||
@app.post("/users") | ||
async def create_user(request): | ||
return {"message": "用户已创建"} | ||
|
||
@app.put("/users/:id") | ||
async def update_user(request): | ||
return {"message": "用户已更新"} | ||
|
||
@app.delete("/users/:id") | ||
async def delete_user(request): | ||
return {"message": "用户已删除"} | ||
``` | ||
|
||
## 请求参数 | ||
|
||
您可以通过多种方式访问请求数据: | ||
|
||
```python | ||
@app.post("/api/data") | ||
async def handle_data(request): | ||
# 访问JSON数据 | ||
json_data = request.json() | ||
|
||
# 访问查询参数 | ||
query_params = request.query_params | ||
|
||
# 访问路径参数 | ||
path_params = request.path_params | ||
|
||
# 访问请求头 | ||
headers = request.headers | ||
|
||
return { | ||
"json": json_data, | ||
"query": query_params, | ||
"path": path_params, | ||
"headers": headers | ||
} | ||
``` | ||
|
||
## 响应类型 | ||
|
||
Robyn支持多种响应类型: | ||
|
||
```python | ||
from robyn import Response, jsonify | ||
|
||
@app.get("/text") | ||
async def text_response(): | ||
return "纯文本响应" | ||
|
||
@app.get("/json") | ||
async def json_response(): | ||
return jsonify({"message": "JSON响应"}) | ||
|
||
@app.get("/custom") | ||
async def custom_response(): | ||
return Response( | ||
status_code=201, | ||
description="自定义响应", | ||
headers={"Content-Type": "text/plain"} | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const description = | ||
'欢迎来到Robyn API文档。您将找到全面的指南和文档,帮助您尽快开始使用Robyn,并在遇到困难时获得支持。' | ||
|
||
# 使用Robyn构建实际应用 | ||
|
||
蝙蝠侠被委托开发一个Web应用程序来管理哥谭市的犯罪数据。该应用程序将允许哥谭警察局存储和检索有关犯罪活动、嫌疑人及其位置的数据。他决定使用Robyn Web框架来高效快速地构建这个应用程序。 | ||
|
||
您可以在[这里](https://github.com/sparckles/example_robyn_app)找到此应用程序的源代码。 | ||
|
||
## 安装Robyn | ||
|
||
第一步是安装Robyn。蝙蝠侠创建了一个虚拟环境并使用pip安装了Robyn。 | ||
|
||
```bash | ||
$ python3 -m venv venv | ||
$ source venv/bin/activate | ||
$ pip install robyn | ||
``` | ||
|
||
## 创建Robyn应用程序 | ||
|
||
蝙蝠侠正准备创建一个`src/app.py`文件,这时他得知Robyn提供了一个CLI工具来创建新应用程序。他运行以下命令来创建一个新的Robyn应用程序: | ||
|
||
```bash | ||
$ python -m robyn --create | ||
``` | ||
|
||
这个示例应用程序将展示如何: | ||
|
||
- 设计和实现RESTful API | ||
- 处理身份验证和授权 | ||
- 使用中间件进行请求处理 | ||
- 实现实时通知 | ||
- 设置监控和日志记录 | ||
- 部署应用程序 | ||
- 生成API文档 | ||
- 组织和构建可维护的代码 |
45 changes: 45 additions & 0 deletions
45
docs_src/src/pages/documentation/example_app/zh/subrouters.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export const description = | ||
'欢迎来到Robyn API文档。您将找到全面的指南和文档,帮助您尽快开始使用Robyn,并在遇到困难时获得支持。' | ||
|
||
## 使用SubRouters组织代码 | ||
|
||
随着应用程序的增长,蝙蝠侠需要一种更好的方式来组织他的路由。他决定使用Robyn的SubRouter功能来对相关路由进行分组。 | ||
|
||
```python | ||
from robyn import SubRouter | ||
|
||
# 创建处理犯罪相关路由的子路由器 | ||
crime_router = SubRouter(__file__, prefix="/crimes") | ||
|
||
@crime_router.get("/list") | ||
def list_crimes(): | ||
return {"crimes": get_all_crimes()} | ||
|
||
@crime_router.post("/report") | ||
def report_crime(request): | ||
crime_data = request.json() | ||
return {"id": create_crime_report(crime_data)} | ||
|
||
# 创建处理嫌疑人相关路由的子路由器 | ||
suspect_router = SubRouter(__file__, prefix="/suspects") | ||
|
||
@suspect_router.get("/list") | ||
def list_suspects(): | ||
return {"suspects": get_all_suspects()} | ||
|
||
@suspect_router.get("/:id") | ||
def get_suspect(request, path_params): | ||
suspect_id = path_params.id | ||
return {"suspect": get_suspect_by_id(suspect_id)} | ||
|
||
# 将子路由器包含在主应用程序中 | ||
app.include_router(crime_router) | ||
app.include_router(suspect_router) | ||
``` | ||
|
||
SubRouters帮助在公共前缀下组织相关路由,使代码更易于维护和理解。在这个例子中: | ||
|
||
- 所有与犯罪相关的路由都在 `/crimes` 下 | ||
- 所有与嫌疑人相关的路由都在 `/suspects` 下 | ||
|
||
这种组织方式清晰地表明了哪些路由处理什么功能,并将相关代码保持在一起。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Guides } from '@/components/documentation/Guides' | ||
import { ApiDocs } from '@/components/documentation/ApiDocs' | ||
import { HeroPattern } from '@/components/documentation/HeroPattern' | ||
|
||
export const description = | ||
'欢迎来到Robyn API文档。您将找到全面的指南和文档,帮助您尽快开始使用Robyn,并在遇到困难时获得支持。' | ||
|
||
export const sections = [ | ||
{ title: '示例应用', id: 'guides' }, | ||
{ title: 'API参考', id: 'api_docs' }, | ||
] | ||
|
||
<HeroPattern /> | ||
|
||
<div className="text-white"> | ||
# API文档 | ||
欢迎来到Robyn API文档。您将找到全面的指南和文档,帮助您尽快开始使用Robyn,并在遇到困难时获得支持。 | ||
|
||
<div className="not-prose mb-16 mt-6 flex gap-3"> | ||
<Button href="/documentation/example_app" arrow="right" children="实际应用示例" /> | ||
<Button href="/documentation/api_reference" variant="outline" children="API文档" /> | ||
</div> | ||
|
||
## 入门指南 {{ anchor: false }} | ||
|
||
示例应用是一个简单的Web应用程序,展示了如何使用Robyn API。如果您是Robyn的新手,这是一个很好的起点。 | ||
|
||
API参考包含了有关Robyn API的详细信息。如果您已经熟悉Robyn并想了解更多关于API的信息,这是一个很好的起点。 | ||
|
||
</div> | ||
<Guides /> | ||
|
||
<ApiDocs /> |