Skip to content

Commit 4fe7115

Browse files
committed
post: rest api
1 parent 42b7e22 commit 4fe7115

File tree

20 files changed

+797
-42
lines changed

20 files changed

+797
-42
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
+++
2+
date = '2025-08-18T8:00:00+08:00'
3+
draft = true
4+
title = 'HTTP Methods, Status Codes and Payloads'
5+
tags = ["HTTP"]
6+
+++
7+
本篇文章基于 REST api 介绍HTTP请求方法、HTTP响应码和API数据载荷, 是之前介绍 REST 那篇文章的延伸
8+
9+
10+
## HTTP Methods
11+
12+
13+
## HTTP Status Codes

content/posts/process.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
+++
2+
date = '2025-08-20T8:00:00+08:00'
3+
draft = true
4+
title = 'A Program is not a PROCESS'
5+
tags = ['OS']
6+
+++
7+
8+
程序是一系列**指令**, 以及 CPU 要完成特定任务的**数据** ≈ 可执行文件
9+
10+
要加载程序, 首先要被加载到内存中, 当加载到内存中时, 包含可执行代码的部分被称为**文本段**, **全局变量****常量值**这类数据被加载到数据段, 还需要额外的内存空间来存放运行是生成的所有数据, 例如用户输入, 临时结果或变量(堆和栈)
11+
12+
- TEXT: 文本段, 不会变化
13+
- DATA: 数据段, 大小不会改变, 但是内容可能发生改变
14+
- HEAP: 堆
15+
- STACK: 栈
16+
17+
以上部分构成了进程的内存布局
18+
19+
假设现在用记事本打开两个不同的文本文件, 则会有两个窗口, 显示不同的文本内容, 这时候就产生了两个进程, 这两个进程的文本段相同, 其他部分不同.
20+
21+
程序本身不是一个进程, 程序本身是一个被动的实体, 进程则是一个主动的实体, 尽管两个进程可能与同一个程序相关联, 但仍然被认为是两个独立的执行序列.
22+
23+
对于 c、c++、rust、go 这类编译型语言而言, 编译的结果是一个可执行文件, 即程序本身
24+
25+
但是对于 python、javascript 这类解释型语言而不是这样
26+
27+
例如, python 实际上有一个解释器, 其代码是一个文本文件不是一个程序. 这种情况下计算机运行的实际上是 python 解释器, 而不是 python 代码, 创建一个解释器进程后, 会像其他进程一样创建内存区域, 在文本段运行的也不是我们编写的代码, 而是解释器代码. 源代码由进程加载到数据段中(例如堆)
28+
29+
进程非正式的定义为正在执行的程序, 这意味着进程和程序是不同的两个概念.
30+
31+
当程序被打开时, 可执行文件被加载到内存中, 这时程序就变成了进程.
32+
这个进程的执行可能需要额外的内存来存储用户输入和临时结果, 操作系统负责分配这部分内存, 即虚拟地址空间 (ADDRESS SPACE).
33+
34+
现在假设有一个单核CPU的机器, 运行多个进程:
35+
当进程1运行中, 某时刻发生中断, CPU 中寄存器和程序计数器等信息被存入进程控制块中(Process Control Block, PCB), 然后会将CPU中的值清空, 然后加载进程2的信息, 这种行为被称为上下文切换(context switch)

