-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom headers per branch or site (#195)
- Loading branch information
Geoffroy Empain
committed
Mar 10, 2021
1 parent
bc5e3ff
commit c8645a7
Showing
10 changed files
with
130 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { object, string } from 'joi'; | ||
|
||
export interface Header { | ||
name: string; | ||
value: string; | ||
} | ||
|
||
export const $header = object({ | ||
name: string().required(), | ||
value: string().optional().empty(''), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Request, Response } from 'express'; | ||
import { array, object } from 'joi'; | ||
import { ARRAY_MAX } from '../../constants'; | ||
import { $header } from './header'; | ||
import { emitEvent } from '../../events/emit-event'; | ||
import { EventType } from '../../events/event-type'; | ||
import { wrapAsyncMiddleware } from '../../commons/utils/wrap-async-middleware'; | ||
import { configureSiteInCaddy } from '../../caddy/configuration'; | ||
import { body } from '../../commons/express-joi/body'; | ||
import { canAdminSiteGuard } from './guards/can-admin-site-guard'; | ||
import { Sites } from './site'; | ||
import { Logger } from '../../commons/logger/logger'; | ||
import { serializeSite } from './serialize-site'; | ||
import { siteExistsGuard } from './guards/site-exists-guard'; | ||
|
||
const validators = [ | ||
body(object({ | ||
headers: array().min(0).max(ARRAY_MAX).optional() | ||
.default([]) | ||
.items($header) | ||
.unique((a, b) => a.name.toLowerCase() === b.name.toLowerCase()), | ||
})), | ||
]; | ||
|
||
const logger = new Logger('meli.api:setBranchHeaders'); | ||
|
||
async function handler(req: Request, res: Response): Promise<void> { | ||
const { siteId } = req.params; | ||
|
||
await Sites().updateOne({ | ||
_id: siteId, | ||
}, { | ||
$set: { | ||
headers: req.body.headers, | ||
}, | ||
}); | ||
|
||
const site = await Sites().findOne({ | ||
_id: siteId, | ||
}); | ||
|
||
configureSiteInCaddy(site).catch(err => { | ||
logger.error(err); | ||
}); | ||
|
||
emitEvent(EventType.site_updated, { | ||
site, | ||
}); | ||
|
||
res.json(serializeSite(site)); | ||
} | ||
|
||
export const setSiteHeaders = [ | ||
...siteExistsGuard, | ||
...canAdminSiteGuard, | ||
...validators, | ||
wrapAsyncMiddleware(handler), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters