Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
Created August 16, 2024 03:29
Show Gist options
  • Save nileshtrivedi/a60998391791851d69d32619089cae37 to your computer and use it in GitHub Desktop.
Save nileshtrivedi/a60998391791851d69d32619089cae37 to your computer and use it in GitHub Desktop.
Single-file Rails app experiment
# frozen_string_literal: true
# Run this app as: puma app.ru
# Most of this was taken from: https://greg.molnar.io/blog/a-single-file-rails-application/
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '~> 7.2.0'
gem 'puma', '~> 6.4.2'
gem 'sqlite3', '~> 2.0.4'
end
require 'rails/all'
database = 'development.sqlite3'
ENV['DATABASE_URL'] = "sqlite3:#{database}"
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: database)
ActiveRecord::Base.logger = Logger.new(STDOUT)
Rails.logger = Logger.new(STDOUT)
ActiveRecord::Schema[7.2].define do
# Let's re-create the table unless it already exists.
create_table :posts, force: false, if_not_exists: true do |t|
end
create_table :comments, force: false, if_not_exists: true do |t|
t.integer :post_id
end
end
class App < Rails::Application
config.eager_load = true
config.root = __dir__
config.consider_all_requests_local = true
config.secret_key_base = 'i_am_a_secret'
config.action_dispatch.default_headers = { 'X-Frame-Options' => 'ALLOWALL' }
config.active_storage.service_configurations = { 'local' => { 'service' => 'Disk', 'root' => './storage' } }
routes.append do
root to: 'welcome#index'
end
end
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class
end
class Post < ApplicationRecord
end
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern
end
class WelcomeController < ApplicationController
def index
render inline: Post.new.inspect
end
end
App.initialize!
run App
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment