Skip to content

Commit e35fa9b

Browse files
committed
✨ Introducing new features.自己实现一个轻量级 HTTP 框架
1 parent b86be8b commit e35fa9b

1 file changed

Lines changed: 84 additions & 58 deletions

File tree

docs/netty/cicada.md

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
12
<div align="center">
23

3-
<img src="https://ws3.sinaimg.cn/large/006tNbRwly1fuvfxbc7y1j30go0e9aay.jpg" width="300"/>
4+
<img src="https://ws3.sinaimg.cn/large/006tNbRwly1fxda6k9k3bj30oy08cjsx.jpg" />
45
<br/>
56

67
[![Build Status](https://travis-ci.org/crossoverJie/cicada.svg?branch=master)](https://travis-ci.org/crossoverJie/cicada)
@@ -26,12 +27,13 @@
2627

2728
- [x] 代码简洁,没有过多依赖。
2829
- [x] 一行代码即可启动 HTTP 服务。
29-
- [x] 自定义拦截器。
30+
- [x] [自定义拦截器](#自定义拦截器)
3031
- [x] 灵活的传参方式。
3132
- [x] `json` 响应格式。
32-
- [x] 自定义配置。
33+
- [x] [自定义配置](#自定义配置)
3334
- [x] 多种响应方式。
34-
- [ ] `Cookie` 支持。
35+
- [x] 内置可插拔 `IOC` 容器。
36+
- [x] [`Cookie` 支持](#cookie-支持)
3537
- [ ] 文件上传。
3638

3739

@@ -43,7 +45,7 @@
4345
<dependency>
4446
<groupId>top.crossoverjie.opensource</groupId>
4547
<artifactId>cicada-core</artifactId>
46-
<version>1.0.3</version>
48+
<version>x.y.z</version>
4749
</dependency>
4850
```
4951

@@ -60,68 +62,99 @@ public class MainStart {
6062

6163
### 配置业务 Action
6264

63-
创建业务 Action 实现 `top.crossoverjie.cicada.server.action.WorkAction` 接口。
64-
6565
```java
66-
@CicadaAction(value = "demoAction")
67-
public class DemoAction implements WorkAction {
66+
@CicadaAction("routeAction")
67+
public class RouteAction {
6868

69+
private static final Logger LOGGER = LoggerBuilder.getLogger(RouteAction.class);
6970

70-
private static final Logger LOGGER = LoggerBuilder.getLogger(DemoAction.class) ;
7171

72-
private static AtomicLong index = new AtomicLong() ;
72+
@CicadaRoute("getUser")
73+
public void getUser(DemoReq req){
7374

74-
@Override
75-
public void execute(CicadaContext context,Param paramMap) throws Exception {
76-
String name = paramMap.getString("name");
77-
Integer id = paramMap.getInteger("id");
78-
LOGGER.info("name=[{}],id=[{}]" , name,id);
79-
80-
DemoResVO demoResVO = new DemoResVO() ;
81-
demoResVO.setIndex(index.incrementAndGet());
82-
WorkRes<DemoResVO> res = new WorkRes();
83-
res.setCode(StatusEnum.SUCCESS.getCode());
84-
res.setMessage(StatusEnum.SUCCESS.getMessage());
85-
res.setDataBody(demoResVO) ;
86-
context.json(res);
75+
LOGGER.info(req.toString());
76+
WorkRes<DemoReq> reqWorkRes = new WorkRes<>() ;
77+
reqWorkRes.setMessage("hello =" + req.getName());
78+
CicadaContext.getContext().json(reqWorkRes) ;
79+
}
80+
81+
@CicadaRoute("getInfo")
82+
public void getInfo(DemoReq req){
83+
84+
WorkRes<DemoReq> reqWorkRes = new WorkRes<>() ;
85+
reqWorkRes.setMessage("getInfo =" + req.toString());
86+
CicadaContext.getContext().json(reqWorkRes) ;
8787
}
8888

89+
@CicadaRoute("getReq")
90+
public void getReq(CicadaContext context,DemoReq req){
91+
92+
WorkRes<DemoReq> reqWorkRes = new WorkRes<>() ;
93+
reqWorkRes.setMessage("getReq =" + req.toString());
94+
context.json(reqWorkRes) ;
95+
}
96+
97+
98+
8999
}
90100
```
91101

92-
启动应用访问 [http://127.0.0.1:7317/cicada-example/demoAction?name=12345&id=10](http://127.0.0.1:7317/cicada-example/demoAction?name=12345&id=10)
102+
启动应用访问 [http://127.0.0.1:5688/cicada-example/routeAction/getUser?id=1234&name=zhangsan](http://127.0.0.1:5688/cicada-example/routeAction/getUser?id=1234&name=zhangsan)
93103

94104
```json
95-
{
96-
"code": "9000",
97-
"dataBody": {
98-
"index": 1
99-
},
100-
"message": "成功"
101-
}
105+
{"message":"hello =zhangsan"}
102106
```
103107

104108
## Cicada 上下文
105109

106110
通过 `context.json(),context.text()` 方法可以选择不同的响应方式。
107111

108112
```java
109-
@CicadaAction("textAction")
110-
public class TextAction implements WorkAction {
111-
@Override
112-
public void execute(CicadaContext context, Param param) throws Exception {
113+
@CicadaAction("routeAction")
114+
public class RouteAction {
115+
116+
private static final Logger LOGGER = LoggerBuilder.getLogger(RouteAction.class);
117+
118+
@CicadaRoute("getUser")
119+
public void getUser(DemoReq req){
120+
121+
LOGGER.info(req.toString());
122+
WorkRes<DemoReq> reqWorkRes = new WorkRes<>() ;
123+
reqWorkRes.setMessage("hello =" + req.getName());
124+
CicadaContext.getContext().json(reqWorkRes) ;
125+
}
126+
127+
@CicadaRoute("hello")
128+
public void hello() throws Exception {
129+
CicadaContext context = CicadaContext.getContext();
130+
113131
String url = context.request().getUrl();
114132
String method = context.request().getMethod();
115133
context.text("hello world url=" + url + " method=" + method);
116-
}
134+
}
135+
136+
117137
}
118138
```
119139

120-
![](https://ws1.sinaimg.cn/large/006tNbRwly1fvxvvo8yioj313i0tudij.jpg)
121140

122-
同时也可以根据 `context.request()` 获得请求上下文中的其他信息。
141+
## Cookie 支持
142+
143+
### 设置 Cookie
144+
145+
```java
146+
Cookie cookie = new Cookie() ;
147+
cookie.setName("cookie");
148+
cookie.setValue("value");
149+
CicadaContext.getResponse().setCookie(cookie);
150+
```
151+
152+
### 获取 Cookie
123153

124-
![](https://ws2.sinaimg.cn/large/006tNbRwly1fvxvxmpsjcj30yy0yo77h.jpg)
154+
```java
155+
Cookie cookie = CicadaContext.getRequest().getCookie("cookie");
156+
logger.info("cookie = " + cookie.toString());
157+
```
125158

126159
## 自定义配置
127160

@@ -197,8 +230,9 @@ public class ExecuteTimeInterceptor implements CicadaInterceptor {
197230
private Long end;
198231

199232
@Override
200-
public void before(Param param) {
233+
public boolean before(Param param) {
201234
start = System.currentTimeMillis();
235+
return true;
202236
}
203237

204238
@Override
@@ -210,23 +244,6 @@ public class ExecuteTimeInterceptor implements CicadaInterceptor {
210244
}
211245
```
212246

213-
### 拦截适配器
214-
215-
同样也可以只实现其中一个方法,只需要继承 `top.crossoverjie.cicada.server.intercept.AbstractCicadaInterceptorAdapter` 抽象类。
216-
217-
```java
218-
@Interceptor(value = "loggerInterceptor")
219-
public class LoggerInterceptorAbstract extends AbstractCicadaInterceptorAdapter {
220-
221-
private static final Logger LOGGER = LoggerBuilder.getLogger(LoggerInterceptorAbstract.class) ;
222-
223-
@Override
224-
public void before(Param param) {
225-
LOGGER.info("logger param=[{}]",param.toString());
226-
}
227-
228-
}
229-
```
230247

231248
## 性能测试
232249

@@ -238,6 +255,15 @@ public class LoggerInterceptorAbstract extends AbstractCicadaInterceptorAdapter
238255

239256
## 更新记录
240257

258+
### v2.0.1
259+
- 更新 Logo ,美化日志。
260+
- 支持 `Cookie`
261+
262+
### v2.0.0
263+
- 修复 [#12](https://github.com/TogetherOS/cicada/issues/12) [#22](https://github.com/TogetherOS/cicada/issues/22) [#28](28)
264+
- 更加灵活的路由方式。
265+
- 内置可插拔 `IOC` 容器。
266+
241267
### v1.0.3
242268

243269
- 修复 [#9](https://github.com/TogetherOS/cicada/issues/9)

0 commit comments

Comments
 (0)