roda-templates/README.md
2024-01-05 19:50:37 -06:00

75 lines
3.1 KiB
Markdown

# roda-templates
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.
My example html layout uses [Bulma](https://bulma.io/) to make it easier to make modern looking webpages.
Next Roda app template iterations will add database, then authentication.
## Setup
### 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.
#### Option 1: System wide packages
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 ruby-bcrypt
sudo gem install roda rack-unreloader rodauth --no-document
```
#### Option 2: Bundler
Run the bundle command from the project's root directory
```
sudo apt install ruby ruby-bundler
bundle config set --local path 'vendor/bundle'
bundle install
```
## Run it
In the project root directory:
```
puma
```
This default to development mode. Run it in production mode with:
```
RACK_ENV=production puma
```
For development, just run it like that. For production, probably want to set up a systemd service.
### 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.
<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 the 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, among other things.