Skip to content

Commit

Permalink
fix: move port validation and waiting logic into proxy server (#182)
Browse files Browse the repository at this point in the history
* allow startup command to work with wait for port
  • Loading branch information
anthonychu committed Apr 13, 2021
1 parent 39fbb9c commit 4674b70
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
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) {
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

0 comments on commit 4674b70

Please sign in to comment.