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.
$ gem install minirails
Create a file myapp.rb with the following content
# myapp.rbdefinedo |app|
classMainController < ActionController::Basedefindexrendertext: "Hello"endendapp.routes.drawdoget"/",to: "main#index"endendthen run
$ minirails myapp.rb
and that's it - you now have fully functional rails app running on port 5000!
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]}endendThey will be run on ports 5000 and 5100 (and 5200 etc.)
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:usersendendYou 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"sleep1endenddefine"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"sleep1endendHow to speed up loading?
Run
$ minirails --fastand then usebin/minirails myapp.rbinstead
- Fork it ( https://github.com/teamon/minirails/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request