From b0fbfdcae3360c515330e7a337e8080437583b57 Mon Sep 17 00:00:00 2001 From: Catalin Constantin Mititiuc Date: Mon, 16 Jun 2025 20:18:05 -0700 Subject: [PATCH] WIP: routes??? --- conf/nginx.conf | 33 +++++++++++++++++++++++-------- routes.lua | 8 +++++--- routes.moon | 52 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 14 deletions(-) diff --git a/conf/nginx.conf b/conf/nginx.conf index 57ae3f5..b38620f 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -16,22 +16,39 @@ http { include mime.types; charset utf-8; - location / { - default_type text/html; + location = /posts { + # rewrite ^ /posts/ last; # loads "/posts/" + # rewrite ^ /posts/ break; # loads "/posts/index.html" + rewrite_by_lua_block { + -- equivalent to `rewrite ^ /posts/ break;` + ngx.req.set_uri("/posts/", false) + } } - location /foo { - default_type text/html; - - rewrite_by_lua_block { - routes = require "routes" - routes:dispatch() + location = /posts/ { + content_by_lua_block { + return ngx.redirect("/posts", ngx.HTTP_MOVED_PERMANENTLY) } } + location /posts/ { + content_by_lua_block { + require "routes":dispatch() + -- ngx.req.set_uri("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html") + -- ngx.req.set_uri(ngx.var.uri, false) + -- ngx.req.set_uri("/posts/index.html", false) + } + + # rewrite ^ /posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html break; + } + location /css { expires 1h; alias css; } + + location /blah { + rewrite ^/posts$ /posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html break; + } } } diff --git a/routes.lua b/routes.lua index 8f28922..0e6179a 100644 --- a/routes.lua +++ b/routes.lua @@ -1,6 +1,8 @@ local route = require("resty.route").new() -route:get("/foo", function(self) - local uri = "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" - return ngx.req.set_uri(uri, true) +route:get("=/posts/index.html", function(self) + return ngx.exec("/posts/index.html") +end) +route:get("=/posts/", function(self) + return ngx.req.set_uri("/posts/index.html", false) end) return route diff --git a/routes.moon b/routes.moon index 21d81ae..a52a8a8 100644 --- a/routes.moon +++ b/routes.moon @@ -1,7 +1,53 @@ route = require("resty.route").new! -route\get "/foo", => - uri = "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" - ngx.req.set_uri(uri, true) +-- flag = last: true, break: false + +route\get "=/posts/index.html", => + -- ngx.req.set_uri("/posts/index.html", false) + ngx.exec("/posts/index.html") + +route\get "=/posts/", => + -- ngx.req.set_uri("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html", false) + -- ngx.exec("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html") + -- ngx.say("resty-route " .. ngx.var.uri) + -- ngx.req.set_uri(ngx.var.uri, false) + -- @render "somecontent", @context + ngx.req.set_uri("/posts/index.html", false) + -- ngx.req.set_uri("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html", true) + -- ngx.say ngx.var.uri + -- ngx.exec("/about.html") + -- ngx.exec("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html") + +-- route\get "#/posts/([%w%-%_]+)[.](%w+)", (file, ext) => + -- ngx.req.set_uri("/posts") + -- ngx.say ngx.var.uri + -- ngx.exec "/posts/#{file}.#{ext}" + -- ngx.req.set_uri("/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html") + +-- route\get "#/posts/([%w%-%_]+)", (id) => +-- ngx.say "

hello

" +-- route\get "#/posts/(%w+)", (id) => + -- ngx.say file + -- ngx.exec "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" + +-- route\get "=/posts", => +-- route\get "#/posts/([%w%-%_]+)", (id) => + -- ngx.exec "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" + -- ngx.req.set_uri("/posts") + -- ngx.exec "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" + -- router\to "/about.html", "get" + -- require("moon").p route + -- { one: "two" } + -- ngx.say [k .. v for k, v in pairs { one: "two" }] + -- keys = [k .. "\n" for k,v in pairs @] + -- keys = [k .. "\n" for k,v in pairs @] + -- ngx.log(ngx.ERR, @) + + -- ngx.say [k .. v for k, v in pairs route] + -- ngx.say type(route) + -- uri = "/posts/2023-08-03-recursively-list-all-files-in-a-directory-with-elixir.html" + -- ngx.req.set_uri("/about.html") + -- ngx.say "

/posts

" + -- @to "/", "get" route