-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Vercel Serverless Hosting Support (#121)
* feat: Vercel Server Support * Add Docs
- Loading branch information
1 parent
a1d4796
commit 1b2272a
Showing
6 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
**➡️ Please note that a working templatereference can be found [here](https://github.com/GabrielTK/VercelSlashCreate).** | ||
|
||
The most important thing is to: | ||
1. Export the VercelServer instance: | ||
```ts | ||
const { VercelServer } = require('slash-create'); | ||
const vercelServer = new VercelServer(); | ||
creator | ||
.withServer(vercelServer) | ||
.registerCommandsIn(path.join(__dirname, 'commands')) | ||
//.syncCommands() | ||
|
||
export const vercel = vercelServer; | ||
export const slash = creator; | ||
``` | ||
2. Then, create a directory called `api`, with the two following files: | ||
|
||
* `interactions.ts` | ||
```ts | ||
import { vercel } from ".."; //This is the VercelServer we exported from the previous step. | ||
|
||
|
||
export default vercel.vercelEndpoint; | ||
``` | ||
|
||
* `resync.ts` - This file can be renamed, and will be used to resync the interactions with Discord API. I recommend you use this as a post-deploy hook. | ||
|
||
```ts | ||
import { VercelRequest, VercelResponse } from "@vercel/node"; | ||
import { slash } from ".."; | ||
|
||
const api = async (req: VercelRequest, res: VercelResponse) => { | ||
//I do recommend you add secret verification here, or some security check. | ||
slash.syncCommands(); | ||
//This basically waits until the sync is done | ||
let awaiter = new Promise((resolve,reject) => { | ||
slash.on('synced', () => { | ||
console.log("Elapsed Sync") | ||
resolve(true); | ||
}); | ||
}); | ||
slash.syncCommands(); | ||
await awaiter; | ||
res.status(200).send(JSON.stringify(slash.commands.map(c => [{name: c.commandName, description: c.description}]))); | ||
} | ||
export default api; | ||
``` | ||
|
||
Now, just push it to Vercel! Be sure to verify that the source files are not exposed, and that `/api/interactions` results in a response such as `Server only supports POST requests.` - That's the URL you'll use as your `INTERACTIONS ENDPOINT URL` on your [Discord Developers Page](https://discord.com/developers/) | ||
|
||
|
||
|
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 |
---|---|---|
|
@@ -30,3 +30,5 @@ | |
path: lambda.md | ||
- name: Azure Functions | ||
path: azure.md | ||
- name: Vercel | ||
path: vercel.md |
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,44 @@ | ||
import { Server, ServerRequestHandler } from '../server'; | ||
// @ts-ignore | ||
import { VercelRequest, VercelResponse } from '@vercel/node'; | ||
|
||
/** | ||
* A server for Vercel. | ||
* @see https://vercel.com/ | ||
* @see https://vercel.com/guides/handling-node-request-body | ||
*/ | ||
export class VercelServer extends Server { | ||
private _handler?: ServerRequestHandler; | ||
|
||
/** | ||
* @param moduleExports The exports for your module, must be `module.exports` | ||
* @param target The name of the exported function | ||
*/ | ||
constructor() { | ||
super({ alreadyListening: true }); | ||
// moduleExports = this._onRequest.bind(this); | ||
} | ||
|
||
vercelEndpoint = (req: VercelRequest, res: VercelResponse) => { | ||
if (!this._handler) return res.status(503).send('Server has no handler.'); | ||
if (req.method !== 'POST') return res.status(405).send('Server only supports POST requests.'); | ||
this._handler( | ||
{ | ||
headers: req.headers, | ||
body: req.body, | ||
request: req, | ||
response: res | ||
}, | ||
async (response) => { | ||
res.status(response.status || 200); | ||
if (response.headers) for (const key in response.headers) res.setHeader(key, response.headers[key] as string); | ||
res.send(response.body); | ||
} | ||
); | ||
}; | ||
|
||
/** @private */ | ||
createEndpoint(path: string, handler: ServerRequestHandler) { | ||
this._handler = 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