Coder Social home page Coder Social logo

mirai's Introduction

mirai

以 Golang 编写、在 Lua 虚拟机中运行的 HTTP 服务器框架,参考了 express.js 的设计。

An expressjs-like http server framework written in Golang and Lua.

Translation

If you're interested in translating README and documents, start an issue!

简介

Mirai 服务器的设计基本参考了 express.js,以请求方法、路径和处理器组成路由,按先后顺序执行。

app:get("/", function(ctx)
  ctx:send("ok")
end)
app:start()

例子中定义了方法为GET,路径为/,处理器为function(ctx) ctx:send("ok") end的路由。app:start()以非阻塞方式启动服务器。

收到请求后,会从第一个路由或中间件开始尝试匹配。如果请求匹配,服务器调用处理器,并传入与请求的上下文有关的ctx

安装

编译

要编译 Mirai,请安装以下环境:

要为 Windows 平台编译,请同时安装:

运行以下命令开始编译:

task build

中间件

路由与中间件设计可以参考 express.js 中的路由与中间件

-- 中间件在最后会调用 ctx:next() 以继续路由匹配流程。
app:use("/admin/*", function(ctx)
  -- 如果密码等于 abcd1234:
  if ctx.params["password"] == "abcd1234" then
    -- 保存状态 ok 为 true。
    ctx.state.ok = true
  end
  -- 继续执行路由。
  ctx:next()
  -- 当下一条路由执行完毕后,会回到这个位置。
  print("请求处理完毕")
end)

app:get("/admin/portal", function(ctx)
  -- 如果状态 ok:
  if ctx.state.ok then
    -- 返回执行成功的信息。
    ctx:send("authorized!")
  end
  -- 由于响应已经发送,无需继续匹配。
end)

文档

文档是以类型定义的方式呈现的。

要查看文档,请安装 lua-language-server,然后在 插件管理器 中找到 Mirai Server 并安装。

注意

线程安全

由于不同的线程同时访问某一变量可能引起数据竞争,传递值不能在运行中改变。

counter = 0
app:get("/counter/add", function(ctx)
  -- 注意!这里试图改变一个由全局环境创建的值,是错误的。
  counter += 1
end)

mirai's People

Contributors

cloudwindy avatar

Stargazers

 avatar  avatar

Watchers

 avatar

mirai's Issues

项目启动报错

我在app目录下运行mirai时报错,报错信息如下:
file tcp [::]:3000: not supported by windows

panic: runtime error: invalid memory address or nil pointer dereference

运行时出现错误:

panic: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
runtime/debug.Stack()
        /usr/lib/go-1.20/src/runtime/debug/stack.go:24 +0x65
github.com/gofiber/fiber/v2/middleware/recover.defaultStackTraceHandler(0x6?, {0xc0ffe0, 0x1208600})
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/recover/recover.go:12 +0x27
github.com/gofiber/fiber/v2/middleware/recover.New.func1.1()
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/recover/recover.go:31 +0x7a
panic({0xc0ffe0, 0x1208600})
        /usr/lib/go-1.20/src/runtime/panic.go:884 +0x213
github.com/cloudwindy/mirai/pkg/lut/pool.(*LSPool).Put(0x0, 0xc0003cc000)
        /home/skey/workspace/mirai/pkg/lut/pool/pool.go:40 +0x3e
github.com/cloudwindy/mirai/pkg/lue.(*Engine).Close(0x0?)
        /home/skey/workspace/mirai/pkg/lue/engine.go:98 +0x52
github.com/cloudwindy/mirai/pkg/leapp.appHandlerAsync.func1(0xc0b280?)
        /home/skey/workspace/mirai/pkg/leapp/application.go:83 +0x162
github.com/gofiber/fiber/v2.(*App).next(0xc00013b200, 0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:144 +0x1bf
github.com/gofiber/fiber/v2.(*Ctx).Next(0x0?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:909 +0x53
main.worker.func5(0xc0003ba300)
        /home/skey/workspace/mirai/main.go:302 +0x34
github.com/gofiber/fiber/v2.(*App).next(0xc00013b200, 0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:144 +0x1bf
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003ba900?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:909 +0x53
github.com/gofiber/fiber/v2.(*App).registerStatic.func3(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:399 +0x85
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc00002a5a0?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/cache.New.func4(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/cache/cache.go:167 +0x526
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003ba300?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
main.worker.func4(0xc0b280?)
        /home/skey/workspace/mirai/main.go:288 +0x90
github.com/gofiber/fiber/v2.(*App).next(0xc00013b200, 0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:144 +0x1bf
github.com/gofiber/fiber/v2.(*Ctx).Next(0xca9020?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:909 +0x53
github.com/cloudwindy/mirai/pkg/timer.Print.func1(0x0?)
        /home/skey/workspace/mirai/pkg/timer/timer.go:29 +0x11d
github.com/gofiber/fiber/v2.(*Ctx).Next(0x0?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/recover.New.func1(0xc0b280?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/recover/recover.go:43 +0xcb
github.com/gofiber/fiber/v2.(*App).next(0xc00013b200, 0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:144 +0x1bf
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc00029ef30?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:909 +0x53
main.worker.func2(0xc0003ba300)
        /home/skey/workspace/mirai/main.go:239 +0x55
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003ba300?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/pprof.New.func1(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/pprof/pprof.go:42 +0x172
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003a6040?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/compress.New.func3(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/compress/compress.go:55 +0x45
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc00029ef30?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/cors.New.func1(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/cors/cors.go:162 +0x3da
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0002662a0?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/logger.New.func3(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/logger/logger.go:122 +0x395
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003bc000?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/requestid.New.func1(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/requestid/requestid.go:31 +0xfe
github.com/gofiber/fiber/v2.(*Ctx).Next(0xc0003ba300?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/gofiber/fiber/v2/middleware/favicon.New.func1(0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/middleware/favicon/favicon.go:112 +0xa5
github.com/gofiber/fiber/v2.(*Ctx).Next(0xca9020?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/ctx.go:906 +0x43
github.com/cloudwindy/mirai/pkg/timer.Print.func1(0xc0b280?)
        /home/skey/workspace/mirai/pkg/timer/timer.go:29 +0x11d
github.com/gofiber/fiber/v2.(*App).next(0xc00013b200, 0xc0003ba300)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:144 +0x1bf
github.com/gofiber/fiber/v2.(*App).handler(0xc00013b200, 0x49c837?)
        /home/skey/workspace/mirai/vendor/github.com/gofiber/fiber/v2/router.go:171 +0x87
github.com/valyala/fasthttp.(*Server).serveConn(0xc000218200, {0xdd0738?, 0xc0000ae2e8})
        /home/skey/workspace/mirai/vendor/github.com/valyala/fasthttp/server.go:2363 +0x11d3
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002ac000, 0xc0000ab980)
        /home/skey/workspace/mirai/vendor/github.com/valyala/fasthttp/workerpool.go:224 +0xa9
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        /home/skey/workspace/mirai/vendor/github.com/valyala/fasthttp/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh
        /home/skey/workspace/mirai/vendor/github.com/valyala/fasthttp/workerpool.go:195 +0x1b0

Plan: Markdown Support

I'm about to add a markdown to html (and even css) converter and it's going to be implemented in lua API.
I plan to add these functions:

  • Compile markdown to html
  • Similar to 1 but with a simple template engine
  • A handler for images (markdown images probably have some caveats in websites)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.