diff --git a/.gitignore b/.gitignore deleted file mode 100644 index aa03a48..0000000 --- a/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/.rake_tasks~ -/temp.db -/vendor/ -/bin/ \ No newline at end of file diff --git a/Gemfile b/Gemfile index b668a39..3606de8 100644 --- a/Gemfile +++ b/Gemfile @@ -4,13 +4,6 @@ gem 'roda' gem 'rack-unreloader' gem 'tilt' gem 'erubi' -gem 'sequel' -gem 'rake' - -# Change to whatever database you plan to use -gem 'sqlite3' -# gem 'pg' # will also want gem sequel-pg -# gem 'mysql2' # change to gunicorn or passenger if you prefer: gem 'puma' diff --git a/README.md b/README.md index 7994bbc..1a574b7 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ For the single file app template, really the only file you need is the config.ru and run it with puma. -## Database - ## Views In this template, I've taken the single-file template and split up the config.ru to put routes and app logic in app.rb and call on templates to render proper html pages. `tilt` and `erubi` gems are added to do the rendering, and `rack-unreloader` gem is added to have puma automatically load file changes while running in development mode. @@ -14,17 +12,6 @@ Next Roda app template iterations will add database, then authentication. ## Setup -### Get git - -``` -sudo apt install git -git config --global user.email "myemail@gmail.com" -git config --global user.name "Full Name" -git config --global credential.helper "cache" - -git clone https://path/to/project.git -``` - ### Prereq installs Will need ruby; install it via package manager or a ruby manager like rbenv/ruby-build. Will need the roda gem, and then an application server such as puma (recommended), gunicorn, or passenger. My examples will use system ruby and puma. @@ -34,64 +21,31 @@ Will need ruby; install it via package manager or a ruby manager like rbenv/ruby With this example, will basically just ignore the project's Gemfile. Debian 12 has a pretty current ruby version so just using it. ``` -sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt ruby-sequel ruby-sqlite3 rake +sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt sudo gem install roda rack-unreloader - -cd my-project ``` -#### Option 2a: Bundler system package +#### Option 2: Bundler -Run the bundle install command from the project's root directory +Run the bundle command from the project's root directory ``` -sudo apt install ruby ruby-bundler ruby-dev gcc pkgconf make g++ libyaml-dev libffi-dev -echo 'gem: --no-document' >> ~/.gemrc -sudo cp ~/.gemrc /root/ -bundle config set --global path 'vendor/bundle' -sudo cp -r .bundle /root/ - -cd my-project +sudo apt install ruby ruby-bundler +bundle config set --local path 'vendor/bundle' bundle install ``` -#### Option 2b: Bundler system gem (recommended) - -Similar to above but might as well use the gem install to get the latest bundler. The Debian apt packaged bundler is currently a bit outdated and missing some features compared to the latest. - -``` -sudo apt install ruby ruby-dev gcc pkgconf make g++ libyaml-dev # zlib1g-dev libffi-dev #(for rails stuff) -echo 'gem: --no-document' >> ~/.gemrc -sudo cp ~/.gemrc /root/ -sudo gem install bundler -bundle config set --global path 'vendor/bundle' -sudo cp -r .bundle /root/ - -cd my-project -bundle install -``` - -#### Option 3: Rbenv Ruby - -Todo - ## Run it In the project root directory: ``` -bundle exec puma - -# or if you did not use bundler to install puma... puma ``` This default to development mode. Run it in production mode with: ``` -RACK_ENV=production bundle exec puma - -# or if you did not install puma RACK_ENV=production puma ``` @@ -99,7 +53,7 @@ For development, just run it like that. For production, probably want to set up ### Run it with systemd in production -Copy the example myapp.service file to `/etc/systemd/system/` and edit accordingly. The example assumes a user named "myapp" with a group name "myapp", the application files are in `/opt/myapp/`, and puma is the system puma. If you installed puma with bundler, the exe will be at `/opt/myapp/vendor/bundle/ruby/3.1.0/bin/puma`. +Copy the example myapp.service file to `/etc/systemd/system/` and edit accordingly. The example assumes a user named "myapp" with a group name "myapp", the application files are in `/opt/myapp/`, and puma is the system puma. ### Notes @@ -109,7 +63,7 @@ This has puma reload with any file changes while you are working on the app, whe . -Basically, you require the app from the config.ru, and then within you app files, any "require_relative" will be "Unreloader.require" instead, plus you do need to include the file extension. +Basically, you require the app from the config.ru, and then within you app files, any "require_relative" will be "Unreloader.require" instead, plus you do need to include the file extension when using Unreloader.require. In config.ru, the `dev =` line stores true or false depending on the RACK_ENV environment variable (see above for changing from default development). Then the `:reload=>dev` uses the dev variable to tell Unreload to reload files or not. Lastly the `run` command uses the dev variable to choose between running Unreloader, or bypass Unreloader and run App like normal in production. diff --git a/Rakefile b/Rakefile deleted file mode 100644 index 4a4ce07..0000000 --- a/Rakefile +++ /dev/null @@ -1,33 +0,0 @@ -namespace :db do - - migrator = lambda do |version| - require_relative 'db' - Sequel.extension :migration - Sequel::Migrator.apply(DB, 'migrate', version) - end - - namespace :migrate do - - desc "Perform migration up to latest migration available" - task :up do - migrator.call(nil) - end - - desc "Perform migration all the way down (erase all data)" - task :down do - migrator.call(0) - end - - end - - desc "Erase all data, then bring back to latest migration" - task :reset do - migrator.call(0) - Sequel::Migrator.apply(DB, 'migrate') - end - - #desc "Migrate to a specific version" - #task :migrate do |version| - #end - -end diff --git a/app.rb b/app.rb index 8c321e0..76eeb17 100644 --- a/app.rb +++ b/app.rb @@ -14,17 +14,10 @@ class App < Roda view :index end - r.is 'users' do - @users = User.order(:id) - view :users - end - - r.on 'hello' do - r.is String do |name| - @page_title = 'A Custom Greeting' - @name = name.capitalize - view :greeting - end + r.is String do |name| + @page_title = 'A Custom Greeting' + @name = name.capitalize + view :greeting end end end diff --git a/config.ru b/config.ru index 1af6d15..4e24994 100644 --- a/config.ru +++ b/config.ru @@ -3,8 +3,7 @@ dev = ENV['RACK_ENV'] == 'development' require 'rack/unreloader' -Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda Sequel::Model', reload: dev, autoload: dev){App} -Unreloader.require './models.rb' +Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda', :reload=>dev){App} Unreloader.require './app.rb' run(dev ? Unreloader : App) diff --git a/db.rb b/db.rb deleted file mode 100644 index 5c72eb9..0000000 --- a/db.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true -require 'sequel/core' - -DB = Sequel.connect('sqlite://temp.db') diff --git a/migrate/001_create_users_table.rb b/migrate/001_create_users_table.rb deleted file mode 100644 index 29d513d..0000000 --- a/migrate/001_create_users_table.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -Sequel.migration do - change do - create_table(:users) do - primary_key :id - String :name, unique: true, null: false - end - end -end - diff --git a/models.rb b/models.rb deleted file mode 100644 index 33c6865..0000000 --- a/models.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true -require_relative 'db' -require 'sequel/model' - -if ENV['RACK_ENV'] == 'development' - Sequel::Model.cache_associations = false -end - -unless defined?(Unreloader) - require 'rack/unreloader' - Unreloader = Rack::Unreloader.new(reload: false, autoload: !ENV['NO_AUTOLOAD']) -end - -Unreloader.autoload('models'){|f| Sequel::Model.send(:camelize, File.basename(f).sub(/\.rb\z/, ''))} diff --git a/models/user.rb b/models/user.rb deleted file mode 100644 index a4f2fa1..0000000 --- a/models/user.rb +++ /dev/null @@ -1,3 +0,0 @@ -# frozen_string_literal: true -class User < Sequel::Model -end diff --git a/views/greeting.erb b/views/greeting.erb index 15c8d7a..9de747b 100644 --- a/views/greeting.erb +++ b/views/greeting.erb @@ -1,3 +1,3 @@ -

+

Hello, <%= @name %>!

diff --git a/views/index.erb b/views/index.erb index 25704e6..4762d4c 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1,3 +1,3 @@ -

- Welcome to my new page! Running in <%= ENV['RACK_ENV'] %> mode! +

+ Welcome to my new page!

diff --git a/views/layout.erb b/views/layout.erb index 4869da1..c5ec841 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -1,17 +1,16 @@ - + <%= @page_title || "My Website" %> - +
-

- <%= @page_title || 'Page Title Placeholder' %> -

+

<%= @page_title || 'Page Title Placeholder' %>

+ <% # maybe put a flash section here some day %> <%== yield %>
diff --git a/views/users.erb b/views/users.erb deleted file mode 100644 index 7fd2caf..0000000 --- a/views/users.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% for u in @users do %> -

- Hello, <%= u.name %>! You're #<%= u.id %>! -

-<% end %>