README.md in lotusrb-0.1.0 vs README.md in lotusrb-0.2.0

- old
+ new

@@ -1,9 +1,23 @@ # Lotus A complete web framework for Ruby +## Frameworks + +Lotus combines together small but yet powerful frameworks: + +* [**Lotus::Utils**](https://github.com/lotus/utils) - Ruby core extentions and class utilities +* [**Lotus::Router**](https://github.com/lotus/router) - Rack compatible HTTP router for Ruby +* [**Lotus::Validations**](https://github.com/lotus/validations) - Validation mixin for Ruby objects +* [**Lotus::Model**](https://github.com/lotus/model) - Persistence with entities, repositories and data mapper +* [**Lotus::View**](https://github.com/lotus/view) - Presentation with a separation between views and templates +* [**Lotus::Controller**](https://github.com/lotus/controller) - Full featured, fast and testable actions for Rack + +All those components are designed to be used independently from each other or to work together in a Lotus application. +If your aren't familiar with them, please take time to go through their READMEs. + ## Status [![Gem Version](https://badge.fury.io/rb/lotusrb.png)](http://badge.fury.io/rb/lotusrb) [![Build Status](https://secure.travis-ci.org/lotus/lotus.png?branch=master)](http://travis-ci.org/lotus/lotus?branch=master) [![Coverage](https://coveralls.io/repos/lotus/lotus/badge.png?branch=master)](https://coveralls.io/r/lotus/lotus) @@ -24,355 +38,140 @@ __Lotus__ supports Ruby (MRI) 2+ ## Installation -Add this line to your application's Gemfile: - -```ruby -gem 'lotusrb' -``` - -And then execute: - ```shell -$ bundle +% gem install lotusrb ``` -Or install it yourself as: +## Usage ```shell -$ gem install lotusrb +% lotus new bookshelf +% cd bookshelf && bundle +% bundle exec lotus server # visit http://localhost:2300 ``` -## Usage +## Architectures -Lotus combines the power and the flexibility of all its [frameworks](https://github.com/lotus). -It uses [Lotus::Router](https://github.com/lotus/router) and [Lotus::Controller](https://github.com/lotus/controller) for routing and controller layer, respectively. -While [Lotus::View](https://github.com/lotus/view) it's used for the presentational logic. +Lotus is a modular web framework. +It scales from **single file HTTP endpoints** to **multiple applications in the same Ruby process**. -**If you're not familiar with those libraries, please read their READMEs first.** - -### Architecture - -Unlike the other Ruby web frameworks, it has a flexible conventions for the code structure. +Unlike other Ruby web frameworks, Lotus has **flexible conventions for code structure**. Developers can arrange the layout of their projects as they prefer. There is a suggested architecture that can be easily changed with a few settings. -Based on the experience on dozens of projects, Lotus encourages the use of Ruby namespaces. -In this way, growing code bases can be split without effort, avoiding monolithic applications. +Lotus encourages the use of Ruby namespaces. This is based on the experience of working on dozens of projects. +By using Ruby namespaces, as your code grows it can be split with less effort. In other words, Lotus is providing gentle guidance for **avoid monolithic applications**. -Lotus has a smart **mechanism of duplication of its frameworks**, that allows multiple copy of a framework and multiple applications to run in the **same Ruby process**. -In other words, even small Lotus applications are ready to be split in separated deliverables, but they can safely coexist in the same heap space. +Lotus has a smart **mechanism of duplication of its frameworks**. +It allows multiple copies of the framework and multiple applications to run in the **same Ruby process**. +In other words, Lotus applications are ready to be split into smaller parts but these parts can coexist in the same heap space. -For instance, when a `Bookshelf::Application` is loaded, `Lotus::View` and `Lotus::Controller` are duplicated as `Bookshelf::View` and `Bookshelf::Controller`, in order to make their configurations completely indepentend from `Backend::Application` thay may live in the same Ruby process. -So that, developers SHOULD include `Bookshelf::Controller` instead of `Lotus::Controller`. +All this adaptability can be helpful to bend the framework for your advanced needs, but we recognize the need of a guidance in standard architectures. +For this reason Lotus is shipped with code generators. -#### One file application -```ruby -# config.ru -require 'lotus' +### _Container_ architecture -module OneFile - class Application < Lotus::Application - configure do - routes do - get '/', to: 'home#index' - end - end - end +**TL;DR: Develop your application like a gem. Implement use cases in `lib/`. Use one or more Lotus applications in `apps/`.** - module Controllers::Home - include OneFile::Controller +This is the default architecture. +When your are about to start a new project use it. - action 'Index' do - def call(params) - end - end - end +The core of this architecture lives in `lib/`, where developers should build features **independently from the delivery mechanism**. - module Views::Home - class Index - include OneFile::View +Imagine you are building a personal finance application, and you have a feature called _"register expense"_. This functionality involves `Money` and `Expense` Ruby objects and the need of persisting data into a database. You can have those classes living in `lib/pocket/money.rb` and `lib/pocket/expense.rb` and use [Lotus::Model](https://github.com/lotus/model) to persist them. - def render - 'Hello' - end - end - end -end +It's based on a few simple concepts: **use cases** and **applications**. +Use cases (features) should be implemented in `lib/` with a combination of pure objects and the needed Ruby gems. +One or more Lotus applications live in `apps/`. They are isolated each other, and depend only on the code in `lib/`. -run OneFile::Application.new -``` +Each of them should serve for only one purpose: user facing web application, administrative backend, JSON API, metrics dashboard, etc. -When the application is instantiated, it will also create `OneFile::Controllers` and `OneFile::Views` namespace, to incentivize the modularization of the resources. -Also, note how similar are the names of the action and of the view: `OneFile::Controllers::Home::Index` and `OneFile::Views::Home::Index`. -**This naming system is a Lotus convention and MUST be followed, or otherwise configured**. +This architecture has important advantages: -#### Microservices architecture + * **Code reusability.** You can consume a feature from the Web UI or from a HTTP API. Each one can be different Lotus application or simple Rack based endpoints. + * **Decoupled components.** The core of your application depends only on a few gems and it doesn't need to worry about the Web/HTTP/Console/Background jobs. + * **Applications are built like a gem**, this ease the process of package them and share between projects, without the need of carry a lot of dependencies. + * **Avoid monoliths**. Each Lotus application under `apps/` is a candidate for later on extraction into a separated [_microservice_](http://martinfowler.com/articles/microservices.html). -``` -test/fixtures/microservices -├── apps -│   ├── backend -│   │   ├── application.rb Backend::Application -│   │   ├── controllers -│   │   │   └── sessions.rb Backend::Controllers::Sessions::New, Create & Destroy -│   │   ├── public -│   │   │   ├── favicon.ico -│   │   │   ├── fonts -│   │   │   │   └── cabin-medium.woff -│   │   │   ├── images -│   │   │   │   └── application.jpg -│   │   │   ├── javascripts -│   │   │   │   └── application.js -│   │   │   └── stylesheets -│   │   │   └── application.css -│   │   ├── templates -│   │   │   ├── backend.html.erb -│   │   │   └── sessions -│   │   │   └── new.html.erb -│   │   └── views -│   │   ├── backend_layout.rb Backend::Views::BackendLayout -│   │   └── sessions -│   │   ├── create.rb Backend::Views::Sessions::Create -│   │   ├── destroy.rb Backend::Views::Sessions::Destroy -│   │   └── new.rb Backend::Views::Sessions::New -│   └── frontend -│   ├── application.rb Frontend::Application -│   ├── assets -│   │   ├── favicon.ico -│   │   ├── fonts -│   │   │   └── cabin-medium.woff -│   │   ├── images -│   │   │   └── application.jpg -│   │   ├── javascripts -│   │   │   └── application.js -│   │   └── stylesheets -│   │   └── application.css -│   ├── controllers -│   │   └── sessions -│   │   ├── create.rb Frontend::Controllers::Sessions::Create -│   │   ├── destroy.rb Frontend::Controllers::Sessions::Destroy -│   │   └── new.rb Frontend::Controllers::Sessions::New -│   ├── templates -│   │   ├── frontend.html.erb -│   │   └── sessions -│   │   └── new.html.erb -│   └── views -│   ├── application_layout.rb Frontend::Views::ApplicationLayout -│   └── sessions -│      ├── create.rb Frontend::Views::Sessions::Create -│      ├── destroy.rb Frontend::Views::Sessions::Destroy -│      └── new.rb Frontend::Views::Sessions::New -└── config.ru -``` +The last point is crucial. In the early days of a new project is really convenient to build and deploy all the code together. +But as the time passes, it can become nearly impossible to extract sets of cohesive functionalities into separated deliverables. +Lotus helps to plan those things ahead of time, but without the burden that is required by those choices, because it support multiple applications natively. -As you can see, the code can be organized as you prefer. For instance, all the sessions actions for the backend are grouped in the same file, -while they're split in the case of the frontend app. +Here's the name _**container**_: a Lotus _"shell"_ that can run multiple micro applications in the same process. -**This because Lotus doesn't have namespace-to-filename conventions, and doesn't have autoload paths.** -During the boot time it **recursively preloads all the classes from the specified directories.** - -```ruby -# apps/backend/application.rb -require 'lotus' - -module Backend - class Application < Lotus::Application - configure do - load_paths << [ - 'controllers', - 'views' - ] - - layout :backend - - routes do - resource :sessions, only: [:new, :create, :destroy] - end - end - end -end - -# All code under apps/backend/{controllers,views} will be loaded +```shell +% lotus new pocket --arch=container +% lotus new pocket # --arch=container is the default ``` -```ruby -# config.ru -require_relative 'apps/frontend/application' -require_relative 'apps/backend/application' +Read more about this [architecture](https://github.com/lotus/lotus/wiki/Container-architecture). -run Lotus::Router.new { - mount Backend::Application, at: '/backend' - mount Frontend::Application, at: '/' -} +### _Application_ architecture -# We use an instance of Lotus::Router to mount two Lotus applications -``` +_upcoming_ -#### Modulized application +### _Micro_ architecture -``` -test/fixtures/furnitures -├── app -│   ├── controllers -│   │   └── furnitures -│   │   └── catalog_controller.rb Furnitures::CatalogController::Index -│   ├── templates -│   │   ├── application.html.erb -│   │   └── furnitures -│   │   └── catalog -│   │   └── index.html.erb -│   └── views -│   ├── application_layout.rb Furnitures::Views::ApplicationLayout -│   └── furnitures -│   └── catalog -│   └── index.rb Furnitures::Catalog::Index -├── application.rb Furnitures::Application -└── public - ├── favicon.ico - ├── fonts - │   └── cabin-medium.woff - ├── images - │   └── application.jpg - ├── javascripts - │   └── application.js - └── stylesheets - └── application.css -``` +_upcoming_ -You may have noticed a different naming structure here, it's easily achieved with a few settings. +## Conventions -```ruby -# application.rb -require 'lotus' - -module Furnitures - class Application < Lotus::Application - configure do - layout :application - routes do - get '/', to: 'catalog#index' - end - - load_paths << 'app' - - controller_pattern "%{controller}Controller::%{action}" - view_pattern "%{controller}::%{action}" - end - end -end -``` - -The patterns above, are indicating to Lotus the name structure that we want to use for our application. -The main actor of the HTTP layer is an action. Actions are classes grouped logically in the same module called controller. - -For an incoming `GET` request to `/`, the router will look for a `CatalogController` with an `Index` action. -Once the action will be called, the control will pass to the view. Here the application will look for a `Catalog` module with an `Index` view. - -**That two patters are interpolated at the runtime, with the controller/action informations passed by the router.** - -#### Top level architecture - -``` -test/fixtures/information_tech -├── app -│   ├── controllers -│   │   └── hardware_controller.rb HardwareController::Index -│   ├── templates -│   │   ├── app.html.erb -│   │   └── hardware -│   │      └── index.html.erb -│   └── views -│   ├── app_layout.rb AppLayout -│   └── hardware -│       └── index.rb Hardware::Index -├── application.rb InformationTech::Application -├── config -│   └── routes.rb -└── public - ├── favicon.ico - ├── fonts - │   └── cabin-medium.woff - ├── images - │   └── application.jpg - ├── javascripts - │   └── application.js - └── stylesheets - └── application.css -``` - -While this architecture is technically possible, it's discouraged, because it pollutes the global namespace and it makes hard to split in several deliverables, once the code base will be big enough. - -```ruby -# application.rb -require 'lotus' - -module InformationTech - class Application < Lotus::Application - configure do - namespace Object - - controller_pattern '%{controller}Controller::%{action}' - view_pattern '%{controller}::%{action}' - - layout :app - - load_paths << 'app' - routes 'config/routes' - end - end -end - -# We use Object, because it's the top level Ruby namespace. -``` - -### Conventions - -* Lotus expects that controllers, actions and views to have a specific pattern (see Configuration for customizations) +* Lotus expects controllers, actions and views to have a specific pattern (see [Configuration](#configuration) for customizations) * All the commands must be run from the root of the project. If this requirement cannot be satisfied, please hardcode the path with `Configuration#root`. * The template name must reflect the name of the corresponding view: `Bookshelf::Views::Dashboard::Index` for `dashboard/index.html.erb`. * All the static files are served by the internal Rack middleware stack. * The application expects to find static files under `public/` (see `Configuration#assets`) * If the public folder doesn't exist, it doesn't serve static files. -### Non-Conventions +## Non-Conventions * The application structure can be organized according to developer needs. * No file-to-name convention: modules and classes can live in one or multiple files. -* No autoloading paths. They must be explicitely configured. +* No autoloading paths. They must be explicitly configured. -### Configuration +## Configuration +<a name="configuration"></a> + A Lotus application can be configured with a DSL that determines its behavior. ```ruby require 'lotus' module Bookshelf class Application < Lotus::Application configure do + ######################## + # BASIC CONFIGURATIONS # + ######################## + # Determines the root of the application (optional) # Argument: String, Pathname, defaults to Dir.pwd # - root 'path/to/root' + root 'path/to/root' # or __root__ - # The Ruby namespace where to lookup for actions and views (optional) - # Argument: Module, Class, defaults to the application module (eg. Bookshelf) - # - namespace Object - # The relative load paths where the application will recursively load the code (mandatory) # Argument: String, Array<String>, defaults to empty set # load_paths << [ - 'app/controllers', - 'app/views' + 'controllers', + 'views' ] + # Handle exceptions with HTTP statuses (true) or don't catch them (false) + # Argument: boolean, defaults to true + # + handle_exceptions true + + ####################### + # HTTP CONFIGURATIONS # + ####################### + # The route set (mandatory) # Argument: Proc with the routes definition # routes do get '/', to: 'home#index' @@ -381,65 +180,196 @@ # The route set (mandatory) (alternative usage) # Argument: A relative path where to find the routes definition # routes 'config/routes' - # The layout to be used by all the views (optional) - # Argument: A Symbol that indicates the name, default to nil + # URI scheme used by the routing system to generate absolute URLs (optional) + # Argument: A string, default to "http" # - layout :application # Will look for Bookshelf::Views::ApplicationLayout + scheme 'https' - # The relative path where to find the templates (optional) - # Argument: A string with the relative path, default to the root of the app + # URI host used by the routing system to generate absolute URLs (optional) + # Argument: A string, default to "localhost" # - templates 'app/templates' + host 'bookshelf.org' + # URI port used by the routing system to generate absolute URLs (optional) + # Argument: An object coercible to integer, default to 80 if the scheme is http and 443 if it's https + # This SHOULD be configured only in case the application listens to that non standard ports + # + port 2323 + + # Toggle cookies (optional) + # Argument: A [`TrueClass`, `FalseClass`], default to `FalseClass`. + # + cookies true + + # Toggle sessions (optional) + # Argument: Symbol the Rack session adapter + # A Hash with options + # + sessions :cookie, secret: ENV['SESSIONS_SECRET'] + # Default format for the requests that don't specify an HTTP_ACCEPT header (optional) # Argument: A symbol representation of a mime type, default to :html # default_format :json - # URI scheme used by the routing system to generate absoule URLs (optional) - # Argument: A string, default to "http" + # Rack middleware configuration (optional) # - scheme 'https' + middleware.use Rack::Protection - # URI host used by the routing system to generate absoule URLs (optional) - # Argument: A string, default to "localhost" + # HTTP Body parsers (optional) + # Parse non GET responses body for a specific mime type + # Argument: Symbol, which represent the format of the mime type (only `:json` is supported) + # Object, the parser # - host 'bookshelf.org' + body_parsers :json, MyXMLParser.new - # URI port used by the routing system to generate absoule URLs (optional) - # Argument: An object coercible to integer, default to 80 if the scheme is http and 443 if it's https - # This SHOULD be configured only in case the application listens to that non standard ports + ########################### + # DATABASE CONFIGURATIONS # + ########################### + + # Configure a database adapter (optional) + # Argument: A Hash with the settings + # type: Symbol, :file_system, :memory and :sql + # uri: String, 'file:///db/bookshelf' + # 'memory://localhost/bookshelf' + # 'sqlite:memory:' + # 'sqlite://db/bookshelf.db' + # 'postgres://localhost/bookshelf' + # 'mysql://localhost/bookshelf' # - port 2323 + adapter type: :file_system, uri: ENV['DATABASE_URL'] - # The name pattern to find controller and actions (optional) - # Argument: A string, it must contain "%{controller}" and %{action} - # Default to "Controllers::%{controller}::%{action}" + # Configure a database mapping (optional) + # Argument: Proc # - controller_pattern '%{controller}Controller::%{action}' + mapping do + collection :users do + entity User + repository UserRepository - # The name pattern to find views (optional) - # Argument: A string, it must contain "%{controller}" and %{action} - # Default to "Views::%{controller}::%{action}" + attribute :id, Integer + attribute :name, String + end + end + + # Configure a database mapping (optional, alternative usage) + # Argument: A relative path where to find the mapping definitions # - view_pattern '%{controller}Views::%{action}' + mapping 'config/mapping' + + ############################ + # TEMPLATES CONFIGURATIONS # + ############################ + + # The layout to be used by all the views (optional) + # Argument: A Symbol that indicates the name, default to nil + # + layout :application # Will look for Bookshelf::Views::ApplicationLayout + + # The relative path where to find the templates (optional) + # Argument: A string with the relative path, default to the root of the app + # + templates 'templates' + + ######################### + # ASSETS CONFIGURATIONS # + ######################### + + # Specify sources for assets (optional) + # Argument: String, Array<String>, defaults to 'public' + # + assets << [ + 'public', + 'vendor/assets' + ] + + # Enabling serving assets (optional) + # Argument: boolean, defaults to false + # + serve_assets true + + ############################# + # FRAMEWORKS CONFIGURATIONS # + ############################# + + # Low level configuration for Lotus::View (optional) + # The given block will be yielded every time `Lotus::View` is included. + # This is helpful to share logic between views + # See the related documentation + # Argument: Proc + # + view.prepare do + include MyCustomRoutingHelpers # included by all the views + end + + # Low level configuration for Lotus::Controller (optional) + # Argument: Proc + controller.prepare do + include Authentication # included by all the actions + before :authenticate! # run auth logic before each action + end end + + ############################### + # ENVIRONMENTS CONFIGURATIONS # + ############################### + + configure :development do + # override the general configuration only for the development environment + handle_exceptions false + serve_assets true + end + + configure :test do + # override the general configuration only for the test environment + host 'test.host' + end end end ``` -## The future +## Command line -Lotus uses different approaches for web development with Ruby, for this reason, it needs to reach a certain code maturity degree. -It will improved by collecting the feedback of real world applications. +Lotus provides a few command line utilities: -Also, it still lacks of features like: live reloading, multiple environments, code generators, cli, etc.. -Please get involved with the project. +### Server -Thank you. +It looks at the `config.ru` file in the root of the application, and starts the Rack server defined in your `Gemfile` (eg. puma, thin, unicorn). It defaults to WEBRick. + +It supports **code reloading** feature by default, useful for development purposes. + +```shell +% bundle exec lotus server +``` + +### Console + +It starts a REPL, by using the engine defined in your `Gemfile`. It defaults to IRb. **Run it from the root of the application**. + +```shell +% bundle exec lotus console +``` + +It supports **code reloading** via the `reload!` command. + +### Routes + +It prints the routes defined by the Lotus application(s). + +```shell +% bundle exec lotus routes +``` + +### Version + +It prints the current Lotus version. + +```shell +% bundle exec lotus version +``` ## Contributing 1. Fork it ( https://github.com/lotus/lotus/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`)