content/posts/redis-learn.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
+++
2+
date = '2025-08-19T8:00:00+08:00'
3+
draft = true
4+
title = 'Introduct Redis'
5+
tags = ["Redis", "Database"]
6+
+++
7+
8+
9+
# 数据结构与应用
10+
11+
## 字符串
12+
字符串建是 Redis 最基本的键值对类型, 这种类型的键值对会在数据库中把单独的一个值关联起来, 被关联的键和值可以为文本, 也可以是图片, 视屏, 音频等二进制数据.
13+
14+
- SET: 为字符串键设置值 O(1)
15+
> SET key value
16+
```Redis
17+
SET number "10086"
18+
> OK
19+
20+
SET book "Redis in action"
21+
> OK
22+
```
23+
24+
对于已经存在的 key, 再次赋值会覆盖原值, 若不想覆盖后面添加参数 NX, 相反, 默认 XX 允许覆盖
25+
```Redis
26+
SET key "10086" NX
27+
> (nil)
28+
29+
SET key "10086" XX
30+
> OK
31+
```
32+
33+
- GET: 获取字符串键的值 O(1)
34+
> GET key
35+
36+
```Redis
37+
GET number
38+
> "10086"
39+
```
40+
41+
对于不存在的值, 返回空
42+
```Redis
43+
GET key_new
44+
> (nil)
45+
```
46+
47+
- GETSET: 获取旧值并更新值 O(1)
48+
> GETSET key new\_value
49+
50+
```Redis
51+
GETSET key "123456"
52+
> "10086"
53+
```
54+
55+
### 示例: 缓存
56+
对数据进行缓存是Redis最常见的用法之一, 将数据存储在内存比存储在硬盘要快得多
57+
首先定义缓存
58+
```Python
59+
class Cache:
60+
def __init__(self, client):
61+
self.client = client
62+
63+
def set(self, key, value):
64+
self.client.set(key, value)
65+
66+
def get(self, key):
67+
return self.client.get(key)
68+
69+
def update(self, new_value, key):
70+
return self.client.getset(key, new_value)
71+
72+
```
73+
74+
然后缓存文本数据
75+
```Python
76+
client = Redis(decode_responses=True) # 使用文本编码方式打开客户端
77+
cache = Cache(client)
78+
79+
cache.set("web_page", "<html><p>hello world</p></html>")
80+
print(cache.get("web_page"))
81+
82+
print(cache.update("web_page", "<html><p>update<p></html>"))
83+
print(cache.get("web_page"))
84+
```
85+
86+
下面是存储一个二进制图片的缓存示例
87+
```Python
88+
client = Redis() # 二进制编码打开客户端
89+
cache = Cache(client)
90+
91+
image = open("DailyBing.jpg", "rb") # 二进制只读方式打开图片
92+
data = image.read() # 读取文件内容
93+
image.close() # 关闭文件
94+
95+
cache.set("daily_bing.jpg", data) # 将二进制图片缓存到键 daily_bing.jpg 中
96+
print(cache.get("daily_bing.jpg")[:20]) # 读取二进制数据的前20字节
97+
```
98+
> b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00\x00\x00\x00\x00\x00'
99+
100+
101+
### 示例: 锁
102+

content/posts/rest-apis.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
+++
22
date = '2025-08-17T8:00:00+08:00'
3-
draft = true
3+
draft = false
44
title = 'Designing and Building REST APIs'
5-
tags = ["API-deisgn", "REST"]
5+
tags = ["REST"]
66
+++
77
这篇文章延续之前微服务的内容, 将介绍关于 REST API 的以下几个方面:
88
- REST API 的设计原则
@@ -214,7 +214,8 @@ HATEOAS 是 REST API 设计中的一种范式(paradigm), 它强调可发现性.
214214

215215

216216
### Level 1: Intorducing the concept of resource
217-
1级引入了资源 URL 的概念, 服务器不再使用通用的 `/api` 端点, 而是暴露表示资源的 URL. 例如:
217+
第1级引入了资源 URL 的概念, 服务器不再使用通用的 `/api` 端点, 而是暴露表示资源的 URL. 例如:
218+
218219
- `/orders` 表示订单集和
219220
- `/order/{order_id}` 表示单个订单
220221

@@ -235,7 +236,64 @@ HATEOAS 是 REST API 设计中的一种范式(paradigm), 它强调可发现性.
235236

236237

237238
### Level 2: Using HTTP methods and status codes
239+
第2级引入了 HTTP 请求方法verbs 和 状态码status 的概念, 这一层, HTTP verbs 用于表示具体操作.
240+
例如, 要下订单, 客户端向 `/orders` 端点发送一个 POST 请求, 内容如下:
241+
```Python
242+
{
243+
"order": [
244+
{
245+
"product": "mocha",
246+
"size": "medium",
247+
"quantity": 2
248+
}
249+
]
250+
}
251+
```
252+
253+
在这个例子中, HTTP 方法 POST 表示要执行的操作, 而请求体仅包含想要下的订单的具体信息
254+
255+
类似地, 如果要获取某个订单的详细信息, 我们会向该订单的 URI 发送 GET 请求: `/orders/{order_id}`. 这里使用 GET 告诉服务器, 希望获取 URI 指定资源的详细信息
256+
257+
前几个级别的响应通常都使用相同的状态码(通常为 200), 而第二级引入了 HTTP 状态码的语义化使用, 用来报告客户端请求处理的结果. 例如:
258+
- 使用 POST 创建资源时, 服务器会返回 201 Created 状态码
259+
- 请求不存在的资源时, 会返回 404 Not Found 状态码
238260

239261

262+
### Level 3: API discoverability
263+
第3级引入了可发现性的概念, 通过 HATEOAS 原则, 并在响应中添加表示可对资源执行操作的链接来实现.
240264

