github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/modules/app/app.go (about) 1 package app 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "strconv" 8 "time" 9 10 "github.com/insionng/yougam/helper" 11 "github.com/insionng/yougam/libraries/cli" 12 "github.com/insionng/yougam/models" 13 "github.com/insionng/yougam/modules/auth" 14 "github.com/insionng/yougam/modules/jsock" 15 "github.com/insionng/yougam/modules/setting" 16 "github.com/insionng/yougam/routers" 17 "github.com/insionng/yougam/routers/apis" 18 "github.com/insionng/yougam/routers/root" 19 20 "github.com/insionng/makross" 21 "github.com/insionng/makross/cache" 22 "github.com/insionng/makross/captcha" 23 "github.com/insionng/makross/cors" 24 "github.com/insionng/makross/csrf" 25 "github.com/insionng/makross/i18n" 26 "github.com/insionng/makross/jwt" 27 //"github.com/insionng/makross/macrof" 28 //"github.com/insionng/makross/macrus" 29 "github.com/insionng/makross/logger" 30 "github.com/insionng/makross/pongor" 31 "github.com/insionng/makross/recover" 32 "github.com/insionng/makross/session" 33 "github.com/insionng/makross/static" 34 35 "github.com/insionng/yougam/modules/apis/version1" 36 "github.com/insionng/yougam/modules/apis/version2" 37 ) 38 39 func App(c *cli.Context) error { 40 41 if err := models.NewEngine(); err != nil { 42 return err 43 } else { 44 models.HasEngine = true 45 } 46 47 /*------------------------------------*/ 48 m := makross.New() 49 /*------------------------------------*/ 50 m.Use(recover.Recover()) 51 //m.Use(macrus.New()) 52 m.Use(logger.Logger()) 53 m.Use(cors.CORS()) 54 /*------------------------------------*/ 55 56 var production bool 57 if len(c.Args()) >= 3 { 58 production, _ = strconv.ParseBool(c.Args()[2]) 59 } 60 61 /*------------------------------------*/ 62 //中间件之间存在顺序及依赖关系,请勿随意移动位置. 63 /*------------------------------------*/ 64 m.Static("/file/", "../file") 65 /*------------------------------------*/ 66 staticPath := fmt.Sprintf("themes/%v/static", helper.Theme()) 67 /*------------------------------------*/ 68 if production { 69 if e := os.RemoveAll("./public"); e != nil { 70 log.Println("删除目录出错:", e) 71 } 72 //复制模板静态资源到public目录 73 if e := helper.CopyDir(staticPath, "./public"); e != nil { 74 log.Println("复制模板静态资源到public目录时出现错误:", e) 75 } 76 m.Use(static.Static("public")) 77 } else { 78 m.Use(static.Static(staticPath)) 79 } 80 fmt.Println("Production:", production) 81 /*------------------------------------*/ 82 templatesPath := fmt.Sprintf("themes/%v/templates", helper.Theme()) 83 m.SetRenderer(pongor.Renderor(pongor.Option{ 84 Directory: templatesPath, 85 Reload: !production, 86 })) 87 /*------------------------------------*/ 88 89 if helper.IsExist("./conf/locale/locale_zh-CN.ini") { 90 m.Use(i18n.I18n(i18n.Options{ 91 Directory: "conf/locale", 92 DefaultLang: "zh-CN", 93 Langs: []string{"en-US", "zh-CN"}, 94 Names: []string{"English", "简体中文"}, 95 Redirect: true, 96 })) 97 } 98 99 /*------------------------------------*/ 100 //如果在前端启用了Nginx自带的Gzip功能,那么此处可以注释掉 101 //m.Use(compress.Gzip()) 102 /*------------------------------------*/ 103 m.Use(cache.Cacher()) 104 /*------------------------------------*/ 105 m.Use(captcha.Captchaer(captcha.Options{ 106 URLPrefix: "/captcha/", // URL prefix of getting captcha pictures. 107 FieldIDName: "captchaid", // Hidden input element ID. 108 FieldCaptchaName: "captcha", // User input value element name in request form. 109 ChallengeNums: 6, // Challenge number. 110 Width: 276, // Captcha image width. 111 Height: 80, // Captcha image height. 112 Expiration: 60, // Captcha expiration time in seconds. 113 CachePrefix: "captcha_", // Cache key prefix captcha characters. 114 })) 115 /*------------------------------------*/ 116 m.Use(session.Sessioner()) 117 /*------------------------------------*/ 118 //macrof.Wrapper(m) 119 //-------------------------------------------------------------------------------------// 120 121 m.File("/favicon.ico", fmt.Sprintf("themes/%v/static/img/favicon.png", helper.Theme())) 122 123 g := m.Group("", setting.BaseMiddler()) 124 125 g.Get("/", routers.GetMainHandler) 126 127 //首页 ?page 128 g.Get("/page<page:\\d+>/", routers.GetMainHandler) 129 //首页 hotness/confidence类 130 g.Get("/topics/<tab:[A-Za-z]+>/", routers.GetMainHandler) 131 //http://localhost/lastest/2/ 132 g.Get("/topics/<tab:[A-Za-z]+>/<page:(\\d+)>/", routers.GetMainHandler) 133 //http://localhost/lastest/page2/ 134 g.Get("/topics/<tab:[A-Za-z]+>/page<page:(\\d+)>/", routers.GetMainHandler) 135 136 //搜索话题 137 g.Get("/search/", routers.GetMainHandler) 138 //同时支持page和keyword参数 139 g.Get("/search/<keyword:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 140 //支持keyword参数 141 g.Get("/search/<keyword:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/", routers.GetMainHandler) 142 143 //浏览分类 "/category/<tab:([A-Za-z]+)>/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/"优先级必须高于"/category/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/" 144 g.Get("/category/<cid:\\d+>/", routers.GetMainHandler) 145 g.Get("/category/<cid:\\d+>/page<page:(\\d+)>/", routers.GetMainHandler) 146 g.Get("/category/<cid:\\d+>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 147 148 g.Get("/category/<tab:([A-Za-z]+)>/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetMainHandler) 149 g.Get("/category/<tab:([A-Za-z]+)>/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 150 151 g.Get("/category/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetMainHandler) 152 g.Get("/category/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 153 g.Get("/category/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 154 155 //浏览节点 "/node/<tab:([A-Za-z]+)>/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/"优先级必须高于"/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/" 156 g.Get("/node/<nid:\\d+>/", routers.GetMainHandler) 157 g.Get("/node/<nid:\\d+>/page<page:(\\d+)>/", routers.GetMainHandler) 158 g.Get("/node/<nid:\\d+>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 159 160 g.Get("/node/<tab:([A-Za-z]+)>/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetMainHandler) 161 g.Get("/node/<tab:([A-Za-z]+)>/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 162 163 g.Get("/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetMainHandler) 164 g.Get("/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 165 g.Get("/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 166 167 g.Get("/createdby/<username:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetMainHandler) 168 g.Get("/createdby/<username:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 169 g.Get("/createdby/<username:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetMainHandler) 170 171 //best 172 g.Get("/best/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/", routers.GetBestHandler) 173 g.Get("/best/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/", routers.GetBestHandler) 174 g.Get("/best/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/page<page:(\\d+)>/", routers.GetBestHandler) 175 g.Get("/best/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetBestHandler) 176 177 //详情页面 178 g.Get("/<tid:(\\d+)>/", routers.GetTopicHandler) 179 g.Get("/topic/<tid:(\\d+)>/", routers.GetTopicHandler) 180 181 g.Get("/identicon/<name>/<size>/default.png", routers.GetIdenticonHandler) 182 183 g.Get("/page/<pageid:\\d+>/", routers.GetPageHandler) 184 g.Get("/page/<page:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetPageHandler) 185 186 g.Get("/user/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/", routers.GetUserHandler) 187 g.Get("/user/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/<type>/", routers.GetUserHandler) 188 g.Get("/user/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/<type>/page<page:(\\d+)>/", routers.GetUserHandler) 189 g.Get("/user/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/<type>/<tab:([A-Za-z]+)>/page<page:(\\d+)>/", routers.GetUserHandler) 190 g.Get("/users/", routers.GetUsersHandler) 191 192 //touch view 193 g.Get("/touch/view/<name:([A-Za-z]+)>/<id:\\d+>/", routers.GetTouchViewHandler) 194 195 //登录 196 g.Get("/signin/", csrf.CSRFWithConfig(csrf.CSRFConfig{ 197 TokenLookup: "form:csrf", 198 }), routers.GetSigninHandler).Post(routers.PostSigninHandler) 199 200 //退出 201 g.Get("/signout/", routers.GetSignoutHandler) 202 203 //注册 204 g.Get("/signup/", routers.GetSignupHandler).Post(routers.PostSignupHandler) 205 g.Get("/signup/<key>/", routers.GetSignupHandler) 206 207 //忘记密码 208 forgot := g.Group("/forgot") 209 forgot.Get("/", routers.GetForgotHandler).Post(routers.PostForgotHandler) 210 forgot.Get("/<key>/", routers.GetForgotHandler).Post(routers.PostForgotHandler) 211 212 //root signin 213 g.Get("/root/signin/", csrf.CSRFWithConfig(csrf.CSRFConfig{ 214 TokenLookup: "form:csrf", 215 }), root.GetRSigninHandler).Post(root.PostRSigninHandler) 216 //无需权限 217 218 //-------------------------------------------------------------------------------------// 219 220 u := g.Group("", setting.AuthWebMiddler()) 221 //创建节点 222 u.Get("/new/node/", routers.GetNewNodeHandler).Post(routers.PostNewNodeHandler) 223 u.Get("/new/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/", routers.GetNewNodeHandler).Post(routers.PostNewNodeHandler) //创建下级节点 :node是上级的title 224 u.Get("/new/node/<nid:\\d+>/", routers.GetNewNodeHandler).Post(routers.PostNewNodeHandler) //创建下级节点 :nid是上级的id 225 u.Get("/new/node/<category:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/category/", routers.GetNewNodeHandler).Post(routers.PostNewNodeHandler) //创建指定:category的下级节点 :nid是上级的id 226 u.Get("/new/node/<cid:\\d+>/category/", routers.GetNewNodeHandler).Post(routers.PostNewNodeHandler) //创建指定:cid的下级节点 :nid是上级的id 227 228 //创建话题 229 u.Get("/subject/<tid>/topic/", routers.GetNewTopicHandler).Post(routers.PostNewTopicHandler) 230 u.Get("/new/topic/", routers.GetNewTopicHandler).Post(routers.PostNewTopicHandler) 231 u.Post("/new/topic/<tid>/", routers.PostNewTopicHandler) 232 233 u.Get("/new/node/<nid:(\\d+)>/topic/", routers.GetNewTopicHandler).Post(routers.PostNewTopicHandler) 234 u.Post("/new/node/<nid:(\\d+)>/topic/<tid>/", routers.PostNewTopicHandler) 235 u.Get("/new/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/topic/", routers.GetNewTopicHandler).Post(routers.PostNewTopicHandler) 236 u.Post("/new/node/<node:([\\x{4e00}-\\x{9fa5}A-Za-z]+)>/topic/<tid>/", routers.PostNewTopicHandler) 237 238 //修改话题 239 u.Get("/modify/<tid>/", routers.GetModifyTopicHandler).Post(routers.PostModifyTopicHandler) 240 //支付话题 241 u.Post("/pay/<name:([A-Za-z]+)>/<amount:\\d+>/<id:\\d+>/", routers.PostPaymentHandler) 242 //收藏话题 243 u.Get("/favorite/<name:([A-Za-z]+)>/<id:\\d+>/", routers.GetFavoriteHandler) 244 245 //创建回复 246 u.Post("/subject/<tid>/comment/", routers.PostNewReplyHandler) 247 248 //通知提醒 249 u.Get("/notifications/", routers.NotificGetHandler) 250 u.Get("/notifications/<page:(\\d+)>/", routers.NotificGetHandler) 251 u.Get("/notifications/page<page:(\\d+)>/", routers.NotificGetHandler) 252 253 //删除通知 254 u.Get("/delete/notification/<notificid>/", routers.DeleteNotificGetHandler) 255 256 //个人设置 257 u.Get("/settings/<name:([\\x{4e00}-\\x{9fa5}A-Z0-9a-z_-]+)>/", routers.SettingsGetHandler).Post(routers.SettingsPostHandler) 258 259 //好友聊天 260 jsock.JSock(m) 261 262 u.Get("/friend/<kind>/<uid:(\\d+)>/", routers.GetFriendHandler).Post(routers.PostFriendHandler) 263 264 //需要权限 265 //-------------------------------------------------------------------------------------// 266 //root routers 267 r := g.Group("/root", setting.RootMiddler()) 268 269 //管理员注册请在models文件里设置,或使用默认管理员身份登录后台自行添加 270 271 r.Get("/signout/", root.GetRSignoutHandler) 272 r.Get("/dashboard/", root.GetRDashboardHandler) 273 274 //主题 275 r.Get("/theme/", root.GetRThemeHandler) 276 r.Get("/theme/<name>/", root.GetRThemeHandler) 277 278 rs := r.Group("/search") 279 rs.Get("/user/", root.GetRReadUserHandler) 280 rs.Get("/user/<keyword>/", root.GetRReadUserHandler) 281 282 rs.Get("/category/", root.GetRSettingPasswordHandler) 283 rs.Get("/node/", root.GetRSettingPasswordHandler) 284 rs.Get("/topic/", root.GetRSettingPasswordHandler) 285 286 //CRUD Create(创建)操作 287 rc := r.Group("/create") 288 rc.Get("/user/", root.GetRCreateUserHandler).Post(root.PostRCreateUserHandler) 289 rc.Get("/category/", root.RCreateCategoryGetHandler).Post(root.RCreateCategoryPostHandler) 290 rc.Get("/node/", root.RCreateNodeGetHandler).Post(root.PostRCreateNodeHandler) 291 rc.Get("/topic/", root.GetRCreateTopicHandler).Post(root.PostRCreateTopicHandler) 292 rc.Get("/<pid>/topic/", root.GetRCreateTopicHandler).Post(root.PostRCreateTopicHandler) 293 rc.Get("/administrator/", root.GetRCreateRootHandler).Post(root.PostRCreateRootHandler) 294 rc.Get("/page/", root.GetRCreatePageHandler).Post(root.PostRCreatePageHandler) 295 rc.Get("/link/", root.GetRCreateLinkHandler).Post(root.PostRCreateLinkHandler) 296 297 //Read(读取)操作 298 rr := r.Group("/read") 299 rr.Get("/user/", root.GetRReadUserHandler).Post(root.PostRReadUserHandler) 300 rr.Get("/user/<uid>/", root.GetRReadUserHandler).Post(root.PostRReadUserHandler) 301 rr.Get("/category/", root.GetRReadCategoryHandler) 302 rr.Get("/category/<cid>/", root.GetRReadCategoryHandler) 303 rr.Get("/node/", root.GetRReadNodeHandler) 304 rr.Get("/node/<nid>/", root.GetRReadNodeHandler) 305 306 rr.Get("/<pid>/topic/", root.GetRReadTopicHandler).Post(root.PostRReadTopicHandler) 307 rr.Get("/<pid>/topic/<tid>/", root.GetRReadTopicHandler).Post(root.PostRReadTopicHandler) 308 309 rr.Get("/topic/", root.GetRReadTopicHandler).Post(root.PostRReadTopicHandler) 310 rr.Get("/topic/<tid>/", root.GetRReadTopicHandler).Post(root.PostRReadTopicHandler) 311 312 rr.Get("/reply/", root.GetRReadReplyHandler).Post(root.PostRReadReplyHandler) 313 rr.Get("/reply/<cmid>/", root.GetRReadReplyHandler).Post(root.PostRReadReplyHandler) 314 315 rr.Get("/administrator/", root.GetRReadRootHandler) 316 rr.Get("/administrator/<uid>/", root.GetRReadRootHandler) 317 318 rr.Get("/report/", root.GetRReadReportHandler).Post(root.PostRReadReplyHandler) 319 rr.Get("/report/<name>/", root.GetRReadReportHandler).Post(root.PostRReadReplyHandler) 320 rr.Get("/report/<rid>/", root.GetRReadReportHandler).Post(root.PostRReadReplyHandler) 321 322 rr.Get("/page/", root.GetRReadPageHandler) 323 rr.Get("/page/<pageid>/", root.GetRReadPageHandler) 324 325 rr.Get("/link/", root.GetRReadLinkHandler) 326 rr.Get("/link/<linkid>/", root.GetRReadLinkHandler) 327 328 //Update(更新)操作 329 ru := r.Group("/update") 330 ru.Get("/user/<uid>/<holder>/", root.GetRUpdateUserHandler) 331 332 ru.Get("/user/<uid>/", root.GetRUpdateUserHandler).Post(root.PostRUpdateUserHandler) 333 ru.Get("/administrator/<uid>/", root.GetRUpdateRootHandler).Post(root.PostRUpdateRootHandler) 334 335 ru.Get("/category/", root.GetRUpdateCategoryHandler).Post(root.PostRUpdateCategoryHandler) 336 ru.Get("/category/<cid>/", root.GetRUpdateCategoryHandler).Post(root.PostRUpdateCategoryHandler) 337 338 ru.Get("/node/", root.GetRUpdateNodeHandler).Post(root.PostRUpdateNodeHandler) 339 ru.Get("/node/<nid>/", root.GetRUpdateNodeHandler).Post(root.PostRUpdateNodeHandler) 340 341 ru.Get("/topic/", root.GetRUpdateTopicHandler).Post(root.PostRUpdateTopicHandler) 342 ru.Get("/topic/<tid>/", root.GetRUpdateTopicHandler).Post(root.PostRUpdateTopicHandler) 343 344 ru.Get("/reply/", root.GetRUpdateReplyHandler).Post(root.PostRUpdateReplyHandler) 345 ru.Get("/reply/<rid>/", root.GetRUpdateReplyHandler).Post(root.PostRUpdateReplyHandler) 346 347 ru.Get("/page/", root.GetRUpdatePageHandler).Post(root.PostRUpdatePageHandler) 348 ru.Get("/page/<pageid>/", root.GetRUpdatePageHandler).Post(root.PostRUpdatePageHandler) 349 350 ru.Get("/link/", root.GetRUpdateLinkHandler).Post(root.PostRUpdateLinkHandler) 351 ru.Get("/link/<linkid>/", root.GetRUpdateLinkHandler).Post(root.PostRUpdateLinkHandler) 352 353 ru.Get("/topic/move/<tid>/", root.GetRMoveTopicHandler).Post(root.PostRMoveTopicHandler) 354 ru.Get("/user/recharge/<uid>/", root.GetRRechargeUserHandler).Post(root.PostRRechargeUserHandler) 355 356 //Delete(删除)操作 357 rd := r.Group("/delete") 358 rd.Get("/user/<uid>/", root.GetRDeleteUserHandler) 359 rd.Get("/administrator/<uid>/", root.GetRDeleteUserHandler) 360 rd.Get("/category/<cid>/", root.GetRDeleteCategoryHandler) 361 rd.Get("/node/<nid>/", root.GetRDeleteNodeHandler) 362 rd.Get("/topic/<tid>/", root.GetRDeleteTopicHandler) 363 rd.Get("/reply/<rid>/", root.GetRDeleteReplyHandler) 364 rd.Get("/page/<pageid>/", root.GetRDeletePageHandler) 365 rd.Get("/link/<linkid>/", root.GetRDeleteLinkHandler) 366 367 rse := r.Group("/setting") 368 rse.Get("/password/", root.GetRSettingPasswordHandler).Post(root.PostRSettingPasswordHandler) 369 370 //-------------------------------------------------------------------------------------// 371 m.Get("/sock/", makross.WrapHTTPHandler(jsock.JSockHandler)) 372 //-------------------------------------------------------------------------------------// 373 374 //需要权限 375 376 //-------------------------------------------------------------------------------------// 377 378 //APIS 379 ra := m.Group("/api", setting.APISessionMiddler()) 380 ra.Get("/messages/", apis.GetMessagesHandler) 381 382 //QINIU SIGN API 383 //m.Get("/sign4qiniu/", apis.ApisSign4QiniuGetHandler) 384 ra.Post("/upload/", apis.PostUploadHandler) 385 386 //touch routers 387 rat := ra.Group("/touch") 388 rat.Get("/<type:([A-Za-z]+)>/<name:([A-Za-z]+)>/<id:\\d+>/", routers.GetTouchHandler) 389 rat.Get("/favorite/<name:([A-Za-z]+)>/<id:\\d+>/", routers.GetFavoriteHandler) 390 //rat.Get("/delete/topic/<tid>/",routers.TouchDeleteHandler) 391 392 /*-------------APIs Start-------------*/ 393 394 v1 := m.Group("/apis/v1") 395 //加密通信API 396 // GET/POST /apis/v1/ 397 v1.Get("/", version1.GetApisHandler) 398 v1.Post("/", setting.APICryptMiddler(), version1.PostApisHandler) 399 //需要权限 针对移动端接口 400 401 /*------------------------------------*/ 402 403 v2 := m.Group("/apis/v2") 404 405 // GET from /apis/v2/ 406 v2.Get("/", version2.GetVersionHandler) 407 408 // POST to /apis/v2/signup/ 409 v2.Post("/signup/", version2.PostSignupHandler) 410 411 // POST to /apis/v2/signin/ 412 v2.Post("/signin/", version2.PostSigninHandler) 413 414 // GET from /apis/v2/signout/ 415 v2.Get("/signout/", version2.GetSignoutHandler) 416 417 // POST to /apis/v2/report/ 418 v2.Post("/report/", version2.PostReportHandler) 419 420 var secret = "secret" 421 jwt.DefaultJWTConfig.SigningKey = secret 422 jwt.DefaultJWTConfig.Expires = time.Minute * 60 423 j := v2.Group("", jwt.JWT(secret), auth.AuthJwtMiddler()) 424 425 // GET from /apis/v2/ping/ 426 j.Get("/ping/", version2.GetPongHandler) 427 428 // GET from /apis/v2/categories/ 429 j.Get("/categories/", version2.GetCategoriesHandler) 430 431 // GET from /apis/v2/category/<id>/ 432 j.Get("/category/<id:\\d+>/", version2.GetCategoryHandler) 433 434 // GET,POST,PUT,DEL /apis/v2/category/ 435 j.Get("/category/", version2.GetCategoryHandler).Post(version2.PostCategoryHandler).Put(version2.PutCategoryHandler).Delete(version2.DelCategoryHandler) 436 437 // GET from /apis/v2/nodes/ 438 j.Get("/nodes/", version2.GetNodesHandler) 439 440 // GET from /apis/v2/node/<id>/ 441 j.Get("/node/<id:\\d+>/", version2.GetNodeHandler) 442 443 // GET,POST,PUT,DEL /apis/v2/node/ 444 j.Get("/node/", version2.GetNodeHandler).Post(version2.PostNodeHandler).Put(version2.PutNodeHandler).Delete(version2.DelNodeHandler) 445 446 // GET from /apis/v2/topics/ 447 j.Get("/topics/", version2.GetTopicsHandler) 448 449 // GET from /apis/v2/topic/<id>/ 450 j.Get("/topic/<id:\\d+>/", version2.GetTopicHandler) 451 452 // GET,POST /apis/v2/content/ 453 j.Get("/content/<id:\\d+>/", version2.GetContentHandler).Post(version2.PostContentHandler).Put(version2.PutContentHandler).Delete(version2.DelContentHandler) 454 455 j.Get("/content/", version2.GetContentHandler).Post(version2.PostContentHandler).Put(version2.PutContentHandler).Delete(version2.DelContentHandler) 456 457 // GET,POST /apis/v2/comment/ 458 j.Get("/comment/", version2.GetCommentHandler).Post(version2.PostCommentHandler) 459 460 // GET from /apis/v2/users/ 461 j.Get("/users/", version2.GetUsersHandler) 462 463 // GET/PUT /apis/v2/user/<id>/ 464 j.Get("/user/<id:\\d+>/", version2.GetUserHandler).Put(version2.PutUserHandler) 465 466 // GET,POST,PUT,DEL /apis/v2/user/ 467 j.Get("/user/", version2.GetUserHandler).Post(version2.PostUserHandler).Put(version2.PutUserHandler).Delete(version2.DelUserHandler) 468 469 // GET 特定用户话题数据 /apis/v2/user/<id>/topics/ 470 j.Get("/user/<id:\\d+>/topics/", version2.GetTopicsByUserHandler) 471 472 // GET,POST /apis/v2/favorite/topic/ 473 j.Get("/favorite/topic/", version2.GetFavoriteTopicHandler).Post(version2.PostFavoriteTopicHandler).Delete(version2.DelFavoriteTopicHandler) 474 475 // POST to /apis/v2/favorite/topic/<id>/ 476 j.Post("/favorite/topic/<id:\\d+>/", version2.PostFavoriteTopicHandler) 477 478 // GET from /apis/v2/favorite/topics/ 479 j.Get("/favorite/topics/", version2.GetFavoriteTopicsHandler) 480 481 // POST to /apis/v2/upload/ 482 j.Post("/upload/", version2.PostUploadHandler) 483 484 /*------------------APIs End------------------*/ 485 486 if len(c.Args()) > 1 { 487 switch l := len(c.Args()); { 488 case l >= 2: 489 { 490 addr := fmt.Sprintf("%v:%v", c.Args()[0], c.Args()[1]) 491 log.Printf("Listen on %v\n", addr) 492 m.Listen(addr) 493 return nil 494 } 495 } 496 497 } else { 498 if len(c.Args()) == 1 { 499 addr := c.Args()[0] 500 log.Printf("Listen on %v\n", addr) 501 m.Listen(addr) 502 return nil 503 } else { 504 port := 8000 505 log.Printf("Listen on %v\n", port) 506 m.Listen(port) 507 return nil 508 } 509 } 510 511 return nil 512 513 }