Euruko 2023

Sinatra Is All You Need!

Sinatra Is All You Need!

by Alexander Nicholson

In the talk titled "Sinatra Is All You Need!" presented at Euruko 2023, speaker Alexander Nicholson explores the advantages of using Sinatra, a lightweight framework for building web applications, over a more extensive framework like Rails. He emphasizes that developers can save time and resources by opting for Sinatra at the early stages of development. Nicholson provides a practical demonstration by showcasing the creation of a conference billboard application using Sinatra.

Key points discussed in the video include:

- Introduction to Sinatra: Described as a DSL (Domain Specific Language) for web applications, Sinclair is simple to use, allowing rapid prototyping and minimal setup for small applications or APIs.

- Simplicity Over Complexity: He compares Sinatra to a bicycle and Rails to a car, suggesting that while bicycles serve well for certain tasks, they might eventually be traded for cars as needs grow.

- Practical Example: The application built during the talk features a billboard where attendees can post comments about the conference, showcasing the utility of Sinatra and Rack support.

- HTMX Integration: Nicholson explains how HTMX enhances the application by enabling dynamic HTML updates without page reloads, facilitating real-time comments.

- Production Preparation: Discusses how to prepare the Sinatra application for production, including setting the environment and utilizing Puma for scaling.

- AI Moderation of Comments: Incorporates OpenAI's moderation endpoint to filter user-generated comments, demonstrating an effective way to manage content with a low barrier to entry.

- Live Engagement: During the presentation, live data on posted comments and engagement showcases the effectiveness of the setup.

- Considerations for Scaling: Nicholson notes that if the application scales or requires a database, options include MySQL or MongoDB, and he recommends using the Ruby ORM Sequel.

- Gradual Transition to Rails: Finally, he points out that as the application grows in complexity, developers can migrate to Rails while maintaining their Sinatra setup.

In conclusion, the talk posits that Sinatra is not just a lightweight alternative but an effective solution for quick application development. It offers a pathway for developers to start small and scale up when necessary, leveraging modern tools like HTMX and AI to enhance functionality at the outset. The use of Tech GPT and GitHub Copilot is also highlighted as resources for generating code quickly and efficiently.

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!