Pradeep's Stream

Adding /export endpoint to tohray

I added the /export endpoint to tohray. This allows the user to backup the whole blog in either JSON on Markdown format.

A GET call to /export returns JSON (default format). And user can get all the posts in markdown format by attaching ?format=md to the above endpoint, ie., /export?format=md.

Added to urls.nim:

pattern("/export", exportAll, HttpGet, name="export"),

Added to views.nim:

proc exportAll*(ctx: Context) {.async.} =
  let
    format = ctx.getQueryParams("format", "json")
    db = open(consts.dbPath, "", "", "")
    rows = db.getAllRows(sql"SELECT slug, created, content FROM post ORDER BY created DESC")
  defer: db.close()
  if format == "json":
    ctx.response.addHeader("Content-Type", "application/json")
    var jsonArray = newJArray()
    for row in rows:
        var jsonObject = %*{
          "slug": row[0],
          "created": row[1],
          "content": row[2]
        }
        jsonArray.add(jsonObject)
    resp jsonResponse(%*{"posts": jsonArray})
  elif format == "md":
    ctx.response.addHeader("Content-Type", "text/plain")
    var res:string
    for row in rows:
      res = res & "\n" & row[0] & "\n" &  row[1] & "\n\n" & $rows[2] & "\n\n" & chr(28) # ascii file separator
    resp res

Compiling nim programs with C, CPP and ObjC backends

Nim supports multiple compiler backends like C, C++, ObjC and JavaScript.

I did not try compiling the program with JS because many OS facing APIs are not available to the JS backend.

  • C* -- nim c -d:release tohray -- 2117992
  • CPP -- nim cpp -d:release tohray -- 2443528
  • ObjC -- nim objc -d:release tohray -- NA

NA: ObjC compile failed with:

/Users/pradeep/.choosenim/toolchains/nim-2.0.8/lib/pure/math.nim(281, 11) template/generic instantiation of `isNaN` from here
/Users/pradeep/.choosenim/toolchains/nim-2.0.8/lib/pure/math.nim(204, 20) Error: undeclared identifier: 'c_isnan'
candidates (edit distance, scope distance); see '--spellSuggest':
 (1, 3): 'isNaN'

The CPP generated executable is slightly larger. I do not know if it's faster for the same compilation settings.

tohray

The source of the software that is behind this blog can be found on my github.com/btbytes/tohray. The Readme file has the details on how to install it, and the technologies behind it.

You can leave your questions and feedback to me on X - x.com/btbytes or on the github issues page of the project.

Things I learnt about Nim while writing Tohray

  • Karax's vdom is super sweet for building HTML interfaces. I like it lot more than using any kind of templates now. the mismatch between code and markup(HTML) is jarring after this.

  • I learnt how to write a .nimble for a working project. Nimble had always left me frustrated.

  • I leant about the distros package and specifically the foreignDep method. With this essentially, I was able to replace apt-get install blah with (see below), which captures the package dependency with the code and not in a script or a Dockerfile

import distros
if detectOs(Ubuntu):
  foreignDep "libpcre3-dev"
  foreignDep "libsqlite3-dev"
  foreignDep "build-essential"
  foreignDep "wget"
  • Once or twice, I thought using a class to share variables would have been nicer. I have to study that more.

  • Learnt about using SQLite's Full Text Search, mostly through the help from Anthropic Claude. The FTS felt finicky with it's inability to handle special characters. But, it's a start towards understanding a powerful DB feature - FTS.

  • Got a lot more comfortable with using multi-stage Docker builds

  • Tried my hand at statically building tohray binary. It was coming out to be 20MB ALL INCLUSIVE, which is a great small size for a full fledged web app. However, I could not figure out how to statically link libpcre.

  • The hunt for static building took me to hello_musl, a mere .nims config, but it showed the power of nim scripting. I think the way forward for static linking pcre is to improve the above script.

About

Welcome to the stream. Here you can find me small snippets of things I'm thinking about or woking on, or things I find interesting.

This application is written using Nim, with the help of Karax and Prologue web frameworks. The styling is provided by Terminal.css

You can find me on github, my homepage or on twitter/X.

Calendar , Export