Split up config.ru and added views

This commit is contained in:
James 2023-12-27 18:19:21 -06:00
parent e5ffa9e318
commit 2151565e9c
7 changed files with 89 additions and 14 deletions

View file

@ -1,4 +1,9 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'roda' gem 'roda'
gem 'rack-unreloader'
gem 'tilt'
gem 'erubi'
# change to gunicorn or passenger if you prefer:
gem 'puma' gem 'puma'

View file

@ -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. 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 ## Setup
### Prereq installs ### 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. 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 apt install ruby ruby-rack puma ruby-erubi ruby-tilt
sudo gem install roda sudo gem install roda rack-unreloader
``` ```
#### Option 2: Bundler #### 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 ### 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. 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.
<https://github.com/jeremyevans/rack-unreloader>.
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.

24
app.rb Normal file
View file

@ -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

View file

@ -1,15 +1,9 @@
require 'roda'
class App < Roda dev = ENV['RACK_ENV'] == 'development'
route do |r|
r.root do
"<h1>Hello, World!</h1>"
end
r.is String do |name| require 'rack/unreloader'
"<h1>Hello, #{name.capitalize}!</h1>"
end
end
end
run App Unreloader = Rack::Unreloader.new(:subclasses=>%w'Roda', :reload=>dev){App}
Unreloader.require './app.rb'
run(dev ? Unreloader : App)

3
views/greeting.erb Normal file
View file

@ -0,0 +1,3 @@
<p class="subtitle is-3">
Hello, <%= @name %>!
</p>

3
views/index.erb Normal file
View file

@ -0,0 +1,3 @@
<p class="subtitle is-3">
Welcome to my new page!
</p>

18
views/layout.erb Normal file
View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><%= @page_title || "My Website" %></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title is-1"><%= @page_title || 'Page Title Placeholder' %></h1>
<% # maybe put a flash section here some day %>
<%== yield %>
</div>
</section>
</body>
</html>