Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
use new default port setting to construct external page urls (#52 and #…
Browse files Browse the repository at this point in the history
…31)

- remove old defaultAppPort constant from preview.ts
- change localAppUrl to just http://localhost without port
- allow relative path page url requests and map them to external Uris with host and port rewriting
  • Loading branch information
RandomFractals committed Jun 1, 2023
1 parent ebb86ff commit 409ed47
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
12 changes: 3 additions & 9 deletions src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ import {
startServer
} from './server';


/**
* Default Evidence app port.
*/
export const defaultAppPort: number = 3000;

/**
* Local Evidence app url.
*/
export const localAppUrl = `http://localhost:${defaultAppPort}`;
export const localAppUrl = `http://localhost`;

/**
* Opens Evidence app or markdown page preview
Expand All @@ -36,7 +30,7 @@ export const localAppUrl = `http://localhost:${defaultAppPort}`;
*/
export async function preview(uri?: Uri) {
// default page url
let pageUrl: string = localAppUrl;
let pageUrl: string = '/';

// create web page url from page Uri
if (uri && (uri.scheme === 'http' || uri.scheme === 'https')) {
Expand All @@ -50,7 +44,7 @@ export async function preview(uri?: Uri) {
let pagePath: string = uri.fsPath.replace(workspaceFolderPath, '')
.split('\\').join('/').replace('/pages/', '')
.replace('index.md', '').replace('.md', '');
pageUrl = `${localAppUrl}/${pagePath}`;
pageUrl = `/${pagePath}`;
}

// create external app page Uri from page url
Expand Down
24 changes: 16 additions & 8 deletions src/commands/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { commands, env, Uri } from 'vscode';

import { Commands } from './commands';
import { executeCommand } from './build';
import { Settings, getConfig } from '../config';
import { getOutputChannel } from '../output';
import { closeTerminal, sendCommand } from '../terminal';
import { defaultAppPort, localAppUrl, preview } from './preview';
import { localAppUrl, preview } from './preview';
import { getNodeVersion, isSupportedNodeVersion } from '../node';
import { timeout } from '../utils/timer';
import { statusBar } from '../statusBar';
import { tryPort } from '../utils/httpUtils';

const localhost = 'localhost';
let _running: boolean = false;
let _activePort: number = defaultAppPort;
let _activePort: number = <number>getConfig(Settings.DefaultPort);

/**
* Creates Evidence app page Uri from the provided pageUrl,
Expand All @@ -26,22 +27,29 @@ let _activePort: number = defaultAppPort;
* when running in GitHub Codespaces.
*/
export async function getAppPageUri(pageUrl?: string): Promise<Uri> {
const defaultPort = <number>getConfig(Settings.DefaultPort);
const serverUrl = `${localAppUrl}:${defaultPort}`;
if (pageUrl === undefined) {
pageUrl = localAppUrl;
pageUrl = serverUrl;
}
else if (pageUrl.startsWith('/')) {
// construct page url for page path wihtout host and port
pageUrl = `${localAppUrl}:${defaultPort}${pageUrl}`;
}

// get external web page url
let pageUri: Uri = await env.asExternalUri(Uri.parse(pageUrl));

// update active server port number
if (!_running) { //pageUri.authority.startsWith(localhost) && !isServerRunning()) {
// get the next available localhost port number
_activePort = await tryPort(defaultAppPort);
_activePort = await tryPort(defaultPort);
}

if (_activePort !== defaultAppPort) {
if (_activePort !== defaultPort) {
// rewrite requested app page url to use the new active localhost server port
pageUri = Uri.parse(pageUri.toString(true) // skip encoding
.replace(`/:${defaultAppPort}/`, `/:${_activePort}/`));
.replace(`/:${defaultPort}/`, `/:${_activePort}/`));
}

const outputChannel = getOutputChannel();
Expand All @@ -57,7 +65,7 @@ export async function getAppPageUri(pageUrl?: string): Promise<Uri> {
*/
export async function startServer(pageUri?: Uri) {
if (!pageUri) {
pageUri = await getAppPageUri(localAppUrl);
pageUri = await getAppPageUri('/');
}

// check supported node version prior to server start
Expand Down Expand Up @@ -133,6 +141,6 @@ export function stopServer() {

// reset server state and status display
_running = false;
_activePort = defaultAppPort;
_activePort = <number>getConfig(Settings.DefaultPort);
statusBar.showStart();
}
5 changes: 2 additions & 3 deletions src/commands/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {

import { Commands } from './commands';
import { getWorkspaceFolder } from '../config';
import { localAppUrl } from './preview';

import {
getAppPageUri,
Expand All @@ -26,13 +25,13 @@ const settingsFilePath = '.evidence/template/evidence.settings.json';
*
* @see https://docs.evidence.dev/core-concepts/data-sources/
*/
const settingsPageUrl = `${localAppUrl}/settings`;
const settingsPagePath = '/settings';

/**
* Opens Evidence app settings page in the built-in vscode simple browser webview.
*/
export async function viewAppSettings() {
const settingsPageUri: Uri = await getAppPageUri(settingsPageUrl);
const settingsPageUri: Uri = await getAppPageUri(settingsPagePath);
if (!isServerRunning()) {
startServer(settingsPageUri);
}
Expand Down

0 comments on commit 409ed47

Please sign in to comment.