{"id":14987681,"url":"https://github.com/ffhelicopter/tmm","last_synced_at":"2025-04-12T00:16:48.149Z","repository":{"id":57484436,"uuid":"134219355","full_name":"ffhelicopter/tmm","owner":"ffhelicopter","description":"使用Gin快速搭建WEB站点以及提供RESTful接口","archived":false,"fork":false,"pushed_at":"2023-03-25T02:59:14.000Z","size":2131,"stargazers_count":85,"open_issues_count":2,"forks_count":30,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T00:16:40.069Z","etag":null,"topics":["gin-gonic","restful","tmm","website"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ffhelicopter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-21T04:50:13.000Z","updated_at":"2024-04-07T05:45:39.000Z","dependencies_parsed_at":"2024-06-18T22:54:41.725Z","dependency_job_id":"93ce4665-78cd-49c7-a980-3fcb5b355a48","html_url":"https://github.com/ffhelicopter/tmm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffhelicopter%2Ftmm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffhelicopter%2Ftmm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffhelicopter%2Ftmm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ffhelicopter%2Ftmm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ffhelicopter","download_url":"https://codeload.github.com/ffhelicopter/tmm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248497818,"owners_count":21113984,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gin-gonic","restful","tmm","website"],"created_at":"2024-09-24T14:15:10.407Z","updated_at":"2025-04-12T00:16:48.111Z","avatar_url":"https://github.com/ffhelicopter.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tmm\n快速搭建WEB站点以及提供RESTful接口 。\n\n## 一：静态资源站点\n\n\t\n\n``` go\n\trouter := gin.Default()\n\n\t// 静态资源加载，本例为css,js以及资源图片\n\trouter.StaticFS(\"/public\", http.Dir(\"D:/goproject/src/github.com/ffhelicopter/tmm/website/static\"))\n\trouter.StaticFile(\"/favicon.ico\", \"./resources/favicon.ico\")\n```\n\n## 二：动态站点\n模板可调用静态资源站点的css，图片等\n\n\t\n\n``` go\n// 导入所有模板，多级目录结构需要这样写\n\trouter.LoadHTMLGlob(\"website/tpl/*/*\")\n\t\n\t// website分组\n\tv := router.Group(\"/\")\n\t{\n\n\t\tv.GET(\"/index.html\", handler.IndexHandler)\n\t\tv.GET(\"/add.html\", handler.AddHandler)\n\t\tv.POST(\"/postme.html\", handler.PostmeHandler)\n\t}\n```\n\n## 三：中间件的使用，在API中可能使用限流，身份验证等\n\n\n\t\n\n``` go\n\t// 中间件 Go语言的net/http包特别容易构建中间件。\n\t// Gin提供了类似的中间件。需要注意的是中间件只对注册过的路由起作用。\n\t// 可以限定中间件的作用范围。大致分为全局中间件，单个处理程序中间件和组中间件。\n\n\t// 使用全局CORS中间件。\n\t// router.Use(Cors())\n\t// 即使是全局中间件，在use前的代码不受影响\n\t// 也可在handler中局部使用，见api.GetUser\n\n\t// 身份认证中间件，对于API，我们可以考虑JSON web tokens\n\n\t//rate-limit 限流中间件 \n\tlmt := tollbooth.NewLimiter(1, nil)\n\t//lmt := tollbooth.NewLimiter(1, \u0026limiter.ExpirableOptions{DefaultExpirationTTL: time.Hour})\n\t//lmt.SetHeader(\"X-Access-Token\", []string{\"abc123\", \"xyz098\"})\n\t//lmt.SetBasicAuthUsers([]string{\"sansa\"})\n\tlmt.SetMessage(\"服务繁忙，请稍后再试...\")\n\t//tollbooth.LimitByKeys(lmt, []string{\"127.0.0.1\", \"/\"})\n```\n\n## 四：RESTful API接口\n\n\t\n\n``` go\n// 组路由以及版本控制\n\tv1 := router.Group(\"/v1\")\n\t{\n\t\t// 下面是组中间件的用法\n\t\t// v1.Use(Cors())\n\n\t\t// 单个中间件的用法\n\t\t// v1.GET(\"/user/:id/*action\",Cors(), api.GetUser)\n\n\t\t// rate-limit\n\t\tv1.GET(\"/user/:id/*action\", LimitHandler(lmt), api.GetUser)\n\n\t\t//v1.GET(\"/user/:id/*action\", Cors(), api.GetUser)\n\t\t// AJAX OPTIONS ，下面是有关OPTIONS用法的示例\n\t\t// v1.OPTIONS(\"/users\", OptionsUser)      // POST\n\t\t// v1.OPTIONS(\"/users/:id\", OptionsUser)  // PUT, DELETE\n\t\t/*\n\t\t\t// 对应的handler中增加处理\n\t\t\tfunc OptionsUser(c *gin.Context) {\n\t\t\t    c.Writer.Header().Set(\"Access-Control-Allow-Methods\", \"DELETE,POST,PUT\")\n\t\t\t    c.Writer.Header().Set(\"Access-Control-Allow-Headers\", \"Content-Type\")\n\t\t\t    c.Next()\n\t\t\t\t...\n\t\t\t}\n\n\t\t*/\n\t}\n```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffhelicopter%2Ftmm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fffhelicopter%2Ftmm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fffhelicopter%2Ftmm/lists"}