Skip to content

Repository files navigation

Minirails

Run example rails app from single file

Inspired by The Smallest Rails App, I wanted to easily try various concepts inside rails apps without having to deal with all that bloat from standard rails directory structure.

I went even further, not only reducing full rails app into single file (that has already been done) but to reducing a set of rails apps into single file 😎

More seriously, you can now create whole microservice architecture within a singe file and somebody else will be able to grasp it all and execute it locally. Or you can easily make a single file app and post it to StackOverflow (and people can actually run this code)

Just think about it. Proof of Concepts. Asking questions. Explaining concepts.

Installation

$ gem install minirails

Basic usage

Create a file myapp.rb with the following content

# myapp.rbdefinedo |app|
classMainController < ActionController::Basedefindexrendertext: "Hello"endendapp.routes.drawdoget"/",to: "main#index"endend

then run

$ minirails myapp.rb

and that's it - you now have fully functional rails app running on port 5000!

Advanced usage

Multiple apps

You can run multiple applications from singe file

# file: multiple.rb# usage: $ minirails multiple.rbdefine"one"do |app|
app.routes.drawdoget"/",to: proc{|*| [200,{},"Hello".lines]}endenddefine"two"do |app|
app.routes.drawdoget"/",to: proc{|*| [200,{},"World".lines]}endend

They will be run on ports 5000 and 5100 (and 5200 etc.)

Database setup

You can also use full ActiveRecord capabilities. The database (specified with ENV["DATABASE_URL"]) will be recreated (drop & create & migrate) on every run

# file: database.rb# usage: $ minirails database.rbdefine"source"do |app|
# ConfigurationENV["DATABASE_URL"]="postgresql://localhost/minirails_test"# MigrationsclassMigrations < ActiveRecord::Migrationdefchangecreate_table:usersdo |t|
t.string:namet.string:emailend# SeedsUser.create![{name: "Jon",email: "jon@example.com"},{name: "Hodor",email: "hodor@example.com"}]endend# ModelsclassUser < ActiveRecord::Baseend# ControllersclassUsersController < ActionController::Basedefindexrenderjson: User.allendend# Routesapp.routes.drawdoresources:usersendend

Non-rails apps

You can start any rack compatible web app inside define block or even generic worker process (sidekiq, clock, whatever) by specifying :type option.

# file: types.rb# usage: $ minirails types.rb# this is rails appdefine"rails"do |app|
app.routes.drawdoget"/",to: proc{|*| [200,{},"Hello from Rails".lines]}endend# with type: :rack you need to return Rack-compatible appdefine"rack",type: :rackdoproc{|*| [200,{},"Hello from Rack".lines]}end# with type: :blank you can run anythingdefine"worker",type: :blankdoloopdo
$stderr.puts"Hello from worker"sleep1endend

Running worker with Rails env loaded

define"my-rails-app"do |app|
app.routes.drawdoget"/",to: proc{|*| [200,{},"Hello from Rails".lines]}endend# you can use :initialize option to load rails (or some rack)# before loading worker code (similar to `rake environment`)define"worker",type: :blank,initialize: "my-rails-app"doloopdoRails.logger.debug"Hello from worker with Rails env"sleep1endend

FAQ

  • How to speed up loading?

    Run $ minirails --fast and then use bin/minirails myapp.rb instead

Contributing

  1. Fork it ( https://github.com/teamon/minirails/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

About

Smallest Rails (and other) Apps

Resources

Stars

10 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages