You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
5.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. ## 内部使用基于go-chi的web框架
  2. # 框架引入
  3. > go get -u github.com/ouxuanserver/osmanthuswine
  4. # 开始
  5. ...
  6. + /config.json 配置文件
  7. ```json
  8. {
  9. "port": "8808",
  10. "host": "0.0.0.0",
  11. "cross_domain": "*",
  12. "post_max_memory": 1024000,
  13. "update_path": "new_exe",
  14. "api_router": "/Api/*",
  15. "db": {
  16. "host": "",
  17. "port": "",
  18. "user": "",
  19. "password": "",
  20. "name": "",
  21. "max_open_conn": 500
  22. }
  23. }
  24. ```
  25. + /main.go文件
  26. ```
  27. package main
  28. import (
  29. "./app/index"
  30. "github.com/ouxuanserver/osmanthuswine"
  31. "github.com/ouxuanserver/osmanthuswine/src/core"
  32. )
  33. func main() {
  34. //注册index控制器
  35. core.GetInstanceRouterManage().Registered(&index.Index{})
  36. //主程序执行
  37. osmanthuswine.Run()
  38. }
  39. ```
  40. + /index/index.go文件
  41. ```
  42. package index
  43. import (
  44. "github.com/ouxuanserver/osmanthuswine/src/core"
  45. )
  46. type Index struct {
  47. core.Controller
  48. }
  49. /*
  50. * 访问url:http://{host}:{port}/{api_router}/{包名首字母大写}/{结构名首字母大写}/{方法名首字母小写}.json
  51. * 例:http://127.0.0.1:8808/Api/Index/Index/index.json
  52. */
  53. func (that *Index) Index() {
  54. that.DisplayByData(that.Request.REQUEST)
  55. }
  56. ```
  57. ## core.Controller.Request 使用说明
  58. #### 输入参数
  59. + that.Request.GET["参数"] //类型:map[string]string
  60. + that.Request.POST["参数"] //类型:map[string]string
  61. + that.Request.REQUEST["参数"] //类型:map[string]string , 为GET以及POST的合并值,当出现值冲突时GET参数会被覆盖
  62. #### session与cookie获取
  63. + that.Request.SESSION["参数"] //类型:map[string]string
  64. + that.Request.COOKIE["参数"] //类型:map[string]string
  65. #### header信息获取
  66. + that.Request.HEADER["参数"] //类型:map[string]string
  67. #### 该取值一般以POST-RAW形式传入原始数据,有可能
  68. + that.Request.BODY //类型:string
  69. #### 快速获取上传的文件
  70. + that.Request.FILE //类型:*multipart.FileHeader
  71. #### 获取上传的所有文件
  72. + that.Request.FILES //类型:map[string][]*multipart.FileHeader
  73. ## core.Controller 使用说明
  74. #### 输出显示
  75. > 即使输出时不处于函数结尾,也无需return
  76. + that.DisplayByData(data interface{})
  77. ```
  78. {
  79. "code":0,
  80. "data":data,
  81. "msg":""
  82. }
  83. ```
  84. - - -
  85. + that.DisplayBySuccess(msg string)
  86. ```
  87. {
  88. "code":0,
  89. "data":null,
  90. "msg":msg
  91. }
  92. ```
  93. - - -
  94. + that.DisplayByError(msg string, code int)
  95. ```
  96. {
  97. "code":code,
  98. "data":null,
  99. "msg":msg
  100. }
  101. ```
  102. - - -
  103. + that.Display(data interface{}, msg string, code int)
  104. ```
  105. {
  106. "code":code,
  107. "data":data,
  108. "msg":msg
  109. }
  110. ```
  111. - - -
  112. + that.DisplayByString(data string)
  113. ```
  114. data //直接输出data以string形式
  115. ```
  116. - - -
  117. + that.DisplayByRaw(data []byte)
  118. ```
  119. data //直接输出data以[]byte形式,可用于直接输出二进制文件
  120. ```
  121. - - -
  122. ##### 20190416新增
  123. + that.CheckErrDisplayByError(err error,msg...)
  124. ```
  125. err //错误信息,自动判断是否等于nil,如果等于nil该语句会被忽略
  126. msg //错误文案提示,不填直接输出err.Error()
  127. ```
  128. #### session操作
  129. > 目前session实现基于securecookie,以加密形式储存在cookie中,注意不要存放大量数据,以免超过cookie的最大储存值
  130. + that.SetSession(name string, value string) //设置session
  131. + that.DeleteSession(name string) //删除session
  132. + that.ClearSession() //清空session
  133. #### cookie操作
  134. > 尽量以session的形式操作
  135. + that.SetCookie(name string, value string)
  136. ## 数据库操作
  137. > 目前框架中集成gorm与xorm框架
  138. + core.GetXormAuto() //获取xorm实例
  139. + core.GetGormAuto() //获取gorm实例
  140. #### 数据库配置
  141. ```
  142. 实例的数据库配置来自于相同目录下的config.json或者private.json文件
  143. {
  144. ...其他配置
  145. "db": {
  146. "host": "",
  147. "port": "",
  148. "user": "",
  149. "password": "",
  150. "name": "",
  151. "prefix": "",
  152. "max_open_conn": 500
  153. }
  154. }
  155. prefix为表前缀
  156. max_open_conn为可支持最大连接数(未测试是否可用
  157. ```
  158. ## 支持WebSocket
  159. > 当传入core.GetInstanceRouterManage().Registered的对象继承自core.WebSocket时,协议升级为websocket,路由地址忽略最后方法名
  160. #### 集成melody库,使用详情https://github.com/olahol/melody
  161. ```
  162. package index
  163. import (
  164. "github.com/ouxuanserver/osmanthuswine/src/core"
  165. "gopkg.in/olahol/melody.v1"
  166. )
  167. type Index struct {
  168. core.WebSocket
  169. }
  170. func (that *Wstest) HandleConnect(session *melody.Session) {
  171. //implement
  172. }
  173. func (that *Wstest) HandlePong(session *melody.Session) {
  174. //implement
  175. }
  176. func (that *Wstest) HandleMessage(session *melody.Session, data []byte) {
  177. that.GetMelody().Broadcast(data)
  178. //implement
  179. }
  180. func (that *Wstest) HandleMessageBinary(session *melody.Session, data []byte) {
  181. //implement
  182. }
  183. func (that *Wstest) HandleSentMessage(session *melody.Session, data []byte) {
  184. //implement
  185. }
  186. func (that *Wstest) HandleSentMessageBinary(session *melody.Session, data []byte) {
  187. //implement
  188. }
  189. func (that *Wstest) HandleDisconnect(session *melody.Session) {
  190. //implement
  191. }
  192. func (that *Wstest) HandleError(session *melody.Session, err error) {
  193. //implement
  194. }
  195. ```
  196. ```
  197. //javascript
  198. var ws = new WebSocket("ws://127.0.0.1/Api/Index/Index")
  199. ```
  200. > PS:不同url对应不同的melody实例
  201. ## 杂项
  202. > 热更新,仅支持linux
  203. ```
  204. 默认情况下,检测同路径下的<文件名_update>,如果该文件与当前文件不一致,则进行热更,已连接的连接无需断连
  205. 可在config.json中配置检测的文件名
  206. {
  207. ...其他配置
  208. "update_path": "需要检测的文件路径"
  209. }
  210. 备注:需要检测的文件路径最好不要与当前运行的文件路径相同
  211. ```