265+
例如, 对 `/orders/{order_id}` 端点发送 GET 请求, 会返回该订单的表示(representation), 并包含一系列相关链接
266+
```Python
267+
{
268+
"id": 8,
269+
"status": "progress",
270+
"created": "2023-09-01",
271+
"order": [
272+
{
273+
"product": "cappuccino",
274+
"size": "small",
275+
"quantity": 1
276+
},
277+
{
278+
"product": "croissant",
279+
"size": "medium",
280+
"quantity": 2
281+
}
282+
],
283+
"links": [
284+
{
285+
"href": "/orders/8/cancel",
286+
"description": "Cancels the order",
287+
"type": "POST"
288+
},
289+
{
290+
"href": "/orders/8/pay",
291+
"description": "Pays for the order",
292+
"type": "GET"
293+
}
294+
]
295+
}
296+
```
297+
在 Richardson 成熟度模型中, 第三级代表了他所称的 "REST 的荣耀(Glory of REST)" 的最后一步
241298

299+
该模型为我们提供了一个框架, 用来思考 API 设计在 REST 原则体系中的位置. 它的目的不是衡量 API 在多大程度上"符合"REST 原则, 也不是评估 API 设计的质量; 而是帮助我们思考如何充分利用 HTTP 协议, 创建表达力强、易理解、易使用的 API.

public/archives/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@
5151
<span class="max-w-[4rem] md:max-w-none truncate">Home</span></a></li><li class="flex items-center gap-1 md:gap-2 min-w-0"><span class="text-muted-foreground/50 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
5252
</span><span class="text-foreground flex items-center gap-0.5 md:gap-1 font-medium min-w-0 flex-shrink-0"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg>
5353
<span class="max-w-[3rem] md:max-w-none truncate">Archives</span></span></li></ol></nav><header class=mb-8><div class="mb-4 flex items-center gap-3"><svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 8h14M5 8a2 2 0 110-4h14a2 2 0 110 4M5 8v10a2 2 0 002 2h10a2 2 0 002-2V8m-9 4h4"/></svg><h1 class="text-foreground text-3xl font-bold">Archives</h1></div><p class="text-muted-foreground mb-6">Browse all articles in chronological order and discover what interests you.</p><div class="text-muted-foreground flex items-center gap-4 text-sm"><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>
54-
<span>16 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55-
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">16
56-
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">16
57-
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/intorduce-uuid/ class=block>Intorduce UUID</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
54+
<span>17 posts total</span></div><div class="flex items-center gap-1"><svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
55+
<span>Timeline view</span></div></div></header><div class=relative><div class="bg-border absolute top-0 bottom-0 left-4 w-0.5"></div><div class=mb-12><div class="relative mb-8 flex items-center"><div class="bg-primary absolute left-0 z-10 flex h-8 w-8 items-center justify-center rounded-full"><svg class="h-4 w-4 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg></div><div class=ml-12><h2 class="text-foreground text-2xl font-bold">2025</h2><p class="text-muted-foreground text-sm">17
56+
posts</p></div></div><div class="relative mb-8"><div class="relative mb-4 flex items-center"><div class="bg-accent border-background absolute left-2 z-10 h-4 w-4 rounded-full border-2"></div><div class=ml-12><h3 class="text-foreground text-lg font-semibold">August 2025</h3><p class="text-muted-foreground text-xs">17
57+
posts</p></div></div><div class="ml-12 space-y-3"><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/designing-and-building-rest-apis/ class=block>Designing and Building REST APIs</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
58+
<time datetime=2025-08-17>08-17</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
59+
<span>8
60+
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/intorduce-uuid/ class=block>Intorduce UUID</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>
5861
<time datetime=2025-08-16>08-16</time></div><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3A9 9 0 113 12a9 9 0 0118 0z"/></svg>
5962
<span>1
6063
min</span></div></div></div></div></article><article class="group bg-card border-border hover:bg-accent/50 rounded-lg border p-4 transition-all duration-300"><div class="flex items-center justify-between gap-4"><div class="min-w-0 flex-1"><h4 class="text-foreground group-hover:text-primary mb-3 font-medium transition-colors duration-200"><a href=/posts/microservice-with-fastapi/ class=block>Microservice with FastAPI</a></h4><div class="text-muted-foreground flex items-center gap-4 text-xs"><div class="flex items-center gap-1"><svg class="h-3 w-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5A2 2 0 003 7v12a2 2 0 002 2z"/></svg>

0 commit comments

Comments
 (0)