diff --git a/README.md b/README.md index e8161a0..a07afbe 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,29 @@ # litestream-ruby +
+ + + + + + + + + + + + + + + + + + + + + +
+ [Litestream](https://litestream.io/) is a standalone streaming replication tool for SQLite. This gem provides a Ruby interface to Litestream. ## Installation @@ -203,6 +227,119 @@ In order to verify that the backup for that database is both restorable and fres After restoring the backup, the `Litestream.verify!` method will delete the restored database file. If you need the restored database file, use the `litestream:restore` rake task or `Litestream::Commands.restore` method instead. +### Dashboard + +The gem provides a web dashboard for monitoring the status of your Litestream replication. To mount the dashboard in your Rails application, add the following to your `config/routes.rb` file: + +```ruby +authenticate :user, -> (user) { user.admin? } do + mount Litestream::Engine, at: "/litestream" +end +``` + +> [!NOTE] +> Be sure to [secure the dashboard](#authentication) in production. + +#### Authentication + +Litestream Rails does not restrict access out of the box. You must secure the dashboard yourself. However, it does provide basic HTTP authentication that can be used with basic authentication or Devise. All you need to do is setup a username and password. + +There are two ways to setup a username and password. First, you can use the `LITESTREAM_USERNAME` and `LITESTREAM_PASSWORD` environment variables: + +```ruby +ENV["LITESTREAM_USERNAME"] = "frodo" +ENV["LITESTREAM_PASSWORD"] = "ikeptmysecrets" +``` + +Second, you can configure the access credentials via the Rails configuration object, under the `litestream` key, in an initializer: + +```ruby +# Set authentication credentials for Litestream +config.litestream.username = Rails.application.credentials.litestream.username +config.litestream.password = Rails.application.credentials.litestream.password +``` + +Either way, if you have set a username and password, Litestream will use basic HTTP authentication. + +> [!IMPORTANT] +> If you have not set a username and password, Litestream will not require any authentication to view the dashboard. + +If you use Devise for authentication in your app, you can also restrict access to the dashboard by using their `authenticate` constraint in your routes file: + +```ruby +authenticate :user, -> (user) { user.admin? } do + mount LitestreamRails::Engine, at: "/litestream" +end +``` + +### Examples + +There is only one screen in the dashboard. + +* the show view of the Litestream replication process: + +![screenshot of the single page in the web dashboard, showing details of the Litestream replication process](images/show-screenshot.png) + +### Usage with API-only Applications + +If your Rails application is an API-only application (generated with the `rails new --api` command), you will need to add the following middleware to your `config/application.rb` file in order to use the dashboard UI provided by Litestream: + +```ruby +# /config/application.rb +config.middleware.use ActionDispatch::Cookies +config.middleware.use ActionDispatch::Session::CookieStore +config.middleware.use ActionDispatch::Flash +``` + +### Overwriting the views + +You can find the views in [`app/views`](https://github.com/fractaledmind/litestream/tree/main/app/views). + +```bash +app/views/ +├── layouts +│ └── litestream +│ ├── _style.html +│ └── application.html.erb +└── litestream + └── processes + └── show.html.erb +``` + +You can always take control of the views by creating your own views and/or partials at these paths in your application. For example, if you wanted to overwrite the application layout, you could create a file at `app/views/layouts/litestream/application.html.erb`. If you wanted to remove the footer and the automatically disappearing flash messages, as one concrete example, you could define that file as: + +```erb + + + ++ <%= notice %> +
+ <% end %> + + <% if alert.present? %> ++ <%= alert %> +
+ <% end %> +