Packages

phoenix_kit

1.7.86
1.7.207 1.7.206 1.7.205 1.7.204 1.7.203 1.7.202 1.7.201 1.7.200 1.7.199 1.7.198 1.7.197 1.7.196 1.7.194 1.7.193 1.7.192 1.7.191 1.7.190 1.7.189 1.7.187 1.7.186 1.7.185 1.7.184 1.7.183 1.7.182 1.7.181 1.7.180 1.7.179 1.7.178 1.7.177 1.7.176 1.7.175 1.7.174 1.7.173 1.7.172 1.7.171 1.7.170 1.7.169 1.7.168 1.7.167 1.7.166 1.7.165 1.7.164 1.7.162 1.7.161 1.7.160 1.7.159 1.7.157 1.7.156 1.7.155 1.7.154 1.7.153 1.7.152 1.7.151 1.7.150 1.7.149 1.7.146 1.7.145 1.7.144 1.7.143 1.7.138 1.7.133 1.7.132 1.7.131 1.7.130 1.7.128 1.7.126 1.7.125 1.7.121 1.7.120 1.7.119 1.7.118 1.7.117 1.7.116 1.7.115 1.7.114 1.7.113 1.7.112 1.7.111 1.7.110 1.7.109 1.7.108 1.7.107 1.7.106 1.7.105 1.7.104 1.7.103 1.7.102 1.7.101 1.7.100 1.7.99 1.7.98 1.7.97 1.7.96 1.7.95 1.7.94 1.7.93 1.7.92 1.7.91 1.7.90 1.7.89 1.7.88 1.7.87 1.7.86 1.7.85 1.7.84 1.7.83 1.7.82 1.7.81 1.7.80 1.7.79 1.7.78 1.7.77 1.7.76 1.7.75 1.7.74 1.7.71 1.7.70 1.7.69 1.7.66 1.7.65 1.7.64 1.7.63 1.7.62 1.7.61 1.7.59 1.7.58 1.7.57 1.7.56 1.7.55 1.7.54 1.7.53 1.7.52 1.7.51 1.7.49 1.7.44 1.7.43 1.7.42 1.7.41 1.7.39 1.7.38 1.7.37 1.7.36 1.7.34 1.7.33 1.7.31 1.7.30 1.7.29 1.7.28 1.7.27 1.7.26 1.7.25 1.7.24 1.7.23 1.7.22 1.7.21 1.7.20 1.7.19 1.7.18 1.7.17 1.7.16 1.7.15 1.7.14 1.7.13 1.7.12 1.7.11 1.7.10 1.7.9 1.7.8 1.7.7 1.7.6 1.7.5 1.7.4 1.7.3 1.7.2 1.7.1 1.7.0 1.6.20 1.6.19 1.6.18 1.6.17 1.6.16 1.6.15 1.6.14 1.6.13 1.6.12 1.6.11 1.6.10 1.6.9 1.6.8 1.6.7 1.6.6 1.6.5 1.6.4 1.6.3 1.5.2 1.5.1 1.5.0 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.2 1.3.1 1.3.0 1.2.10 1.2.9 1.2.8 1.2.7 1.2.5 1.2.4 1.2.2 1.2.1 1.2.0 1.1.0 1.0.0

A foundation for building Elixir Phoenix apps — SaaS, social networks, ERP systems, marketplaces, and more

Current section

Files

Jump to
phoenix_kit lib modules maintenance README.md
Raw

lib/modules/maintenance/README.md

