Skip to content

Commit

Permalink
feat: prompt server port during setup (PalisadoesFoundation#2263)
Browse files Browse the repository at this point in the history
* feat: prompt server port during setup

* fix: parseInt for env

* fix: SERVER_PORT in github env

* fix:set SERVER_PORT as env

* refactor: remove console log

* fix: positioning of text
  • Loading branch information
NamitBhutani authored May 1, 2024
1 parent eb6470b commit 9fa6a1c
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 138 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
# Possible values are: development, production, test
NODE_ENV=development

#This environment variable is used to provide the port on which the server will run
SERVER_PORT=


# This environment variable is used to provide a unique random secret key for
# for signing/verifying access tokens using JWT(jsonwebtokens)

Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ jobs:

- name: Generate Refresh Token Secret
run: echo "REFRESH_TOKEN_SECRET=$(openssl rand -hex 32)" >> $GITHUB_ENV

- name: Set up SERVER_PORT env
run: echo "SERVER_PORT=4000" >> $GITHUB_ENV

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
Expand All @@ -231,7 +234,10 @@ jobs:
uses: juliangruber/sleep-action@v2
with:
time: 10s


- name: Create a videos directory
run: mkdir -p videos

- name: Run the tests
run: npm run test

Expand Down
358 changes: 227 additions & 131 deletions INSTALLATION.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Core features include:

<!-- toc -->

- [Talawa Components](#talawa-components)
- [Talawa Components](#talawa-components)
- [Documentation](#documentation)
- [Videos](#videos)

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
context: .
dockerfile: Dockerfile.dev
ports:
- 4000:4000
- "${SERVER_PORT:-4000}:${SERVER_PORT:-4000}"
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
Expand Down
14 changes: 13 additions & 1 deletion setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,19 @@ async function main(): Promise<void> {
await recaptchaSiteKey();
}

const { serverPort } = await inquirer.prompt([
{
type: "input",
name: "serverPort",
message: "Enter the server port:",
default: 4000,
},
]);
if (process.env.NODE_ENV === "development") {
const config = dotenv.parse(fs.readFileSync(".env"));
config.SERVER_PORT = serverPort;
updateEnvVariable(config);
}
console.log(
"\n You can configure either SMTP or Mail for sending emails through Talawa.\n",
);
Expand All @@ -864,7 +877,6 @@ async function main(): Promise<void> {
`Mail username already exists with the value ${process.env.MAIL_USERNAME}`,
);
}

const { shouldSetMail } = await inquirer.prompt([
{
type: "confirm",
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,8 @@ export const MAXIMUM_IMAGE_SIZE_LIMIT_KB = 20000;

export const BASE_URL = `http://localhost:${process.env.port || 4000}/`;

export const SERVER_PORT = ENV.SERVER_PORT;

export const ACCESS_TOKEN_SECRET = ENV.ACCESS_TOKEN_SECRET;

export const REFRESH_TOKEN_SECRET = ENV.REFRESH_TOKEN_SECRET;
Expand Down
1 change: 1 addition & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const envSchema = z.object({
NODE_ENV: z
.string()
.refine((value) => ["development", "production"].includes(value)),
SERVER_PORT: z.string(),
ACCESS_TOKEN_SECRET: z.string(),
REFRESH_TOKEN_SECRET: z.string(),
MONGO_DB_URL: z.string().url(),
Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { logger } from "./libraries";
import { isAuth } from "./middleware";
import { composedResolvers } from "./resolvers";
import { typeDefs } from "./typeDefs";
import { SERVER_PORT } from "./constants";
export const pubsub = new PubSub();

// defines schema
Expand Down Expand Up @@ -121,7 +122,7 @@ async function startServer(): Promise<void> {

// Modified server startup
await new Promise<void>((resolve) =>
httpServer.listen({ port: 4000 }, resolve),
httpServer.listen({ port: parseInt(SERVER_PORT as string) }, resolve),

Check failure on line 125 in src/index.ts

View workflow job for this annotation

GitHub Actions / Testing Application (20.x)

Unhandled error

RangeError: options.port should be >= 0 and < 65536. Received type number (NaN). ❯ Server.listen node:net:1945:5 ❯ src/index.ts:125:16 ❯ startServer src/index.ts:124:9 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_SOCKET_BAD_PORT' } This error originated in "tests/resolvers/Mutation/updatePluginStatus.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/resolvers/Mutation/updatePluginStatus.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 125 in src/index.ts

View workflow job for this annotation

GitHub Actions / Testing Application (20.x)

Unhandled error

RangeError: options.port should be >= 0 and < 65536. Received type number (NaN). ❯ new NodeError node:internal/errors:405:5 ❯ validatePort node:internal/validators:409:11 ❯ Server.listen node:net:1945:5 ❯ src/index.ts:125:16 ❯ startServer src/index.ts:124:9 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_SOCKET_BAD_PORT' } This error originated in "tests/resolvers/Mutation/updatePluginStatus.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "tests/resolvers/Mutation/updatePluginStatus.spec.ts". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 125 in src/index.ts

View workflow job for this annotation

GitHub Actions / Testing Application (20.x)

Unhandled error

RangeError: options.port should be >= 0 and < 65536. Received type number (NaN). ❯ new NodeError node:internal/errors:405:5 ❯ validatePort node:internal/validators:409:11 ❯ Server.listen node:net:1945:5 ❯ src/index.ts:125:16 ❯ startServer src/index.ts:124:9 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_SOCKET_BAD_PORT' } This error originated in "tests/resolvers/Mutation/createPlugin.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "Creates a Plugin and returns it". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 125 in src/index.ts

View workflow job for this annotation

GitHub Actions / Testing Application (20.x)

Unhandled error

RangeError: options.port should be >= 0 and < 65536. Received type number (NaN). ❯ new NodeError node:internal/errors:405:5 ❯ validatePort node:internal/validators:409:11 ❯ Server.listen node:net:1945:5 ❯ src/index.ts:125:16 ❯ startServer src/index.ts:124:9 ❯ processTicksAndRejections node:internal/process/task_queues:95:5 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_SOCKET_BAD_PORT' } This error originated in "tests/resolvers/Mutation/createPlugin.spec.ts" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "Creates a Plugin and returns it". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
);

// Log all the configuration related issues
Expand All @@ -130,9 +131,11 @@ async function startServer(): Promise<void> {
logger.info(
`🚀 Server ready at ${
process.env.NODE_ENV === "production" ? "https" : "http"
}://localhost:4000/graphql`,
}://localhost:${SERVER_PORT}/graphql`,
);
logger.info(
`🚀 Subscription endpoint ready at ws://localhost:${SERVER_PORT}/graphql`,
);
logger.info(`🚀 Subscription endpoint ready at ws://localhost:4000/graphql`);
}

startServer();
Expand Down

0 comments on commit 9fa6a1c

Please sign in to comment.