Introducing the Macaw Framework: A Simple, Evolving Ruby Web Framework
Tags: ruby, webdev, backend, programming
Published: 2023-04-30 19:49:06

Hello there! I am excited to introduce you to the Macaw Framework, a Ruby-based web framework that I have been passionately developing. The focus of Macaw is on simplicity and ease-of-use, while still providing powerful features. In this post, I will showcase the framework's core features and guide you through building a basic web application using Macaw. Let's get started!

## Setting up Macaw

To begin, install the Macaw gem and create a new file named app.rb. In this file, require the Macaw Framework and create an instance of the Macaw class:

```ruby
require "macaw_framework"

app = MacawFramework::Macaw.new
```

## Creating HTTP Endpoints with Ease

Macaw offers straightforward methods for creating HTTP endpoints, such as get, post, put, patch, and delete. Each method accepts a path and a block of code that's executed when a request is made to the path. For example, to create a GET endpoint at the path /hello:

```ruby
app.get("/hello") do |context|
  "Hello, world!"
end
```

## Rate Limiting: Simple and Effective

Enabling rate limiting is easy. Just update your application.json configuration file with the desired rate limiting settings:

```json
{
  "macaw": {
    ...
    "rate_limiting": {
      "window": 60,
      "max_requests": 100
    },
    ...
  }
}

```
With this configuration, clients will be limited to a maximum of 100 requests per minute.

## SSL Support: Secure Your Application

To enable SSL, add the following to your application.json:

```json
{
  "macaw": {
    ...
    "ssl": {
      "cert_file_name": "path/to/your/cert.pem",
      "key_file_name": "path/to/your/key.pem"
    },
    ...
  }
}
```

Ensure you replace the file paths with the actual paths to your SSL certificate and private key files.

## Caching: Improve Performance

To enable caching for a specific endpoint, set the cache parameter to true when creating the endpoint:

```ruby
app.get("/cached_hello", cache: true) do |context|
  "Hello, world! (cached)"
end
```

Cache also needs some configuration on the application.json file:

```json
{
  "macaw": {
    ...
    "cache": {
      "cache_invalidation": 3600
    },
    ...
  }
}
```

## Monitoring with Prometheus: Stay Informed

To enable Prometheus monitoring, add the following to your application.json:

```json
{
  "macaw": {
    ...
    "prometheus": {
      "endpoint": "/metrics"
    },
    ...
  }
}
```

This will enable Prometheus monitoring for your application.

## Starting the Server: Ready, Set, Go!

To start the Macaw server, simply call the start! method:

```ruby
app.start!
```

This will start the server on the configured host and port.

In summary, the Macaw Framework provides a simple and flexible API for building web applications in Ruby. I am proud of the progress made so far; nevertheless, there is always room for growth and improvement. Check out the Macaw Framework's GitHub repository [here](https://github.com/ariasdiniz/macaw_framework) and dive into the code! I invite you to join me on this journey and contribute to the Macaw Framework's development, so if you have some time to test and experiment with it, give me your feedback!