VulCode

facet

A Rails-style web framework written in Ruby against the standard library only, with no Bundler and no gems. Its own HTTP/1.1 server with WebSockets, routing and resources, controllers and ERB templates, a file-backed ORM with migrations and associations, background jobs, mail and a CLI generator.

TypeWeb framework
LanguageRuby 3.3 or newer
DependenciesStandard library only
TestsMinitest, about 1500
facet

Overview

facet is a Ruby web framework written against the standard library only: no Bundler and not a single runtime gem. It covers the whole stack of a small Rails-style application, from its own HTTP/1.1 server, through routing, controllers and views, to a file-backed ORM with migrations, background jobs, mailers, WebSockets, translations, caching, security helpers, a test toolkit and a CLI generator.

All it needs is Ruby 3.3 or newer. Every library facet uses ships with the language, so an application runs straight from a checkout, or the framework can be built and installed as a gem.

An application in a dozen lines

The simplest application is one class. Routes are declared Sinatra-style, values from the path, the query and the request body all land in one params, and a server started on port 0 asks the system for a free port.

blog.rb · ruby
require 'facet'

class Blog < Facet::Application
  enable_sessions(secret: 'change-me')
  use Facet::Middleware::Logger

  get '/' do
    html('<h1>Facet</h1>')
  end

  get '/users/:id' do
    json(id: params['id'])
  end

  group '/api' do
    get '/status' do
      json(ok: true)
    end
  end
end

server = Facet::Server.new(Blog, port: 3000).start

A bigger application uses the Rails-style layer: controllers with before_action, resources declared with resources and resource with only and except, nested resources, and member and collection routes. The view app/views/posts/index.erb is wrapped in a layout, partials sit next to their view with a leading underscore, and form_for builds a form with validation errors next to the fields.

The path of a request through the framework

Every request passes through the same layers, and each of them can be read in its own file under lib/facet.

Server

its own HTTP/1.1 server on TCPServer accepts the connection, keeps it alive, streams responses and upgrades the connection to a WebSocket on the same port.

Middleware

a Rack-style stack: a logger, signed sessions, static files with ETags, fingerprinted assets, gzip compression, CORS and rate limiting.

Router

matches the method and path, extracts named parameters and splats, and handles constraints, groups, namespaces, filters and named routes with url_for.

Controller

runs the filters, the action and rescue_from, and picks the response format with respond_to based on the Accept header.

View

an ERB-like template with escaping by default, a layout, partials, content_for and fragment caching.

On top of that come conditional GET with ETag and Last-Modified, answering 304 and 412, and byte ranges for static files.

An ORM on a single file

The data layer writes everything to one PStore file that survives a process restart. It has migrations with rollback, transactions with savepoints, unique and composite indexes, validations, associations with has_many :through and polymorphic belongs_to, eager loading through includes, optimistic and pessimistic locking, soft delete, AES-256-GCM attribute encryption and N+1 query detection.

models.rb · ruby
Facet::Orm.connect('db/development.pstore')
Facet::Orm.migrator('db/migrate').migrate!

class Post < Facet::Orm::Model
  validates :title, presence: true, length: { minimum: 3 }
  belongs_to :author, class_name: 'User'
end

class User < Facet::Orm::Model
  has_many :posts, foreign_key: 'author_id'
end

Post.where(title: 'Hello').order(:created_at, :desc).limit(10).offset(20)

It is a choice with a clearly stated limit: every transaction copies the tables, so the store suits small applications and tests, not large data sets or high write rates. There is no SQL adapter.

Everything beyond routing

What else is inside

ModuleWhat it does
Background jobsa persistent PStore queue, perform_later with a run time, retries with linear or exponential backoff, a dead-letter list and a facet jobs work daemon
MailHTML and text templates, attachments, delivery now or later, SMTP and a browser preview
WebSocketshandshake, framing and fragmentation, ping and pong, message size limits and a pub/sub hub
TranslationsJSON locale files, pluralization, fallbacks and the locale from params, the session or Accept-Language
SecurityCSRF, strong parameters, signed and encrypted cookies, sign-in with remember-me per device and access policies
Rate limitingthrottles, blocklists and safelists and a fail2ban-style filter, in memory or in PStore
CLInew, model and migration generators, db:migrate, db:rollback, routes, console and assets:precompile

Tests and limitations

The Minitest suite has about 1500 tests and covers every module. HTTP integration tests start the server on a free port, WebSocket clients connect over real sockets, and the job queue, locking and the CLI have multi-process tests. The repository also holds examples, including a full blog application with scripts that drive it over HTTP.

The limitations are stated plainly. The server starts one thread per connection, speaks plain HTTP/1.1 with no TLS or HTTP/2 and does not enforce request size or time limits, so for traffic from the internet it sits behind a reverse proxy. Templates have no ERB trim modes, and WebSockets have no extensions such as permessage-deflate.

Questions about facet

More projects

More work from the same category - see how we tackle similar challenges.

Have a similar project?

Get in touch - a quote is free and comes back within an hour.