Brut Web Framework
repository·main·Indexed 42 days ago
https://github.com/thirdtank/brutA standards-based web framework for Ruby that emphasizes core web technologies like HTTP, HTML, CSS, and JavaScript. The ecosystem includes the core Ruby gem, brut-css (a minimal utility CSS library version 0.22.0), and brut-js for utility custom elements.
What's inside Brut
- Brut CSS is a minimal utility CSS library that provides single-purpose classes. It is designed to allow developers to prototype and design directly within their HTML or HTML-generation code. It is primarily intended to be used alongside BrutRB to provide immediate UI capabilities without additional setup, though it can serve as a foundation for full-scale CSS solutions.
Overview of BrutCSS
mainBrutCSS is a utility-based CSS library intended for rapid prototyping and designing directly within your HTML. It is not a replacement for CSS, but rather a tool to bootstrap designs using two main components:
- A Design System: Built on CSS custom properties (variables).
- Utility Classes: A set of single-purpose CSS classes for direct application in HTML.
Developers can use the low-level utility classes for speed and then abstract them into components (e.g., using Phlex) or custom CSS classes as their project grows.
Overview of Instrumentation and Observability in Brut
mainBrut provides built-in support for OpenTelemetry, an open standard for observability that allows you to monitor and understand your application's behavior in production.
For development environments, Brut includes a pre-configured setup for otel-desktop-viewer, enabling you to visualize instrumentation data locally.
Configure CLI bootstrapping and environment
mainDepending on whether your CLI needs access to the database or Brut configuration, you must implement specific methods in your
Brut::CLI::Commands::BaseCommandsubclass. These methods apply to each command separately.Requirement Method to implement Description Access Brut config def default_rack_env = "..."Allows access to configuration values based on a specific RACK_ENV(e.g.,development).Access Database/Business Logic def default_rack_env = "..."ANDdef bootstrap? = trueFully bootstraps the application, allowing access to Brut.container.XXXand external data stores.Note: If you do not implement these,
Brut.container.XXXcalls will fail.class NotificationsCLI < Brut::CLI::Commands::BaseCommand def description = "Manages app notifications" def default_rack_env = "development" def bootstrap? = true def run # Brut.container.XXX will work # Business logic will work end endAccessing HTTP Headers via Keyword Injection
mainBrut allows you to access any HTTP header by declaring a keyword argument starting with
http_.Naming Convention: Brut converts the standard Rack/CGI header format (e.g.,
HTTP_USER_AGENT) to lowercase Ruby-friendly names. To access theUser-Agentheader, usehttp_user_agent.Behavior based on argument requirement:
- Required arguments (no default value): If the header is missing,
nilis injected. If present, the value is injected (even if it is an empty string). - Optional arguments (has a default value): If the header is missing, the default value is used. If present, the value is injected.
- Required arguments (no default value): If the header is missing,
Create reusable Components
mainComponents are Phlex components (classes implementing
view_template) used to manage HTML complexity. You can instantiate and render them within any view using therendermethod.class FlashComponent < AppComponent def initialize(flash:) # ... logic to set @role and @message_key end def view_template div(role: @role) do t([ :flash, @message_key ]) end end end # Usage in a page def page_template render FlashComponent.new(flash:) endConfigure and manage the `app/` directory
mainThe
app/directory is the heart of your application. It contains the following key components:bootstrap.rb: A Ruby file that sets up your app and ensures all necessary files arerequired correctly.config/: Application configuration (e.g., localizations and translations). Note: Brut avoids using YAML for configuration.public/: The root directory for public assets served by the application.src/: The location for all application source code.
Core concepts and architectural mental model of Brut
mainBrut organizes web application development around four primary domains. Understanding these boundaries is essential for structuring your application code:
- Client (Client Side): The web browser or HTTP client. This is the environment where CSS is applied to HTML, JavaScript is executed, and HTTP requests are initiated.
- Server (Server Side): The environment where all code not running in the browser executes, including HTML generation and SQL queries.
- Front End: Code responsible for producing the user interface or HTTP API. While this code often runs on the server side, its purpose is to provide the interface.
- Back End: Code responsible for everything else, including database access, business logic execution, and background job management.
Brut is designed to leverage web standards (HTML, CSS, SQL, and the Web Platform) rather than abstracting them away into proprietary DSLs.
Manage database timezones using timestamp with time zone
mainTo avoid ambiguity when reading and storing dates across different system or database timezones, Brut recommends using the
timestamp with time zoneSQL data type.If your system is set to UTC and you store a timestamp using this type, the value will be read back correctly even if the system timezone is later changed (e.g., to
America/Los_Angeles). Usingtimestamp without time zone(or the standard SQLtimestamp) can lead to ambiguity during timezone shifts.Style Constraint Violations
mainBrut uses custom elements for constraint violations. The
<brut-form>element sets asubmitted-invalidattribute when a user attempts to submit an invalid form.To show error messages only after a submission attempt or when they are provided by the server, use the following CSS selectors:
brut-cv: The custom element for individual error messages.brut-form[submitted-invalid] brut-cv: Targets error messages when a form has been submitted and is invalid.brut-cv[server-side]: Targets error messages generated by the server.
/* Hide messages by default */ brut-cv { display: none; color: #A60053; font-weight: bold; font-size: 1rem; } /* Show messages on invalid submission or server-side errors */ brut-form[submitted-invalid] brut-cv, brut-cv[server-side] { display: block; }Understand why Brut is not an MVC framework
mainBrut explicitly avoids the Model-View-Controller (MVC) pattern and does not use 'resources' as a core abstraction.
Unlike MVC frameworks that attempt to map HTTP verbs to specific 'resources' (e.g., mapping
DELETEto a specific resource action), Brut focuses on the practical reality of web development: serving pages and handling requests. This avoids the complexity and ambiguity often found when trying to force-map business logic and database tables into a strict resource-based RESTful structure.Database Schema and Migrations Overview
mainBrut manages database schemas using the Sequel library. While Sequel is compatible with many database systems, Brut's specific extensions and enhancements are currently designed exclusively for Postgres. Ensure your environment is running Postgres before attempting to use Brut's schema management features.