Skip to content

Commit 454e0c7

Browse files
committed
added files
0 parents  commit 454e0c7

27 files changed

+1048
-0
lines changed

Diff for: .editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.json]
12+
insert_final_newline = ignore
13+
14+
[**.min.js]
15+
indent_style = ignore
16+
insert_final_newline = ignore
17+
18+
[MakeFile]
19+
indent_style = space
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

Diff for: .github/lock.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
ignoreUnless: {{ STALE_BOT }}
3+
---
4+
# Configuration for Lock Threads - https://github.com/dessant/lock-threads-app
5+
6+
# Number of days of inactivity before a closed issue or pull request is locked
7+
daysUntilLock: 60
8+
9+
# Skip issues and pull requests created before a given timestamp. Timestamp must
10+
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
11+
skipCreatedBefore: false
12+
13+
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
14+
exemptLabels: ['Type: Security']
15+
16+
# Label to add before locking, such as `outdated`. Set to `false` to disable
17+
lockLabel: false
18+
19+
# Comment to post before locking. Set to `false` to disable
20+
lockComment: >
21+
This thread has been automatically locked since there has not been
22+
any recent activity after it was closed. Please open a new issue for
23+
related bugs.
24+
25+
# Assign `resolved` as the reason for locking. Set to `false` to disable
26+
setLockReason: false

Diff for: .github/stale.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
ignoreUnless: {{ STALE_BOT }}
3+
---
4+
# Number of days of inactivity before an issue becomes stale
5+
daysUntilStale: 60
6+
7+
# Number of days of inactivity before a stale issue is closed
8+
daysUntilClose: 7
9+
10+
# Issues with these labels will never be considered stale
11+
exemptLabels:
12+
- 'Type: Security'
13+
14+
# Label to use when marking an issue as stale
15+
staleLabel: 'Status: Abandoned'
16+
17+
# Comment to post when marking an issue as stale. Set to `false` to disable
18+
markComment: >
19+
This issue has been automatically marked as stale because it has not had
20+
recent activity. It will be closed if no further activity occurs. Thank you
21+
for your contributions.
22+
23+
# Comment to post when closing a stale issue. Set to `false` to disable
24+
closeComment: false

Diff for: .github/workflows/test.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: test
2+
3+
on:
4+
- push
5+
- pull_request
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- name: Install
13+
run: npm install
14+
- name: Run lint
15+
run: npm run lint
16+
17+
typecheck:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Install
22+
run: npm install
23+
- name: Run typecheck
24+
run: npm run typecheck
25+
26+
tests:
27+
runs-on: ${{ matrix.os }}
28+
strategy:
29+
matrix:
30+
os: [ubuntu-latest, windows-latest]
31+
node-version:
32+
- 20.10.0
33+
- 21.x
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Use Node.js ${{ matrix.node-version }}
37+
uses: actions/setup-node@v1
38+
with:
39+
node-version: ${{ matrix.node-version }}
40+
- name: Install
41+
run: npm install
42+
- name: Run tests
43+
run: npm test
44+
windows:
45+
runs-on: windows-latest
46+
strategy:
47+
matrix:
48+
node-version:
49+
- 20.10.0
50+
- 21.x
51+
steps:
52+
- uses: actions/checkout@v2
53+
- name: Use Node.js ${{ matrix.node-version }}
54+
uses: actions/setup-node@v1
55+
with:
56+
node-version: ${{ matrix.node-version }}
57+
- name: Install
58+
run: npm install
59+
- name: Run tests
60+
run: npm test

Diff for: .gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules
2+
coverage
3+
.DS_STORE
4+
.nyc_output
5+
.idea
6+
.vscode/
7+
*.sublime-project
8+
*.sublime-workspace
9+
*.log
10+
build
11+
dist
12+
yarn.lock
13+
shrinkwrap.yaml
14+
package-lock.json

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

Diff for: .prettierignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
docs
3+
coverage
4+
*.html

