Skip to content

Commit

Permalink
feat: ✨ add network flag and enhance CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jan 5, 2025
1 parent 7da45cf commit b666ece
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ Start a GenAIScript local server
Options:
-p, --port <number> Specify the port number, default: 8003
-k, --api-key <string> API key to authenticate requests
-n, --network Opens server on 0.0.0.0 to make it accessible on the
network
-c, --cors Enable CORS
-h, --help display help for command
```
Expand Down
21 changes: 19 additions & 2 deletions docs/src/content/docs/reference/cli/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@ sidebar:
Launch a local web server that is used to run the playground
or Visual Studio Code.


Run from the workspace root:

```bash
npx genaiscript serve
```
```

## port

The default port is `8003`. You can specify the port by setting the `--port` flag.

```bash
npx genaiscript serve --port 8004
```

## CORS

You can enable [Cross Origin Shared Resource](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) by setting the `--cors` flag.

```bash
npx genaiscript serve --cors
```

By default, the origin is set to `*`. You can specify the origin by setting the `GENAISCRIPT_CORS_ORIGIN` environment variable.
4 changes: 4 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ export async function cli() {
`Specify the port number, default: ${SERVER_PORT}`
)
.option("-k, --api-key <string>", "API key to authenticate requests")
.option(
"-n, --network",
"Opens server on 0.0.0.0 to make it accessible on the network"
)
.option("-c, --cors", "Enable CORS")
.action(startServer) // Action to start the server

Expand Down
20 changes: 14 additions & 6 deletions packages/cli/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ export async function startServer(options: {
httpPort?: string
apiKey?: string
cors?: boolean
network?: boolean
}) {
// Parse and set the server port, using a default if not specified.
const cors = !!options.cors
const port = parseInt(options.port) || SERVER_PORT
const apiKey = options.apiKey ?? process.env.GENAISCRIPT_API_KEY
const serverHost = options.network ? "0.0.0.0" : "127.0.0.1"

const wss = new WebSocketServer({ noServer: true })

Expand Down Expand Up @@ -435,13 +437,19 @@ export async function startServer(options: {

if (method === "OPTIONS") {
if (!cors) {
res.end(405)
res.statusCode = 405
res.end()
} else {
res.setHeader("Access-Control-Allow-Origin", "*")
const origin = process.env.GENAISCRIPT_CORS_ORIGIN || "*"
res.setHeader("Access-Control-Allow-Origin", origin)
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET")
res.setHeader("Access-Control-Max-Age", 24 * 3600) // 1 day
res.setHeader("Access-Control-Allow-Headers", "content-type")
res.end(204)
res.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Accept"
)
res.statusCode = 204
res.end()
}
return
}
Expand Down Expand Up @@ -516,9 +524,9 @@ export async function startServer(options: {
} else socket.destroy()
})
// Start the HTTP server on the specified port.
httpServer.listen(port, () => {
httpServer.listen(port, serverHost, () => {
console.log(
`GenAIScript server v${CORE_VERSION} at http://127.0.0.1:${port}/${apiKey ? `#api-key:${encodeURIComponent(apiKey)}` : ""}`
`GenAIScript server v${CORE_VERSION} at http://${serverHost}:${port}/${apiKey ? `#api-key:${encodeURIComponent(apiKey)}` : ""}`
)
})
}

0 comments on commit b666ece

Please sign in to comment.