# Maintenance Mode Module
The Maintenance module provides a system-wide maintenance mode that allows you to temporarily show a maintenance page to non-admin users while you work on the site. Admins and owners can still access the site normally.
## Overview
This is a true maintenance mode system - not just a component you add to pages, but a system-wide feature that intercepts all page loads and conditionally shows maintenance content based on user permissions.
## Quick Start (Parent App Setup)
The maintenance mode module is automatically configured when you install PhoenixKit:
```bash
# Run the PhoenixKit installer
mix phoenix_kit.install
```
The installer automatically adds the required integration plug to your browser pipeline, so maintenance mode will work immediately after installation.
**That's it!** Now when you enable maintenance mode from `/admin/modules`, non-admin users will see the maintenance page on ALL pages of your site.
### What the Installer Does
The installer automatically adds this line to your `lib/your_app_web/router.ex`:
```elixir
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_live_flash
plug :put_root_layout, html: {YourAppWeb.Layouts, :root}
plug :protect_from_forgery
plug :put_secure_browser_headers
plug PhoenixKitWeb.Plugs.Integration # ← Added automatically
end
```
No manual configuration required!
## Core Features
- **System-Wide Protection** – When enabled, ALL pages show maintenance content to non-admin users
- **Role-Based Bypass** – Admins and owners can access the site normally while maintenance mode is active
- **Conditional Rendering** – No redirects - page content is replaced inline for seamless experience
- **Live Refresh** – When maintenance mode is disabled, users just refresh and see real content
- **Customizable Content** – Configure header and subtext via database settings
- **Database Storage** – Settings persisted in `phoenix_kit_settings` table
## Module Structure
```
lib/modules/maintenance/
├── README.md # This documentation
├── maintenance.ex # Main context module (pure Elixir)
├── settings.ex # Settings interface (pure Elixir)
└── web/ # Web-specific code
├── plugs/
│ └── maintenance_mode.ex # Maintenance mode plug
├── components/
│ └── maintenance_page.ex # Maintenance page component
└── settings.html.heex # Settings UI template
```
## Integration Points
- **Plug:** `PhoenixKitWeb.Plugs.Integration` (main entry point that internally calls `PhoenixKitWeb.Plugs.MaintenanceMode` at `lib/modules/maintenance/web/plugs/maintenance_mode.ex`)
- **Context module:** `PhoenixKit.Maintenance` (at `lib/modules/maintenance/maintenance.ex`)
- **Settings interface:** `PhoenixKit.Maintenance` settings API (at `lib/modules/maintenance/settings.ex`)
- **Component:** `MaintenancePage.maintenance_page/1` (at `lib/modules/maintenance/web/components/maintenance_page.ex`)
- **Auth integration:** Checks user session for admin/owner role
- **Module card:** Displayed in Modules dashboard at `{prefix}/admin/modules`
- **Settings storage:** Database-backed via `phoenix_kit_settings` table
## How It Works
### 1. Plug Intercepts Request
When a request comes in, the `MaintenanceMode` plug runs early in the browser pipeline:
```elixir
def call(conn, _opts) do
if UnderConstruction.enabled?() do
user = get_user_from_session(conn)
scope = Scope.for_user(user)
if scope && (Scope.admin?(scope) || Scope.owner?(scope)) do
conn # Admin/Owner bypasses - continue to route normally
else
render_maintenance_page(conn) # Non-admin sees maintenance page
end
else
conn # Maintenance disabled - continue normally
end
end
```
### 2. Role Check
The plug:
1. Gets user from session token (if exists)
2. Creates scope for the user
3. Checks if user is admin or owner
4. Returns maintenance page HTML for non-admin users
5. Halts connection so request never reaches routes
### 3. Maintenance Page Rendering
For non-admin users, the plug renders a complete HTML page directly:
```elixir
defp render_maintenance_page(conn) do
config = UnderConstruction.get_config()
html = """
<!DOCTYPE html>
<html>
<head>
<title>#{config.header}</title>
<link rel="stylesheet" href="/assets/app.css" />
</head>
<body>
<!-- Full maintenance page with header, subtext, animation -->
</body>
</html>
"""
conn
|> send_resp(:service_unavailable, html)
|> halt()
end
```
## Context API
### `PhoenixKit.Maintenance`
**Available functions:**
- `enabled?/0` – Check if maintenance mode is active (returns boolean)
- `enable_system/0` – Enable maintenance mode
- `disable_system/0` – Disable maintenance mode
- `get_header/0` – Get maintenance page header text
- `get_subtext/0` – Get maintenance page subtext
- `update_header/1` – Update header text
- `update_subtext/1` – Update subtext
- `get_config/0` – Get full configuration map
**Examples:**
```elixir
# Check if maintenance mode is enabled
if PhoenixKit.Maintenance.enabled?() do
# Maintenance mode is active
end
# Enable maintenance mode
{:ok, setting} = PhoenixKit.Maintenance.enable_system()
# Disable maintenance mode
{:ok, setting} = PhoenixKit.Maintenance.disable_system()
# Update maintenance page content
PhoenixKit.Maintenance.update_header("Coming Soon!")
PhoenixKit.Maintenance.update_subtext("We're launching something amazing...")
# Get full configuration
config = PhoenixKit.Maintenance.get_config()
# => %{
# enabled: true,
# header: "Maintenance Mode",
# subtext: "We'll be back soon..."
# }
```
## Component Reference
### `MaintenancePage.maintenance_page/1`
Renders a full-page maintenance message with customizable content.
**Attributes:**
- `header` (string, optional) – Main heading text (default: loaded from settings)
- `subtext` (string, optional) – Descriptive message (default: loaded from settings)
**Examples:**
```heex
<%!-- Use settings from database --%>
<PhoenixKitWeb.Components.Core.MaintenancePage.maintenance_page />
<%!-- Custom content --%>
<PhoenixKitWeb.Components.Core.MaintenancePage.maintenance_page
header="Coming Soon"
subtext="We're building something special!"
/>
```
## Module Dashboard Integration
The maintenance mode module appears in the Modules dashboard at `{prefix}/admin/modules` with:
- **Toggle switch** – Enable/disable maintenance mode system-wide
- **Status badges** – Shows "Active" (warning) or "Inactive" (neutral)
- **Warning alert** – When active, shows "Non-admin users see maintenance page"
- **Content preview** – Displays current header and subtext
- **Stats** – Explains that admins/owners can still access the site
## Settings Storage
The module uses the PhoenixKit Settings system to persist configuration:
**Setting keys:**
- `maintenance_enabled` (boolean, default: `false`)
- `maintenance_header` (string, default: `"Maintenance Mode"`)
- `maintenance_subtext` (string, default: `"We'll be back soon. Our team is working hard to bring you something amazing!"`)
**Storage:** `phoenix_kit_settings` database table
## Use Cases
1. **Scheduled Maintenance** – Enable before deploying major updates
2. **Emergency Downtime** – Quickly show maintenance page during incidents
3. **Beta Testing** – Allow only admin team to access while testing new features
4. **Gradual Rollout** – Keep site in maintenance mode while adding team members
5. **Development Mode** – Work on live site without affecting users
## User Experience Flow
### Regular User
1. User visits site
2. `on_mount` hook checks maintenance mode → enabled
3. User is not admin → `@show_maintenance = true`
4. Layout renders maintenance page instead of content
5. User sees "Maintenance Mode" message
6. Admin disables maintenance mode
7. User refreshes page → sees real content (no redirect needed)
### Admin/Owner User
1. Admin visits site
2. `on_mount` hook checks maintenance mode → enabled
3. Admin has elevated role → `@show_maintenance = false`
4. Layout renders normal content
5. Admin sees real site and can work normally
6. (Optional) Admin sees warning banner at top
## Design Considerations
- **No Redirects** – Content replacement is inline, no URL changes
- **Live Refresh** – Users just refresh when maintenance ends
- **Role-Based** – Owner and Admin roles bypass automatically
- **Settings-Driven** – All configuration stored in database
- **Graceful Degradation** – If settings missing, uses sensible defaults
- **Performance** – Check runs only on mount, minimal overhead
## Customization Examples
### Update Maintenance Message
```elixir
# Via IEx or Phoenix console
PhoenixKit.Maintenance.update_header("Scheduled Maintenance")
PhoenixKit.Maintenance.update_subtext("We'll be back online at 3:00 PM EST. Thank you for your patience!")
```
### Enable/Disable via Code
```elixir
# Enable for deployment
PhoenixKit.Maintenance.enable_system()
# Run migrations, deploy new code, test, etc.
# Disable when ready
PhoenixKit.Maintenance.disable_system()
```
### Check Status in Templates
```heex
<%= if PhoenixKit.Maintenance.enabled?() do %>
<div class="alert alert-warning">
Maintenance mode is currently active for non-admin users.
</div>
<% end %>
```
## Operational Notes
- The maintenance check runs on every LiveView mount via `on_mount` hook
- Regular Phoenix controllers (non-LiveView) are not affected by this system
- Static assets and auth routes work normally even in maintenance mode
- The `show_maintenance` assign is set per-socket, allowing real-time updates
- When disabled, the `show_maintenance` assign is `false` for all users
- No database queries on each request - settings are cached
## Future Enhancements
Potential additions to this module could include:
- Admin warning banner when maintenance mode is active
- Scheduled maintenance mode (enable/disable at specific times)
- Custom maintenance page templates
- IP whitelist for bypassing maintenance mode
- Maintenance mode history/audit log
- API endpoint protection during maintenance
- Email notifications when maintenance mode changes
- Integration with deployment systems
## Troubleshooting
### Users still see normal content after enabling
- Check that the LiveView is using `on_mount: [{PhoenixKitWeb.Users.Auth, :phoenix_kit_mount_current_scope}]`
- Verify `show_maintenance` assign is being set in socket
- Check that `root.html.heex` layout includes the maintenance mode check
### Admins see maintenance page
- Verify user has Admin or Owner role in database
- Check that `phoenix_kit_current_scope` is being assigned correctly
- Ensure `Scope.admin?()` or `Scope.owner?()` returns true for the user
### Maintenance page doesn't match settings
- Clear browser cache and refresh
- Check Settings database for correct values
- Verify `PhoenixKit.Maintenance.get_header()` returns expected value
Update this README whenever new features, components, or workflows are added to the Maintenance module so CLAUDE.md can remain lightweight.