One web concept for all programming languages
Depending on the context, I program for backend in different languages. Professionally, I am a Java programmer, but I have been doing personal projects in Scala and Python. Lately, I have also been keeping an eye on JavaScript frameworks to keep up with the isomorphic applications trend.
When jumping through all those languages I usually end up needing to search for a new backend web framework. So, which one is my favourite? Although I never wrote a single line of Ruby, my favourite framework is Sinatra. Sinatra is a lean framework with a simple and powerful conceptual model. Its concepts are so powerful that have been used as inspiration for several other frameworks in different languages.
Switching between programming languages is hard, but if you needed to jump between big frameworks, for example, from Spring to Django, there are a lot of different concepts that you would have to learn. By using Sinatra inspired frameworks, switching contexts is easier since some concepts will be shared.
Sinatra for Ruby
In Sinatra, the inspiration for the rest of this post, an HTTP endpoint is created with a Domain Specific Language (DSL):
get ‘/hi’ do
“Hello World!”
End
This code is simple to read: When a call to the HTTP method ”get” is made on the ”hi” endpoint, return “hello world”. Each framework translated this DSL to fit the specific language idioms, but they all kept this simplicity.
Spark for Java
If you come from the Java world, which is still leaving the XML configuration era, Spark will blow your mind!
public class HelloWorld {
public static void main(String[] args) {
get(“/hello”, (req, res) -> “Hello World”);
}
}As you can see from the sample, you can simply write your application inside your main class to declare your endpoints. Simply add the library as a dependency to your project and you are ready to go! Spark comes with a bundled Jetty so you do not need to configure a container.
Spark already existed before Java 8 and the page handlers were classes, with the introduction of lambdas the API was improved and it feels very natural. My only concern with spark is its name which is the same as a popular big data tool. Searching for the keywords “Spark” and “Java” on Google usually leads to the other tool, so it may be a little hard to find proper help online. But don’t consider it as a big risk, the documentation is very good!
Flask for Python
I mostly use Python for scripting. When I think of Python code I think of it as small, clean and powerful. Flask fits this concept really well. I simply create a Python script file, add decorators and I magically have my endpoints running
@app.route(‘/’)
def hello_world():
return ‘Hello World!’
Flask is a little different from the previous framework in the sense that it uses decorators on functions instead of receiving handlers as an argument to a function. It also differs in the fact that the HTTP method is used as an argument to the decorator instead of being the decoration itself. GET is considered by default and you can configure the route decorator with other HTTP methods.
Finatra for Scala
Just by looking at the name Finatra you can already see a Sinatra inspiration. The “Fi” part stands for Finagle, Twitter’s RPC for the JVM.
class HelloWorldController extends Controller { get(“/hi”) { request: Request =>
“Hello World”
}}
Finatra ends up being the fatter on this post. By default, it comes with the concept of Controllers which have the Sinatra-like semantics. The bootstrap of the controllers needs to be configured in an HTTPServer. But its usage is still simple, only add it as a dependency and you are ready to go.
The scala community takes performance very seriously, and all the Finatra handlers return a Future, which means you can use reactive programming with Finatra! To simplify your code you can also return a String as in the example and it is transformed to a Future through implicit conversion.
Express for JavaScript
JavaScript is now the most popular backend technology according to the StackOverflow 2016 survey, and Express is part of this trendy ecosystem.
app.get(‘/’, function (req, res) {
res.send(‘Hello World!’);
});The main difference from the other cases until now is that you are writing directly to the response instead of returning a value. Since JavaScript is mostly an event oriented language this enables you to answer the requests after other asynchronous events. This provides you the same reactive power that is given by Finatra.
One of the coolest parts of the Express trendiness is the creation of out of the box solutions to bootstrap the full-stack of your projects such as MEAN stack.
What if I don’t use any of these languages
Sinatra API is a great conceptual model, and there are others frameworks inspired by it, in some languages there are even multiple frameworks (e.g check Scalatra for Scala)! I only approached those that I already used, but after a five-minute search on Google I was able to find others: Compojure for Clojure, Goji for Go, Opium for OCaml and Nancy for C#.
These frameworks are really thin and good for prototyping, so give a chance to the one in your language! Even if you end up not using them on big projects, they are a valuable tool.