Skip to content

Commit 19f4f58

Browse files
Merge pull request #37 from fractaledmind/dashboard-docs
Add sections to README on the web dashboard
2 parents 3fc8091 + 9f9f18d commit 19f4f58

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# litestream-ruby
22

3+
<p>
4+
<a href="https://rubygems.org/gems/litestream">
5+
<img alt="GEM Version" src="https://img.shields.io/gem/v/litestream?color=168AFE&include_prereleases&logo=ruby&logoColor=FE1616">
6+
</a>
7+
<a href="https://rubygems.org/gems/litestream">
8+
<img alt="GEM Downloads" src="https://img.shields.io/gem/dt/litestream?color=168AFE&logo=ruby&logoColor=FE1616">
9+
</a>
10+
<a href="https://github.com/testdouble/standard">
11+
<img alt="Ruby Style" src="https://img.shields.io/badge/style-standard-168AFE?logo=ruby&logoColor=FE1616" />
12+
</a>
13+
<a href="https://github.com/fractaledmind/litestream-ruby/actions/workflows/main.yml">
14+
<img alt="Tests" src="https://github.com/fractaledmind/litestream-ruby/actions/workflows/main.yml/badge.svg" />
15+
</a>
16+
<a href="https://github.com/sponsors/fractaledmind">
17+
<img alt="Sponsors" src="https://img.shields.io/github/sponsors/fractaledmind?color=eb4aaa&logo=GitHub%20Sponsors" />
18+
</a>
19+
<a href="https://ruby.social/@fractaledmind">
20+
<img alt="Ruby.Social Follow" src="https://img.shields.io/mastodon/follow/109291299520066427?domain=https%3A%2F%2Fruby.social&label=%40fractaledmind&style=social">
21+
</a>
22+
<a href="https://twitter.com/fractaledmind">
23+
<img alt="Twitter Follow" src="https://img.shields.io/twitter/url?label=%40fractaledmind&style=social&url=https%3A%2F%2Ftwitter.com%2Ffractaledmind">
24+
</a>
25+
</p>
26+
327
[Litestream](https://litestream.io/) is a standalone streaming replication tool for SQLite. This gem provides a Ruby interface to Litestream.
428

529
## Installation
@@ -203,6 +227,119 @@ In order to verify that the backup for that database is both restorable and fres
203227

204228
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.
205229

230+
### Dashboard
231+
232+
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:
233+
234+
```ruby
235+
authenticate :user, -> (user) { user.admin? } do
236+
mount Litestream::Engine, at: "/litestream"
237+
end
238+
```
239+
240+
> [!NOTE]
241+
> Be sure to [secure the dashboard](#authentication) in production.
242+
243+
#### Authentication
244+
245+
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.
246+
247+
There are two ways to setup a username and password. First, you can use the `LITESTREAM_USERNAME` and `LITESTREAM_PASSWORD` environment variables:
248+
249+
```ruby
250+
ENV["LITESTREAM_USERNAME"] = "frodo"
251+
ENV["LITESTREAM_PASSWORD"] = "ikeptmysecrets"
252+
```
253+
254+
Second, you can configure the access credentials via the Rails configuration object, under the `litestream` key, in an initializer:
255+
256+
```ruby
257+
# Set authentication credentials for Litestream
258+
config.litestream.username = Rails.application.credentials.litestream.username
259+
config.litestream.password = Rails.application.credentials.litestream.password
260+
```
261+
262+
Either way, if you have set a username and password, Litestream will use basic HTTP authentication.
263+
264+
> [!IMPORTANT]
265+
> If you have not set a username and password, Litestream will not require any authentication to view the dashboard.
266+
267+
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:
268+
269+
```ruby
270+
authenticate :user, -> (user) { user.admin? } do
271+
mount LitestreamRails::Engine, at: "/litestream"
272+
end
273+
```
274+
275+
### Examples
276+
277+
There is only one screen in the dashboard.
278+
279+
* the show view of the Litestream replication process:
280+
281+
![screenshot of the single page in the web dashboard, showing details of the Litestream replication process](images/show-screenshot.png)
282+
283+
### Usage with API-only Applications
284+
285+
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:
286+
287+
```ruby
288+
# /config/application.rb
289+
config.middleware.use ActionDispatch::Cookies
290+
config.middleware.use ActionDispatch::Session::CookieStore
291+
config.middleware.use ActionDispatch::Flash
292+
```
293+
294+
### Overwriting the views
295+
296+
You can find the views in [`app/views`](https://github.com/fractaledmind/litestream/tree/main/app/views).
297+
298+
```bash
299+
app/views/
300+
├── layouts
301+
│   └── litestream
302+
│   ├── _style.html
303+
│   └── application.html.erb
304+
└── litestream
305+
└── processes
306+
   └── show.html.erb
307+
```
308+
309+
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:
310+
311+
```erb
312+
<!DOCTYPE html>
313+
<html>
314+
<head>
315+
<title>Litestream</title>
316+
<%= csrf_meta_tags %>
317+
<%= csp_meta_tag %>
318+
319+
<%= render "layouts/litestream/style" %>
320+
</head>
321+
<body class="h-full flex flex-col">
322+
<main class="container mx-auto max-w-4xl mt-4 px-2 grow">
323+
<%= content_for?(:content) ? yield(:content) : yield %>
324+
</main>
325+
326+
<div class="fixed top-0 left-0 right-0 text-center py-2">
327+
<% if notice.present? %>
328+
<p class="py-2 px-3 bg-green-50 text-green-500 font-medium rounded-lg inline-block">
329+
<%= notice %>
330+
</p>
331+
<% end %>
332+
333+
<% if alert.present? %>
334+
<p class="py-2 px-3 bg-red-50 text-red-500 font-medium rounded-lg inline-block">
335+
<%= alert %>
336+
</p>
337+
<% end %>
338+
</div>
339+
</body>
340+
</html>
341+
```
342+
206343
### Introspection
207344

208345
Litestream offers a handful of commands that allow you to introspect the state of your replication. The gem provides a few rake tasks that wrap these commands for you. For example, you can list the databases that Litestream is configured to replicate:

images/show-screenshot.png

256 KB
Loading

0 commit comments

Comments
 (0)