{"id":21007893,"url":"https://github.com/lflxp/gin-tryfile","last_synced_at":"2025-08-01T13:05:13.099Z","repository":{"id":57649576,"uuid":"446439998","full_name":"lflxp/gin-tryfile","owner":"lflxp","description":"gin middlewares,  just like nginx try-file function","archived":false,"fork":false,"pushed_at":"2022-01-10T15:36:57.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-27T14:56:52.307Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/lflxp.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}},"created_at":"2022-01-10T13:44:29.000Z","updated_at":"2022-08-11T12:27:36.000Z","dependencies_parsed_at":"2022-09-05T13:12:04.864Z","dependency_job_id":null,"html_url":"https://github.com/lflxp/gin-tryfile","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/lflxp/gin-tryfile","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lflxp%2Fgin-tryfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lflxp%2Fgin-tryfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lflxp%2Fgin-tryfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lflxp%2Fgin-tryfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lflxp","download_url":"https://codeload.github.com/lflxp/gin-tryfile/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lflxp%2Fgin-tryfile/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268229230,"owners_count":24217008,"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","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-19T09:10:27.485Z","updated_at":"2025-08-01T13:05:13.068Z","avatar_url":"https://github.com/lflxp.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gin Middleware TryFile\n\nThis project is to solve the problem that the gin framework processes the dynamic routing file in the front-end compilation file and imitates nginx try_file function\n\n本项目是解决gin框架处理前端编译文件中动态路由文件，模仿nginx try_file功能\n\n```\nlocation /images/ {\n    root /opt/html/;\n    try_files $uri   index.html; \n}\n```\n\n# Usage\n\n`func RegisterTryFile(router *gin.Engine, hfs http.FileSystem, staticFileDir ...string)`\n\nParameter analysis\n* router *gin.Engine\n* hfs http.FileSystem\n    * http.FS\n    * http.Dir\n    * http.FS(embed.FS)\n* staticFileDir ...string\n    * only one word as default,which means gin router and static path\n    * include two word,first is gin router,second is static path, default try file is index.html\n    * include three word,first is gin router,second is static path,three is try file name eg: index.html\n# Demo\n\n```\npackage main\n\nimport (\n\t\"embed\"\n\t\"net/http\"\n\n\t\"github.com/gin-gonic/gin\"\n\ttryfile \"github.com/lflxp/gin-tryfile\"\n)\n\n//go:embed dist\nvar distFile embed.FS\n\nfunc main() {\n\tr := gin.Default()\n\t// As Default, Gin Router equal StaticPath\n\ttryfile.RegisterTryFile(r, http.FS(distFile), \"/dist\")\n\n\t// Custom Gin Router and StaticPath\n\t// tryfile.RegisterTryFile(r, http.FS(distFile), \"/static\", \"/dist\")\n\n\t// Fully custom parameters\n\t// tryfile.RegisterTryFile(r, http.FS(distFile), \"/static\", \"/dist/custom\", \"index.html\")\n\n\t// As you wish\n\tr.Any(\"/try/file/*any\", tryfile.WrapHandler(\"/dist\", http.FS(distFile)))\n\tr.Run()\n}\n```\n\n## Verification results\n\n```\n➜  gin-tryfile git:(main) ✗ curl http://127.0.0.1:8080/dist/this/is/not/exist\n\u003chtml\u003e\n    \u003cbody\u003e\n        \u003ch3\u003eHello Gin-TryFile\u003c/h3\u003e\n    \u003c/body\u003e\n\u003c/html\u003e\n➜  gin-tryfile git:(main) ✗ curl http://127.0.0.1:8080/try/file/this/is/not/exist\n\u003chtml\u003e\n    \u003cbody\u003e\n        \u003ch3\u003eHello Gin-TryFile\u003c/h3\u003e\n    \u003c/body\u003e\n\u003c/html\u003e\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flflxp%2Fgin-tryfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flflxp%2Fgin-tryfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flflxp%2Fgin-tryfile/lists"}