Why I chose Lua for this blog
(andregarzia.com)143 points by nairadithya 14 hours ago
143 points by nairadithya 14 hours ago
I miss the days when "implement your own blogging engine" was one of the most popular learning projects for engineers.
We should bring that back! Its such a great way to play around with client- and server-side development options in an almost zero-risk environment.
Still a good project when one learns a new programming language.
yep. What makes it good is that it's an easy concept to imagine the use cases, but contains subtle details that covers the breadth and depth to gain the experience you need/want. Getting it wrong is not a death sentence (as long as you quarantine the project to it's own, rather than make it part of your home setup...).
Everything web-facing, if it's not a static website delivered by a well-tested web server, happens in a high-risk environment. And doubly so, if, like in this case, stuff like custom cgi libraries are involved. One has to be either very confident in their skills to do that or very, very brave.
My point here is that if someone breaks your blog, they've broken your blog. The blast radius of that should be strictly limited.
Obviously don't go rolling your custom CGI scripts on a server that also hosts your personal email - but these days we are spoiled for choice in terms of isolated hosting strategies for a blog.
Heroku, Vercel, Cloudflare Workers, Fly.io, GitHub Pages, a $5/month VPS...
Philosophically: L'État, c'est moi, build your crappy cgi scripts with nginx or apache all from the CLI and all in vim and you will understand.
Practically: Ports 22, 80, and 443 open and directly accessible from 0.0.0.0/0 is extremely manageable.
Yes, if they do it for fun.
If they do it for money they will install something else and run it in parallel with the blog of the owner of the server. If they are good they'll be lean with resources and not get noticed for months or years. Example: I don't run ps -ef on my server very often and even if I did there are things that can be hidden from ps. It can get complicated.
In any web application all data that comes from the outside world is potentially hostile. A decent web framework takes care of basic security measures, does input sanitation, provides referer checking and csrf for forms, etc. When you roll your own, your _are_ on your own to do that all properly yourself, if you even know all the potential pitfalls. And if you write your own cgi library like the op even more so. I'm not advocating for using WordPress either. I'm advocating for either having a static blog or using a decent, tested web framework or the the very least cgi module that provides tested implementations of common security features that in my experience are typically missed in self-made cgi scripts.
As a a javascript dev I kinda feel you and agree that the tools are moving faster than most codebases. The thing is though, you don't really have to use that many tools unless you really want to.
You can spin up a node server yourself and use web components and it will run fine probably just as long as the lua code will. It's also way easier today to just use web components and not any framework since LLMs can help you speed up the development.
I use Lua for almost all my custom tools these days.
Fennel looks quite great! And I love Lisp so there is definitely some allure there. I don't use it for mostly the reasons mentioned in OP:
* to minimize dependencies. Lua < Lua + Fennel. I'm more extreme than OP in that I don't even use LuaRocks. When I need a library I copy it in, and I pick a library that won't change often so that is a reasonable approach. I try to avoid native libraries.
* for even greater stability. Fennel is pretty stable, but I use Lua 5.1 for the most part which hasn't changed since 2008 or so. I'm more extreme than OP in even avoiding later versions of Lua.
Bottomline: the reasons I like Lua have nothing to do with syntax and are much more about these operational meta characteristics of the language. If I cared more about syntax I'd be on Fennel in a heartbeat.
I use Lua the same way, without LuaRocks. I use a Makefile to run my programs on Lua 5.1~5.4 and LuaJIT and compare the output files, to ensure portability across versions.
Lua 5.1 to 5.2 was a fairly significant breaking change; one that has forked the community to this day with luaJIT never coming on board. 5.2 to 5.3 also broke things with the introduction of integers but mostly at the level of bindings. There is also very little included in terms of standard library and while luarocks exists many significant packages go abandoned. There are breaking language changes in the upcoming 5.5 as well though they are relatively minor.[1]
All to say I think if long term compatibility is the primary goal there are probably better languages.
Have you already discounted php or perl?
Why stay on the upgrade treadmill? For such a minimal language, are the updates really that compelling?
NeoVim is committing to 5.1 and leaving it at that.
Sure that's an option, most distros continue to include every lua version back to at least 5.1; and since luaJIT stayed there a lot of the rest of the community did too.
I guess I'm not sure what advantage lua has in that regard: you could stick to an old version of any language, including node, which was called out as being hard to keep up with.
The simple interpreter seems worth a lot. The official one is under 20k lines. There are reimplementations in many other host language (Go,Rust,JS, etc). Meaning it should be possible run Lua code forever without maintaining a full legacy virtual machine OS. I am not sure I can compile Node today, let alone N years from now as compilers and platforms shift.
I don't know man, every time I tried to learn Lua (to write nvim plugins and HammerSpoon spoons) I disliked the ergonomics of the language. I don't understand why people say it's an easy language—
easy ≠ simple
Again, tastes. I found Erlang to be one of the easiest languages I ever used. I just immediately "got" it. Unfortunately I've had very few professional opportunities to use it.
Erlang has multiple higher level barrier of entries than other languages. For starting, its syntax.
There are languages that compile to Lua. Have you investigated those? For example, you can use a TypeScript syntax and pick up free typechecking along the way: https://typescripttolua.github.io/
There's also the venerable MoonScript: https://moonscript.org/
And YueScript, a personal fave: https://yuescript.org/doc/
A whole list: https://github.com/hengestone/lua-languages
it's easy, but the 1 indexes and global by default suck
The 1 indexes are only a difference from what you're used to. Lua was made by mathematicians, who of course wanted to address the first element as 1, the second element as 2, etc.
0-indexing makes sense in the context of C where the index operator is syntactic sugar for pointer arithmetic. In higher-level languages like C# and Python and others, it's pretty much just a leftover habit from C devs that we all got used to.
Global by default is a perpetual issue, agreed.
You are correct that 1-based indexing is the norm among mathematicians, but none of the creators of Lua is a mathematician, AFAIK. You can read about the early history of Lua here:
Lua is the scripting language of [Recoil]. I've been writing some fairly complex game code on it for a few years now, I lost a lot of prejudice over time on 1-indexing.
In fact, at least for this application I've come to enjoy its practicality!
Why not choose Hugo (https://gohugo.io) or Zola (https://www.getzola.org ?. Both are pretty well-supported by communities and have tonnes of blog themes. (Hugo has a truckload and is the top-3 widely deployed SSG's)
I don't have a background in web development and have a genuine question.
> Your blog is your place to experiment and program how you want it
I 100% agree with your statement and people don't need to justify their hobbies. I've done really pointless things simply for lolz and because I wanted.
My question arises because I was surprised in how ... architected and (dare I say) complex the tech stack in your blog is. In my blogging days I wrote my own HTML/CSS and published it on a Internet facing server. Later, I've used CSS templates and Markdown-to-HTML to generate the static content. What is the purpose of Lua and having a database and all the other complexity for what seems like a static blog? Again, "because I wanted to experiment" or "sharpen my skills" is a totally valid answer but seeing I don't have a background in web development I am inquiring to see if there is a technical reason for doing this. Would be curious to learn what, if any, technical problem warrants such a set up :)
What are your thoughts on something like arturo which I know is quite recent but it has a lot of features for scripting and an argument might in fact be made that it is in fact it might have too many batteries but it was an absolute pleasure to learn and I had a lot of aha moments in their discord server and the community was really pleasant to follow through actually.
I know its definitely smaller but I just want your opinions on it and what you might think of the language and I may be a bit sorry if this comes across as a little off topic but your blog really reminded me of arturo and my attempts on creating something like hugo in arturo but the project was abandoned mid way but if I remember correctly it was just some 50 lines of code to convert from markdown to complete website or even less since arturo's battery include markdown syntax as well as well as a web server and its written in nim which I cherish too.
I am genuinely interested in your opinions about it!
Took a while to find examples:
https://arturo-lang.io/documentation/in-a-nutshell/
It looks interesting.
Why would you build your blog to fail if some article on it ever gets popular? The fact that the most hits you ever received was 50k in a week isn't relevant; a single important post could receive that in seconds.
It basically costs nothing to pre-render a static site, which then serves several orders of magnitude faster. I'm confused why anyone would do it this way in this day and age.
People like to tinker. It's fine. If it blows up one time, then decide what to do from there.
I spend a lot of time blogging but all I use is a ~50 line Python file that converts my markdown pages to HTML, adds my template, and generates the ToC page. Then I push to GitHub Pages.
I can't imagine needing more than that. Why are these blog stacks so complex?
Speaking for myself at least, after you've been blogging for a quarter century or more there are some nice features you might want like pagination of your table-of-contents, RSS feeds (do it, everyone should do it), support for redirects so that ancient links mostly work across those decades (I've kept redirects from like three or four blogging systems now), tags pages for finding lost treasures and silly things. I been on both sides of "needing" comments tools over the decades, similar with things like WebMentions. With so much of blogging on social media WebMentions don't seem that big a deal this decade as it was in the one where every other person (in college) had at least one Blogger.com Blog or LiveJournal and a lot of discussions were cross-links between blogs.
Admittedly most of my blogging history has been something of a path towards simplification from hand-rolled PHP+MySQL, with custom "forum code" markup language, stuff before "blogging" was even an agreed upon term for it (and before Markdown was anywhere near as pervasive), to complex third-party beasts like Drupal, to homegrown Python (and reStructuredText), to very simple SSG tools (these days still Jekyll, but I don't like working in Ruby much, so I keep debating a switch to Lume but I don't think its Redirects plugin is yet compatible enough with GitHub Pages for my liking and I haven't tested its RSS support yet, both of which are personal hard requirements).
I had that thought too. My own blogging engine is ~100 lines of lua that accomplishes the same as you describe, plus RSS, with one additional library for markdown parsing. The author mentions Mustache templates and WebMentions, but ten dependencies still seems like a lot; I wonder what they are.
I love the Lua language, has all the nice to have data structures, no need to think about memory management, is straightforward (keep the whole thing in your head easily) and will go forever without changes. Just stick with v5.1.
It’s so cool to take the interpreter source, type make and be using it in one minute.
I've been thinking of how to make a blog simple recently, and I came across xslt. It looks really cool and seems pretty set in stone, so I thought I'd ask, what are the advantages/drawbacks of making your own tech stack versus xslt? At first glance, it seems perfectly able to handle rss and other simple linking patterns, and pretty much anything can easily be turned into an xml then xslt could be used to generate an html (server-side, or rather writer-side, not like the blog is gonna change) that you serve?
XSLT might be removed from the HTML spec soon, see discussion here: https://news.ycombinator.com/item?id=44952185
XSL is neat, and it is a functional language, but between XSL and XPath, it is quite verbose. Here's a small section of XSL I use to generate my website (not my blog):
<xsl:choose>
<!-- ... other code -->
<xsl:when test="name(.) = 'subsection'">
<xsl:choose>
<xsl:when test="not(boolean(ancestor-or-self::*/@next)) or ancestor-or-self::*/@next != 'rev'">
<xsl:if test="boolean(following-sibling::subsection[@listindex != 'no']/attribute::directory)">
<link rel="next" href="../{following-sibling::subsection[@listindex != 'no']/attribute::directory}" title="{following-sibling::subsection[@listindex != 'no']/child::title}"/>
</xsl:if>
<xsl:if test="boolean(preceding-sibling::subsection[@listindex != 'no'][position()=1]/attribute::directory)">
<link rel="prev" href="../{preceding-sibling::subsection[@listindex != 'no'][position()=1]/attribute::directory}" title="{preceding-sibling::subsection[@listindex != 'no'][position()=1]/child::title}"/>
</xsl:if>
<link rel="first" href="../{../subsection[@listindex != 'no'][position()=1]/@directory}" title="{../subsection[@listindex != 'no'][position()=1]/title}"/>
<link rel="last" href="../{../subsection[@listindex != 'no'][position()=last()]/@directory}" title="{../subsection[@listindex != 'no'][position()=last()]/title}"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="boolean(preceding-sibling::subsection[@listindex != 'no'][position()=1]/attribute::directory)">
<link rel="next" href="../{preceding-sibling::subsection[@listindex != 'no'][position()=1]/attribute::directory}" title="{preceding-sibling::subsection[@listindex != 'no'][position()=1]/child::title}"/>
</xsl:if>
<xsl:if test="boolean(following-sibling::subsection[@listindex != 'no']/attribute::directory)">
<link rel="prev" href="../{following-sibling::subsection[@listindex != 'no']/attribute::directory}" title="{following-sibling::subsection[@listindex != 'no']/child::title}"/>
</xsl:if>
<link rel="first" href="../{../subsection[@listindex != 'no'][position()=last()]/@directory}" title="{../subsection[@listindex != 'no'][position()=last()]/title}"/>
<link rel="last" href="../{../subsection[@listindex != 'no'][position()=1]/@directory}" title="{../subsection[@listindex != 'no'][position()=1]/title}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<!-- ... other code ... -->
</xsl:choose>
And yes, there is other code I've omitted for brevity. This is used to generate the navigation links for the site. I initially write this ... prior to 2009 (that's when I moved it into git). There have been some minor fixes to the XSL over the years, but it's largely unchanged (for a reason that I hope is obvious). Yes, I still use it, because it still works, and it's for a static website.> Your blog is your place to experiment and program how you want it.
I guess this is his why ;-)
I manage to embed lua into my project. https://rapidforge.io its incredible language I don't understand why would you need dsl if you have something like lua to use
I liked this post, and I can totally understand where you’re coming from…
But couldn’t anything you say about Lua also be said about JS? You mentioned how Lua wasn’t batteries included, so you try to limit your libraries. Couldn’t you say the same for JS? JS itself doesn’t change much, it’s the ecosystem. Couldn’t you just pick out some small and stable libraries the same way you could with Lua?
For me, it isn't 100% language warts. It is the customer experience that matters the most for me. Can I compile and ship my products to my customers without having them to install a VMs, container runtime or a language runtime? That the question that is critical for me.
I agree, I think that you must really like golang and how easy it can make cross compilation and how fast it is to build.
Golang has one of the best developer experiences and there are only very very few minor nitpicks I might have of the language but the whole ecosystem on packaging software and what not is just so easy and I love golang.
You may want to benchmark lua versus luajit if you are writing scripts or other short-lived programs.
JIT-compiled languages aren't generally faster in starting up, they generally are used to speed up long-lived programs that have hot sections.
LuaJIT bucks the trend of slow-warmup JITs. It is extremely quick to compile and load, and its interpreter is very fast -- faster than the JIT-compiled code from LuaJIT v1 IIRC, and certainly faster than the interpreter of Lua.
It wasn't until LuaJIT that I realized that JIT didn't inherently have to be these slow lumbering beasts that take hundreds of milliseconds just to wake from their slumber.
Yet I've witnessed Lua 5.1 launching faster than luajit for some of my use cases.
My point still stands though. Don't just use LuaJIT thinking it will magically make things faster in all cases. If you are embedding, LuaJIT is a no-brainer. If you are using a stand-alone interpreter, measure if you care about reality.
You could also do something like this, have Caddy webserver parse your md files through a template
https://github.com/dbohdan/caddy-markdown-site/blob/master/C...
^^ the above combined with caddy git fs to have your md files cloned in memory and refresh every X interval is kind of magical. Git push a new md file and wait X minutes and your website updates.
https://github.com/mohammed90/caddy-git-fs
====
Or a one-file FastHTML (python web framework) solution:
https://gist.github.com/simonMoisselin/f63c52f087704c99b6a62...
ha - I'm well behind you trying to get my blog from Mullenweg hell to pure Raku
I guess it provides less freedom/flexibility on the dev side. You're forced to use LuaJIT, and you're forced to buy into OpenResty's coroutine paradigm where global variables act weird, and stuff like that. Also I bet it doesn't play super nice with LuaRocks.
Totally valid choice to make, but in my opinion OP is missing out. OpenResty is state of the art and has a ton of great libraries embedded in it. It's "batteries included" so to speak, and the batteries are well designed. Yichun Zhang is one of the GOATs, along with Mike Pall. And Roberto, obviously.
Can the redbean webserver dynamically parse markdown?
I am not sure why one would have to use an engine for this. A blog consist of mostly repetitive list of posts made from a single identical template. Any 10y old kid could just master it enough to write them in html directly. And pandoc is just installed in seconds from any package manager if one wants to use an even easier different markup language. Heck in age of microblogging and social medias most blogs aren't even seeing a new post every week, barely anyone is on dialup anymore[1] and those who do probably won't load images automatically, one could have all their posts in a year on a single html page and just anchor tags. This is certainly the case from the blog linked here.
A shell function is enough to call pandoc, update any kind of index (date or tag based) and an rss page really. I would hardly call that an engine, it is just an helper to not forget to update indexes and feeds.
[1] loading speed was the main reason blog engines used to split posts in single pages