{"id":23412529,"url":"https://github.com/cloudwindy/mirai","last_synced_at":"2025-04-09T04:09:21.642Z","repository":{"id":181887007,"uuid":"667608508","full_name":"cloudwindy/mirai","owner":"cloudwindy","description":"An expressjs-like http server framework written in Golang and Lua.","archived":false,"fork":false,"pushed_at":"2023-12-17T07:55:45.000Z","size":5633,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T22:31:14.782Z","etag":null,"topics":["golang","high-performance","http","http-server","lua"],"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/cloudwindy.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":"2023-07-17T22:54:07.000Z","updated_at":"2023-08-08T22:58:08.000Z","dependencies_parsed_at":"2023-12-16T07:36:20.268Z","dependency_job_id":"82f1c506-8cdc-4b56-9fdf-520ffb7634f5","html_url":"https://github.com/cloudwindy/mirai","commit_stats":null,"previous_names":["cloudwindy/mirai"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudwindy%2Fmirai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudwindy%2Fmirai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudwindy%2Fmirai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloudwindy%2Fmirai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloudwindy","download_url":"https://codeload.github.com/cloudwindy/mirai/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974731,"owners_count":21026742,"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":["golang","high-performance","http","http-server","lua"],"created_at":"2024-12-22T18:16:19.977Z","updated_at":"2025-04-09T04:09:21.619Z","avatar_url":"https://github.com/cloudwindy.png","language":"Go","readme":"# mirai\n以 Golang 编写、在 Lua 虚拟机中运行的 HTTP 服务器框架，参考了 express.js 的设计。\n\nAn expressjs-like http server framework written in Golang and Lua.\n\n## Translation\nIf you're interested in translating README and documents, start an issue!\n\n## 简介\n\nMirai 服务器的设计基本参考了 express.js，以请求方法、路径和处理器组成路由，按先后顺序执行。\n```lua\napp:get(\"/\", function(ctx)\n  ctx:send(\"ok\")\nend)\napp:start()\n```\n例子中定义了方法为```GET```，路径为```/```，处理器为```function(ctx) ctx:send(\"ok\") end```的路由。```app:start()```以非阻塞方式启动服务器。\n\n收到请求后，会从第一个路由或中间件开始尝试匹配。如果请求匹配，服务器调用处理器，并传入与请求的上下文有关的```ctx```。\n\n## 安装\n\n### 编译\n\n要编译 Mirai，请安装以下环境：\n* [Golang](https://go.dev/dl/)\n* [Taskfile](https://taskfile.dev/installation)\n\n要为 Windows 平台编译，请同时安装：\n* [MinGW-w64](https://www.mingw-w64.org/downloads/)\n\n运行以下命令开始编译：\n```\ntask build\n```\n\n## 中间件\n路由与中间件设计可以参考 [express.js 中的路由与中间件](https://expressjs.com/zh-cn/guide/using-middleware.html)。\n\n```lua\n-- 中间件在最后会调用 ctx:next() 以继续路由匹配流程。\napp:use(\"/admin/*\", function(ctx)\n  -- 如果密码等于 abcd1234：\n  if ctx.params[\"password\"] == \"abcd1234\" then\n    -- 保存状态 ok 为 true。\n    ctx.state.ok = true\n  end\n  -- 继续执行路由。\n  ctx:next()\n  -- 当下一条路由执行完毕后，会回到这个位置。\n  print(\"请求处理完毕\")\nend)\n\napp:get(\"/admin/portal\", function(ctx)\n  -- 如果状态 ok：\n  if ctx.state.ok then\n    -- 返回执行成功的信息。\n    ctx:send(\"authorized!\")\n  end\n  -- 由于响应已经发送，无需继续匹配。\nend)\n```\n\n## 文档\n文档是以类型定义的方式呈现的。\n\n要查看文档，请安装 [lua-language-server](https://github.com/LuaLS/lua-language-server)，然后在 [插件管理器](https://github.com/LuaLS/lua-language-server/wiki/Addons#vs-code-addon-manager) 中找到 Mirai Server 并安装。\n\n## 注意\n\n### 线程安全\n\n由于不同的线程同时访问某一变量可能引起数据竞争，传递值不能在运行中改变。\n\n```lua\ncounter = 0\napp:get(\"/counter/add\", function(ctx)\n  -- 注意！这里试图改变一个由全局环境创建的值，是错误的。\n  counter += 1\nend)\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudwindy%2Fmirai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloudwindy%2Fmirai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloudwindy%2Fmirai/lists"}