Skip to content

Commit

Permalink
add-dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangnanscu committed Dec 6, 2024
1 parent c24af29 commit 3397147
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 43 deletions.
106 changes: 65 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,10 @@ npm create resty-app@latest -y
```
Then follow the instructions to complete the project.

# Api

## new

```lua
(method) Router:new()
-> Router
```

create a router.

## insert
# Dependencies

```lua
(method) Router:insert(path: string, handler: string|function, methods?: string|string[])
-> Router
```

insert a route.

## extend

```lua
---@alias Route {[1]:string, [2]:function|string, [3]?:string|string[]}
(method) Router:extend(routes: Route[])
-> Router
```

insert routes to a router.

## match

```lua
(method) Router:match(path: string, method: string)
-> string|function
2. { [string]: string|number }?
```

match a http request
* [OpenResty](https://openresty.org/)
* (Optional) [lfs](https://luarocks.org/modules/hisham/luafilesystem) or [syscall.lfs](https://github.com/justincormack/ljsyscall) - if you want to use `fs` method to load routes from directory

# Synopsis

Expand All @@ -62,6 +27,7 @@ local cnt = 0
local error_cnt = 0

-- Test events
-- for test purpose, assume the nginx is single-threaded
router:on('add', function(ctx)
cnt = cnt + 1
end)
Expand Down Expand Up @@ -169,7 +135,7 @@ end)
router:get("/func", function(ctx)
return function()
ngx.header.content_type = 'text/plain; charset=utf-8'
ngx.say("function called2")
ngx.say("function called")
end
end)

Expand All @@ -186,6 +152,64 @@ end)
return router

```
## reference

- [nginx api for lua](https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#nginx-api-for-lua)
# Api

## new

```lua
(method) Router:new()
-> Router
```

create a router.

## insert

```lua
(method) Router:insert(path: string, handler: string|function, methods?: string|string[])
-> Router
```

insert a route.

## extend

```lua
---@alias Route {[1]:string, [2]:function|string, [3]?:string|string[]}
(method) Router:extend(routes: Route[])
-> Router
```

insert routes to a router.

## match

```lua
(method) Router:match(path: string, method: string)
->
1. string|function
2. { [string]: string|number }?
```

match a http request

## run
```lua
(method) Router:run()
-> nil
```
dispatch http request

## fs

```lua
(method) Router:fs(dir: string)
-> nil
```

load routes from directory (requires `lfs` or `syscall.lfs` module).

# reference

- [nginx api for lua](https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#nginx-api-for-lua)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-resty-app",
"version": "0.5.0",
"version": "0.6.0",
"description": "high performance router",
"type": "module",
"bin": {
Expand Down
5 changes: 4 additions & 1 deletion template/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ router:get('/', function(ctx)
<div class="container">
<h1>πŸŽ‰πŸŽ‰πŸŽ‰πŸŽ‰ Congratulations!</h1>
<p>Your resty application is running</p>
<p>Here are some example routes (requires `lfs` or `syscall.lfs` module):</p>
<div class="api-list">
<div class="api-item">
<a href="/test/hello"><span class="method">GET</span> /test/hello - Simple String Response</a>
Expand Down Expand Up @@ -101,6 +102,8 @@ router:get('/', function(ctx)
end)

-- add routes from lua files in api folder
router:fs('./api')
if pcall(require, "syscall.lfs") or pcall(require, "lfs") then
router:fs('./api')
end

return router

0 comments on commit 3397147

Please sign in to comment.