Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move port validation and waiting logic into proxy server #182

Merged
merged 2 commits into from Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions src/cli/commands/start.ts
Expand Up @@ -3,15 +3,7 @@ import fs from "fs";
import path from "path";
import { DEFAULT_CONFIG } from "../../config";
import builder from "../../core/builder";
import {
createStartupScriptCommand,
isAcceptingTcpConnections,
isHttpUrl,
logger,
parseUrl,
readWorkflowFile,
validateDevServerConfig,
} from "../../core";
import { createStartupScriptCommand, isAcceptingTcpConnections, isHttpUrl, logger, parseUrl, readWorkflowFile } from "../../core";

export async function start(startContext: string, options: SWACLIConfig) {
// WARNING: code below doesn't have access to SWA CLI env vars which are defined later below
Expand All @@ -22,7 +14,7 @@ export async function start(startContext: string, options: SWACLIConfig) {
let startupCommand: string | undefined | null = undefined;

if (isHttpUrl(startContext)) {
useAppDevServer = await validateDevServerConfig(startContext);
useAppDevServer = startContext;
options.outputLocation = useAppDevServer;
} else {
// make sure the CLI default port is available before proceeding.
Expand All @@ -45,7 +37,7 @@ export async function start(startContext: string, options: SWACLIConfig) {

if (options.apiLocation) {
if (isHttpUrl(options.apiLocation)) {
useApiDevServer = await validateDevServerConfig(options.apiLocation);
useApiDevServer = options.apiLocation;
options.apiLocation = useApiDevServer;
}
// make sure api folder exists
Expand Down
11 changes: 10 additions & 1 deletion src/proxy/server.ts
Expand Up @@ -10,7 +10,7 @@ import path from "path";
import serveStatic from "serve-static";
import { processAuth } from "../auth/";
import { DEFAULT_CONFIG } from "../config";
import { address, decodeCookie, findSWAConfigFile, isHttpUrl, logger, registerProcessExit, validateCookie } from "../core";
import { address, decodeCookie, findSWAConfigFile, isHttpUrl, logger, registerProcessExit, validateCookie, validateDevServerConfig } from "../core";
import { applyRules } from "./routes-engine/index";

const SWA_WORKFLOW_CONFIG_FILE = process.env.SWA_WORKFLOW_CONFIG_FILE as string;
Expand Down Expand Up @@ -323,6 +323,15 @@ const requestHandler = (userConfig: SWAConfigFile | null) =>
}
return http.createServer(requestHandler(userConfig));
};

if (isStaticDevServer) {
await validateDevServerConfig(SWA_CLI_OUTPUT_LOCATION);
}
const isApi = Boolean(SWA_CLI_API_LOCATION && SWA_CLI_API_URI);
if (isApi) {
Comment on lines +330 to +331
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a global flag called isApiDevServer:

Suggested change
const isApi = Boolean(SWA_CLI_API_LOCATION && SWA_CLI_API_URI);
if (isApi) {
if (isApiDevServer) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to wait whether it’s using dev server or if someone passed a path to the API folder, so that the output from func is printed before the proxy starts.

await validateDevServerConfig(SWA_CLI_API_URI);
}

const server = createServer();
server.listen(SWA_CLI_PORT, SWA_CLI_HOST, onServerStart);
server.listen(SWA_CLI_PORT, localIpAdress);
Expand Down