# simpleframework
### 使用说明
+ 创建路由
```
router := simpleframework.Default()`
```
+ GET方法,用文本输出的工具方法验证:
```
router.GET("/get/h/i/hello", func(c *simpleframework.Context) error {
return c.String(200, "get——hello")
})
```
+ POST方法:
```
router.POST("/post", func(c *simpleframework.Context) error {
return c.String(200, "post——hello")
})
```
+ 用simpleframework.Context取其中的Resp和Req,读取静态资源:
```
//要把工作路径改为“$GOPATH/src/其他分级文件夹/simpleftamwork/demo”,包路径改为“其他分级文件夹/simpleftamwork/demo”
router.GET("/html/*", func(c *simpleframework.Context) error {
staticHandle := http.StripPrefix("/html",
http.FileServer(http.Dir("./static/html")))
staticHandle.ServeHTTP(c.Resp, c.Req)
return nil
})
router.GET("/image/*", func(c *simpleframework.Context) error {
staticHandle := http.StripPrefix("/image",
http.FileServer(http.Dir("./static/image")))
staticHandle.ServeHTTP(c.Resp, c.Req)
return nil
})
```
+ 两种方式传参:
```
//直接取simpleframework.Context中Req的参数
router.GET("/hello", func(c *simpleframework.Context) error {
name := c.Req.FormValue("name")
if name==""{
return c.String(200,"hello,guest")
}else {
return c.String(200,"hello,"+name)
}
})
//路径传参(动态路由),同样从simpleframework.Context中Req取参数
router.GET("/hello/:name", func(c *simpleframework.Context) error {
return c.String(200,"hello,"+c.Req.Form["name"][0])
})
```
+ json输出的工具方法
```
//获取json方法1
router.GET("/json", func(c *simpleframework.Context) error {
return c.JSON(200, 200, "this is a message1", map[string]string{"data1": "demo1", "data2": "demo2"})
})
//获取json方法2
router.GET("/jsonn", func(c *simpleframework.Context) error {
return c.JSONN(200, map[string]interface{}{"message": "this is a message2", "status": 200})
})
```
+ 极简的WebSocketDemo
```
//WebSocketDemo仅限低字节文本
router.WebSocket("/ws", simpleframework.WebSocketConfig{
OnOpen: func(wsc *simpleframework.WebSocketContext) error{
log.Println("ws:open!")
return nil
},
OnClose: func(wsc *simpleframework.WebSocketContext) error {
log.Println("ws:close!")
return nil
},
OnMessage: func(wsc *simpleframework.WebSocketContext, s string) error {
log.Println(wsc.Conn.RemoteAddr(),"客户端发送了信息:", s)
wsc.Send(s,1)
return nil
},
OnError: nil,
})
```
+ 最后监听端口
```
router.Run(":80")
```
### 备注
+ 暂不支持GET/POST以外的方法,路由树里未添加
+ 极简WebSocket只能每帧传小于125字节的文本
+ WebSocket和GET共用路由树;不能出现重复路径,否者后者覆盖前者

matlab大师
- 粉丝: 2964
最新资源
- 实施方案进程调度算法模拟.docx
- 模拟卷基于Aja的Web聊天系统设计与实现.doc
- 复合桥面防水层的特点以及施工监理要点.doc
- 维修工作记录表.docx
- 【QC】降低模板工程对混凝土质量的不良影响.ppt
- 全方位剖析seo实战大型站群网站之三步解决关键字筛选.ppt
- 单片机的煤气报警器研究与设计开发.doc
- 全国计算机等级考试《三级网络技术》上机考试冲刺试题rd文档.doc
- 基于ESI和InCites数据库的高校科研竞争力分析.docx
- 网络时代会计的发展趋势与策略选择.docx
- 购买时刻回顾与总结-汇总版.pdf
- 工程项目施工成本控制与索赔管理(附表格).ppt
- 机械设计制造及其自动化中计算机技术应用探索.docx
- 第六册-microsoft-word-文档.doc
- 软件项目策划成功进行的九个基本要点.docx
- 智慧城市测绘中地理信息系统的应用.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


