From 2151565e9c752b685ea350d7945024b9a6f8ce08 Mon Sep 17 00:00:00 2001 From: James Dinkel Date: Wed, 27 Dec 2023 18:19:21 -0600 Subject: [PATCH] Split up config.ru and added views --- Gemfile | 5 +++++ README.md | 32 ++++++++++++++++++++++++++++++-- app.rb | 24 ++++++++++++++++++++++++ config.ru | 18 ++++++------------ views/greeting.erb | 3 +++ views/index.erb | 3 +++ views/layout.erb | 18 ++++++++++++++++++ 7 files changed, 89 insertions(+), 14 deletions(-) create mode 100644 app.rb create mode 100644 views/greeting.erb create mode 100644 views/index.erb create mode 100644 views/layout.erb diff --git a/Gemfile b/Gemfile index fb482ca..3606de8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,9 @@ source 'https://rubygems.org' gem 'roda' +gem 'rack-unreloader' +gem 'tilt' +gem 'erubi' + +# change to gunicorn or passenger if you prefer: gem 'puma' diff --git a/README.md b/README.md index 9c97c72..42e3b86 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,18 @@ For the single file app template, really the only file you need is the config.ru and run it with puma. +## Views + +Next iteration: + +adding plugin csrf, and maybe public, assets +split out app routes from config.ru +set up view templates +add unreloader + +future iterations will add database, then authentication + + ## Setup ### Prereq installs @@ -13,8 +25,8 @@ 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 -sudo gem install roda +sudo apt install ruby ruby-rack puma ruby-erubi ruby-tilt +sudo gem install roda rack-unreloader ``` #### Option 2: Bundler @@ -46,3 +58,19 @@ 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. + +### Notes + +## Unreloader + +This has puma reload with any file changes while you are working on the app, when in development mode. When puma is in production mode, it loads the app on startup like normal with no performance penalty. It's very useful during development and no reason to not leave it in. + +. + +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. + +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 dev variable to choose between running Unreloader, or bypass Unreloader and run App like normal in production. + +## Additional credit + +I took a lot of inspirations from Jeremy Evans [roda-sequel-stack](https://github.com/jeremyevans/roda-sequel-stack). Jeremy Evans is the author of Roda and also Unreloader, Sequel, and a ruby core contributor. diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..76eeb17 --- /dev/null +++ b/app.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true +require 'roda' +require 'tilt' +require 'tilt/erubi' + +class App < Roda + plugin :render, escape: true + plugin :route_csrf + + route do |r| + check_csrf! + + r.root do + view :index + 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 77fc7ae..4e24994 100644 --- a/config.ru +++ b/config.ru @@ -1,15 +1,9 @@ -require 'roda' -class App < Roda - route do |r| - r.root do - "

Hello, World!

" - end +dev = ENV['RACK_ENV'] == 'development' - r.is String do |name| - "

Hello, #{name.capitalize}!

" - end - end -end +require 'rack/unreloader' -run App +Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda', :reload=>dev){App} +Unreloader.require './app.rb' + +run(dev ? Unreloader : App) diff --git a/views/greeting.erb b/views/greeting.erb new file mode 100644 index 0000000..9de747b --- /dev/null +++ b/views/greeting.erb @@ -0,0 +1,3 @@ +

+ Hello, <%= @name %>! +

diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..4762d4c --- /dev/null +++ b/views/index.erb @@ -0,0 +1,3 @@ +

+ Welcome to my new page! +

diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 0000000..c5ec841 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,18 @@ + + + + + + <%= @page_title || "My Website" %> + + + +
+
+

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

+ <% # maybe put a flash section here some day %> + <%== yield %> +
+
+ +