2025-07-11 15:36:02 +08:00

32 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

从0到1实现一个web服务器
---
### 实现https访问
1. 生成证书
1. 方式1使用openssl生成证书
```shell
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out keystore.p12 -name myhttps -CAfile chain.pem -caname root
```
2. 方式2使用第三方工具证书key和crt
```shell
# key 和 crt 转换成 PKCS12.p12
openssl pkcs12 -export -in ssl.crt -inkey ssl.key -out keystore_3rd.p12 -name myhttps -CAfile ca.crt -caname root
Enter Export Password: (123456)
Verifying - Enter Export Password: (123456)
```
2. 代码参照: [top.yexuejc.demo.core.WebServer](src/main/java/top/yexuejc/demo/core/WebServer.java).startHttps
### 扩展一个ctrl能正常处理请求
1. 定义[@RestController](src/main/java/top/yexuejc/demo/annotation/RestController.java)和[@GetMapping](src/main/java/top/yexuejc/demo/annotation/GetMapping.java)注解
2. 定义扫描器和模拟bean容器: [ControllerSupplier](src/main/java/top/yexuejc/demo/core/ControllerSupplier.java)
3. 接入处理逻辑: [RequestHandler](src/main/java/top/yexuejc/demo/core/RequestHandler.java) line 49
```java
Response response = ControllerSupplier.invoke(request);
```
4. 程序入口装配: [WebServerApplication](src/main/java/top/yexuejc/demo/WebServerApplication.java) line 16
```java
ControllerSupplier.builder(WebServerApplication.class);
```
### 启动参数配置
参照 [WebServerApplication](src/main/java/top/yexuejc/demo/WebServerApplication.java).argsProcess
### 配置文件读取
参照 [AppConfig](src/main/java/top/yexuejc/demo/AppConfig.java)