00:00:12.480
Hi everyone, my name is Alexander. I flew here from Tokyo, Japan, so excuse me if I sound a little bit jet-lagged. I'm doing my best. This talk is titled 'All You Need Is Sinatra.' I want to prove that you don't actually need to use a fully-blown Rails framework from day one.
00:00:20.220
I currently live in Tokyo, Japan, where I have been for the past four years. I work as an engineering manager at TableCheck, a restaurant reservation and table management platform. We love building software that venues use every day.
00:00:48.539
So, what is Sinatra? If you don’t know, Sinatra is essentially a DSL for creating web applications with minimal effort. On the right, you can see a basic 'Hello World' example of what we're going to build today. To illustrate the difference, I liken Rails to a car and Sinatra to a bicycle. You can get a lot of use out of a bicycle, but eventually, you're probably going to trade it in for a car.
00:01:03.359
The number one reason to use Sinatra is simplicity. You can quickly prototype any small application or API; you can get a project off the ground within an hour. This can save you thousands to even hundreds of thousands in developer costs because it's easy to get started, and you can always transition to something more complex later on. There's nothing stopping you from doing that.
00:01:50.100
For this talk, we're going to build a Euruko conference billboard where people can post their thoughts about the conference. This implementation is just the most basic example I could come up with; it requires Sinatra. We have one GET endpoint that serves an index.html page, alongside two REST API endpoints for comments: one is a GET that shows all the comments, and the other is a POST that allows users to submit a comment.
00:02:04.200
As you can see, we can still use Rack, so we have full Rack support inside our application. We can utilize utilities for escaping HTML, so if someone inputs something inappropriate, we can strip it out.
00:02:36.120
Next, we need an index.html file. I used HTMX for this because it's very simple to build applications with it. There are two special tags that I've used, prefixed by 'hx', which allow me to set specific parameters that HTMX looks for to swap out different parts of the DOM in the HTML. The great thing about HTMX is that it supports polling by default, so I've set it to automatically call our API every two seconds to fetch new comments.
00:03:05.519
Can we productionize it? Yes! You just need to set the environment variable to 'production', and you can even configure workers with cluster mode for Puma, allowing for significant scaling.
00:03:37.739
On my MacBook Air, keep in mind that I created this presentation this morning, so I didn't have time to properly optimize the load test. However, with just one thread, I was able to achieve around 2,000 requests per second. This isn't massive, but it should support my application for about ten users for a while.
00:04:35.699
Now, let's talk about AI. Everyone's ears probably perked up. I'm going to use an endpoint called moderation from OpenAI. It's very effective because it's free, and you can use it to moderate any content. This is extremely powerful for building applications today. If your app takes in user comments, you can have those fully moderated at no cost.
00:04:58.560
This moderation endpoint provides a list of categories to determine if a comment is abusive, harmful, or mean-spirited. We can reject comments that exceed a certain score—here, I chose a threshold of 0.007. I tested it by entering a comment that suggested being nicer.
00:05:27.060
I've deployed this just 20 minutes ago. If you'd like, please scan the QR code or enter the URL to write some comments. Kindly refrain from posting anything inappropriate, although I hope the AI will manage that.
00:06:03.080
Now, we are getting some engagement. It looks like people are writing a lot of comments, and there have been attempts at code injection. I'm seeing some interesting inputs, like the infamous 'drop table' command. The comments update every two seconds, giving us a live feed of thoughts from the conference.
00:06:56.520
This setup may sound too good to be true, and you might be right. If you need a database, you will likely have to choose between MySQL, MongoDB, or another system. I highly recommend an excellent Ruby ORM called Sequel.
00:07:14.040
Additionally, my company, TableCheck, has its fork of Mongoid, which is the MongoDB driver for Ruby. It has some enhancements that we believe are beneficial.
00:07:39.240
There are also considerations for user management, throttling, caching, and integrations with OpenID, among others. Always remember that you have the power of Rack at your disposal, so these problems can be managed effectively.
00:07:55.259
Even better, you can migrate to Rails if your application becomes too heavy. You can load Sinatra within Rails while retaining your routes, then gradually strip out certain routes as necessary.
00:08:22.140
This entire presentation, including the code, was generated with the help of Tech GPT and GitHub Copilot. So, if you are interested in making easy money, this is definitely a way to go.
00:08:37.020
That's all from me. The GitHub link is available if you'd like to access my code. Please feel free to use it, as it is under the MIT license. Thank you very much!