Diff for: LICENSE.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The MIT License
2+
3+
Copyright (c) 2023
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: README.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# AdonisJS Maintenance Mode
2+
3+
>This package enables a simple maintenance mode in AdonisJS applications. When activated, the application temporarily disables access for users, displaying a maintenance message or page.
4+
5+
## Features
6+
- Toggle maintenance mode with CLI commands
7+
- Customize maintenance mode with templates, HTTP status, retries, refresh intervals, redirection, and secret access
8+
- Exclude specific URLs from maintenance mode
9+
- Uses `cookie-based` access control
10+
- File-based, creating a maintenance file in the `tmp/` directory
11+
## Installation
12+
13+
You can install the package using either node ace or npm.
14+
15+
### Using Adonis CLI
16+
```bash
17+
node ace add adonisjs-maintenance-mode
18+
```
19+
### Or Using NPM
20+
```bash
21+
npm install adonis-maintenance-mode
22+
```
23+
## Configuration
24+
After installation, configure maintenance mode in the following ways:
25+
26+
- **Middleware:** A `block_requests_during_maintenance_middleware.ts` file is generated in the middleware folder. Inside this file:
27+
- Define `excludedUrls:` an array of URLs that should bypass maintenance mode.
28+
29+
Environment Variable:
30+
- Add `APP_URL` to your .env file to specify your application’s base URL.
31+
32+
## Usage
33+
Activating Maintenance Mode
34+
35+
Enable maintenance mode via CLI:
36+
37+
```bash
38+
node ace maintenance:on
39+
```
40+
This command creates a file called `maintenance_mode_on` in the `tmp/` directory, enabling maintenance mode.
41+
42+
Command Options:
43+
- `--template:` Specify a custom maintenance view template under the resources folder.
44+
```bash
45+
node ace maintenance:on --template="pages/errors/503"
46+
```
47+
48+
- `--status:` Set a custom HTTP status code (default is 503).
49+
```bash
50+
node ace maintenance:on --status=503
51+
```
52+
53+
- `--retry:` Specify a retry interval for retrying requests.
54+
```bash
55+
node ace maintenance:on --retry=60
56+
```
57+
58+
- `--refresh:` Set the page refresh interval.
59+
```bash
60+
node ace maintenance:on --refresh=30
61+
```
62+
63+
- `--redirect:` Provide a URL to redirect users during maintenance mode.
64+
```bash
65+
node ace maintenance:on --redirect=example-url
66+
```
67+
68+
- `--secret:` Add a secret key to bypass maintenance mode. When using `--secret`, ensure `APP_URL` is set in your .env file. The default value for `APP_URL` is `http://$HOST:$PORT`, but you may customize it to match your application's URL.
69+
```bash
70+
node ace maintenance:on --secret=mysecretkey
71+
```
72+
73+
You can also generate a random secret key:
74+
```bash
75+
node ace maintenance:on --secret=random
76+
```
77+
78+
## Custom Error Pages Configuration
79+
The `statusPages` configuration in `handler.ts` allows you to define custom renderers for specific HTTP status codes, enabling personalized error pages for various scenarios, including maintenance mode.
80+
81+
```typescript
82+
protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
83+
'503': (error, { view }) => {
84+
return view.render('pages/errors/503', { error })
85+
},
86+
}
87+
```
88+
### !! Note on command --template option !!
89+
If you enable maintenance mode with the `--template` option (e.g., node ace maintenance:on --template=path_to_template), the template specified will automatically be used for the 503 error response, making manual changes in `handler.ts` unnecessary. This provides flexibility for setting custom templates dynamically without requiring code modifications.
90+
91+
## Deactivating Maintenance Mode
92+
Turn off maintenance mode using the following command:
93+
94+
```bash
95+
node ace maintenance:off
96+
```
97+
98+
This command removes the `maintenance_mode_on` file from the `tmp/` directory.
99+
## Deactivating Maintenance Mode
100+
Turn off maintenance mode using the following command:
101+
102+
```bash
103+
node ace maintenance:off
104+
```
105+
This command removes the `maintenance_mode_on` file from the `tmp/` directory.
106+
107+
## Example Middleware Usage
108+
In `#middleware/block_requests_during_maintenance_middleware.ts`, you’ll find excludedUrls where you can list URLs that should bypass maintenance mode. This is useful for allowing access to specific resources or pages even during maintenance.
109+
110+
* The `'*'` wildcard allows access to any route that starts with `/users/`, meaning routes like `/users/1`, `/users/profile`, and any other `/users/{sub-route}` will be accessible during maintenance mode.
111+
* If you add users to the excluded array, all routes beginning with users will be accessible, including `/users` itself.
112+
* If you specify `users/*`, only routes under `/users/` `(like /users/1, /users/profile)` will be accessible, but not /users itself.
113+
* Using `users*` allows access to all routes starting with users, equivalent to simply users.
114+
115+
```typescript
116+
import EnsureMaintenanceMode from 'adonisjs-maintenance-mode14/ensure_maintenance_mode'
117+
118+
/**
119+
* BlockRequestsDuringMaintenanceMiddleware class extends the functionality of
120+
* EnsureMaintenanceMode to block requests during maintenance mode,
121+
* while allowing the specification of certain URLs to be excluded.
122+
*
123+
* @class
124+
* @extends EnsureMaintenanceMode
125+
*
126+
* @property {string[]} excludedUrls - An array of URL strings that are excluded from blocking
127+
* requests during maintenance mode.
128+
*/
129+
export default class BlockRequestsDuringMaintenanceMiddleware extends EnsureMaintenanceMode {
130+
/**
131+
* An array of URL paths that should be excluded from being blocked during maintenance mode.
132+
* Requests to these URLs will bypass the maintenance check.
133+
*
134+
* @protected
135+
* @type {string[]}
136+
*/
137+
protected excludedUrls: string[] = [
138+
'users/*',
139+
'about',
140+
]
141+
}
142+
```

Diff for: bin/test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { assert } from '@japa/assert'
2+
import { configure, processCLIArgs, run } from '@japa/runner'
3+
import { fileSystem } from '@japa/file-system'
4+
import { apiClient } from '@japa/api-client'
5+
6+
processCLIArgs(process.argv.splice(2))
7+
8+
configure({
9+
files: ['tests/**/*.spec.ts'],
10+
plugins: [assert(), fileSystem(), apiClient('http://localhost:3333')],
11+
})
12+
13+
run()

Diff for: commands/maintenance_off.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { BaseCommand } from '@adonisjs/core/ace'
2+
import { CommandOptions } from '@adonisjs/core/types/ace'
3+
4+
export default class MaintenanceOff extends BaseCommand {
5+
static commandName = 'maintenance:off'
6+
static description = 'Takes the application out of maintenance mode.'
7+
8+
static options: CommandOptions = {
9+
startApp: true,
10+
}
11+
12+
async run() {
13+
try {
14+
const maintenanceModeService = await this.app.container.make('maintenanceMode')
15+
16+
if (!(await maintenanceModeService.isEnabled())) {
17+
this.logger.info('Application is already maintenance mode.')
18+
return
19+
}
20+
21+
await maintenanceModeService.disabled()
22+
23+
this.logger.success('Application is now live.')
24+
} catch (error) {
25+
this.logger.error(error)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)