Skip to content

Commit

Permalink
feat(api): add create aspect to maintainence
Browse files Browse the repository at this point in the history
add create aspect to maintaince to more selectively disable
  • Loading branch information
hannahhoward committed Jul 1, 2024
1 parent f79a38b commit b0c9213
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ PICKUP_BASIC_AUTH_TOKEN = dGVzdDp0ZXN0
PICKUP_API_URL = http://127.0.0.1:9094

# Maintenance Mode
MAINTENANCE_MODE = rw
MAINTENANCE_MODE = r--

# S3
S3_ENDPOINT = http://127.0.0.1:9000
Expand Down
29 changes: 15 additions & 14 deletions packages/api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import { getServiceConfig } from './config.js'
import {
withMode,
READ_ONLY as RO,
READ_WRITE as RW,
READ_WRITE_ONLY as RW,
READ_WRITE_CREATE as RWC,
} from './middleware/maintenance.js'
import { getContext } from './utils/context.js'
import { withAuth } from './middleware/auth.js'
Expand Down Expand Up @@ -96,7 +97,7 @@ r.add(
r.add(
'post',
'/pins',
withAuth(withMode(pinsAdd, RW), {
withAuth(withMode(pinsAdd, RWC), {
checkHasPsaAccess,
checkHasAccountRestriction,
}),
Expand All @@ -105,7 +106,7 @@ r.add(
r.add(
'post',
'/pins/:requestid',
withAuth(withMode(pinsReplace, RW), {
withAuth(withMode(pinsReplace, RWC), {
checkHasPsaAccess,
checkHasAccountRestriction,
}),
Expand All @@ -114,7 +115,7 @@ r.add(
r.add(
'delete',
'/pins/:requestid',
withAuth(withMode(pinsDelete, RW), {
withAuth(withMode(pinsDelete, RWC), {
checkHasDeleteRestriction,
checkHasPsaAccess,
}),
Expand All @@ -128,7 +129,7 @@ r.add('get', '/:cid', withAuth(withMode(nftGet, RO)), [postCors])
r.add(
'post',
'/upload',
withAuth(withMode(nftUpload, RW), {
withAuth(withMode(nftUpload, RWC), {
checkHasAccountRestriction,
checkUcan,
}),
Expand All @@ -137,24 +138,24 @@ r.add(
r.add(
'patch',
'/upload/:cid',
withAuth(withMode(nftUpdateUpload, RW), { checkHasAccountRestriction }),
withAuth(withMode(nftUpdateUpload, RWC), { checkHasAccountRestriction }),
[postCors]
)
r.add(
'post',
'/store',
withAuth(withMode(nftStore, RW), { checkHasAccountRestriction }),
withAuth(withMode(nftStore, RWC), { checkHasAccountRestriction }),
[postCors]
)
r.add(
'delete',
'/:cid',
withAuth(withMode(nftDelete, RW), { checkHasDeleteRestriction }),
withAuth(withMode(nftDelete, RWC), { checkHasDeleteRestriction }),
[postCors]
)

// Temporary Metaplex upload route, mapped to metaplex user account.
r.add('post', '/metaplex/upload', withMode(metaplexUpload, RW), [postCors])
r.add('post', '/metaplex/upload', withMode(metaplexUpload, RWC), [postCors])

// User
r.add(
Expand Down Expand Up @@ -206,7 +207,7 @@ r.add(
r.add(
'post',
'/api/pins',
withAuth(withMode(pinsAdd, RW), {
withAuth(withMode(pinsAdd, RWC), {
checkHasPsaAccess,
checkHasAccountRestriction,
}),
Expand All @@ -215,7 +216,7 @@ r.add(
r.add(
'post',
'/api/pins/:requestid',
withAuth(withMode(pinsReplace, RW), {
withAuth(withMode(pinsReplace, RWC), {
checkHasPsaAccess,
checkHasAccountRestriction,
}),
Expand All @@ -224,7 +225,7 @@ r.add(
r.add(
'delete',
'/api/pins/:requestid',
withAuth(withMode(pinsDelete, RW), {
withAuth(withMode(pinsDelete, RWC), {
checkHasDeleteRestriction,
checkHasPsaAccess,
}),
Expand All @@ -238,13 +239,13 @@ r.add('get', '/api/:cid', withAuth(withMode(nftGet, RO)), [postCors])
r.add(
'post',
'/api/upload',
withAuth(withMode(nftUpload, RW), { checkUcan, checkHasAccountRestriction }),
withAuth(withMode(nftUpload, RWC), { checkUcan, checkHasAccountRestriction }),
[postCors]
)
r.add(
'delete',
'/api/:cid',
withAuth(withMode(nftDelete, RW), { checkHasDeleteRestriction }),
withAuth(withMode(nftDelete, RWC), { checkHasDeleteRestriction }),
[postCors]
)

Expand Down
22 changes: 16 additions & 6 deletions packages/api/src/middleware/maintenance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@ import { ErrorMaintenance, HTTPError } from '../errors.js'
import { getServiceConfig } from '../config.js'

/**
* @typedef {'rw' | 'r-' | '--'} Mode
* @typedef {'rwc' | 'rw-' | 'r--' | '---'} Mode
* @typedef {import('../bindings').Handler} Handler
*/

/**
* Read and write and create
*/
export const READ_WRITE_CREATE = 'rwc'

/**
* Read and write.
*/
export const READ_WRITE = 'rw'
export const READ_WRITE_ONLY = 'rw-'

/**
* Read only mode.
*/
export const READ_ONLY = 'r-'
export const READ_ONLY = 'r--'

/**
* No reading or writing.
*/
export const NO_READ_OR_WRITE = '--'
export const NO_READ_OR_WRITE = '---'

/** @type {readonly Mode[]} */
export const modes = Object.freeze([NO_READ_OR_WRITE, READ_ONLY, READ_WRITE])
export const modes = Object.freeze([
NO_READ_OR_WRITE,
READ_ONLY,
READ_WRITE_ONLY,
READ_WRITE_CREATE,
])

/**
* The default maintenance mode (normal operation).
*/
export const DEFAULT_MODE = READ_WRITE
export const DEFAULT_MODE = READ_WRITE_CREATE

/** @type {() => Mode} */
let getMaintenanceMode = () => getServiceConfig().MAINTENANCE_MODE
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/maintenance.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import {
READ_ONLY,
READ_WRITE,
READ_WRITE_ONLY,
NO_READ_OR_WRITE,
} from '../src/middleware/maintenance.js'
import { createClientWithUser } from './scripts/helpers.js'
Expand Down Expand Up @@ -46,7 +46,7 @@ test('maintenance middleware should throw error when in maintenance mode', async

const expectedError = { message: /API undergoing maintenance/ }

await setMode(t, READ_WRITE)
await setMode(t, READ_WRITE_ONLY)
await t.notThrowsAsync(tryRead(t, token))
await t.notThrowsAsync(tryWrite(t, token))

Expand Down

0 comments on commit b0c9213

Please sign in to comment.