💾 Archived View for gemini.panda-roux.dev › log › entry › 53 captured on 2022-06-03 at 22:57:41. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2022-04-28)

➡️ Next capture (2023-01-29)

-=-=-=-=-=-=-

New gemlog URL format (and how it works)

Posted on Friday April 8, 2022

The URL format that my gemlog uses has typically relied on query parameters to specify the post ID:

/log/entry?53

Which is equivalent to:

/log/entry/index.gmi?53

This is necessary because the Lua scripts in /log/entry/index.gmi use that query parameter to look up the post in a SQLite database.

---

I personally prefer paths that contain resource identifiers themselves, like so:

/log/entry/53

I find this format to be easier to read and more intuitive. However, MoonGem's path resolution logic is very simplistic: if the path ends in what looks like a directory, then it appends "index.gmi" onto the end. As a result, the above would be resolved to this non-existent path:

/log/entry/53/index.gmi

So what I needed was to add some method of rewriting URLs behind the scenes. Here is the process I wanted:

---

Recently I added the ability to specify "pre-request scripts" to MoonGem with the "--before" command-line parameter for situations just like this. Sometimes it's helpful to intercept and modify requests before they make it to the main server logic.

Here's the script I'm using (with comments added for explanation):

-- get the raw request path (i.e. prior to 'index.gmi' being added)
-- this will look like /log/entry/<id> for log entries
local path = mg.get_raw_path()

-- if the path looks like one of the gemlog entry URLs...
if path:find('^log/entry/%d+') then

  -- then yoink the ID from the last segment
  _, _, id = path:find('^log/entry/(%d+)')

  -- set the request's input parameter to that ID
  mg.set_input(id)

  -- set the path to the actual log entry document
  -- (which will use the ID to look up and render the requested gemlog post)
  mg.set_path('log/entry/index.gmi')
end

Here's a link to the actual script file

That's all there is to it. Now my gemlog entries can be accessed in that nicer URL format.

---

Eventually I'd like to break this kind of logic out into a more sophisticated library, in order to make virtual directories and argument binding more intuitive. For my day job I use ASP.NET pretty heavily, so my goal is to have a solution that approximates its route-templating system.

This might look something like:

mg.for_route('/books/{author}/{isbn}').serve(
  function (author, isbn)
    ...
  end function
)

- panda-roux -

next: "Another late night and digital notetaking"

prev: "Question about malicious web requests"

index

home

Leave a comment