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

Currently ignored argv and just uses projectDir. #146

Open
github-actions bot opened this issue Oct 24, 2020 · 0 comments
Open

Currently ignored argv and just uses projectDir. #146

github-actions bot opened this issue Oct 24, 2020 · 0 comments
Labels

Comments

@github-actions
Copy link

Currently ignored argv and just uses projectDir.

// FIXME: Currently ignored argv and just uses projectDir.

import bodyParser from "body-parser";
import { writeFile } from "fs";
import { DEFAULT_LOCAL_2KEYS, DEFAULT_PORT, WINDOWS_SERVER_PID_FILE } from "./util/constants";
import { Arguments } from "yargs";
import { loadProjectConfig } from "@twokeys/core/lib/config";
import { join } from "path";
import getAPI from "./routes/api";
import { ProjectConfig } from "@twokeys/core/lib/interfaces";
import helmet from "helmet";
import mkdirp from "mkdirp";
import startupScripts from "./util/startup";
import { Logger } from "@twokeys/core";

const app = express();
const logger: Logger = new Logger({
	name: "server",
});

interface ServerArgs {
	"pid-file"?: string;
}

const server = async (port: number = DEFAULT_PORT, argv: ServerArgs, projectDir: string, projectConfig: ProjectConfig): Promise<ReturnType<typeof express>> => {

	// Default start scripts
	await startupScripts();

	app.use(bodyParser.json());
	app.use(helmet());
	app.use("/api", await getAPI(projectConfig, projectDir));

	// Error handler
	app.use((err, req, res, next) => {
		logger.err(`An error was encountered on router ${req.originalUrl}`);
		logger.printError(err);
		next(err);
	});

	app.listen(port, () => {
		logger.info("Server now listenning on port " + port);
		logger.debug("PID: " + process.pid);
	});

	// FIXME: Currently ignored argv and just uses projectDir.
	if (Object.prototype.hasOwnProperty.call(argv, "pid-file") && typeof argv["pid-file"] === "string" && argv["pid-file"]) {
		logger.debug(`Writing pid file to ${join(projectDir, DEFAULT_LOCAL_2KEYS, WINDOWS_SERVER_PID_FILE)}...`);
		mkdirp(join(projectDir, DEFAULT_LOCAL_2KEYS));
		writeFile(join(projectDir, DEFAULT_LOCAL_2KEYS, WINDOWS_SERVER_PID_FILE), process.pid.toString(), (err) => {
			if (err) { logger.printError(err); }
			logger.info("PID file Written.");
		});
	}

	return app;

};

/**
 * Eventually default starter for server
 * @param projectDir Absoluter path to project
 */
export async function starter(projectDir: string): Promise<void> {
	logger.info("Starting 2Keys....");
	logger.info("Loading project...");
	// TODO: Version checks to check config version matches this version of 2Keys
	const projectConfig = await loadProjectConfig(projectDir);
	await server(projectConfig.server.port, {
		"pid-file": join(projectDir, DEFAULT_LOCAL_2KEYS, WINDOWS_SERVER_PID_FILE)
	} as Arguments<ServerArgs>, projectDir, projectConfig);
}

export default server;
export { app };
eleted file mode 100644
ndex 748c11b..0000000
++ /dev/null

a1e9c5bf366ca97931ff4616fd19f9fbcd277e1b

@github-actions github-actions bot added the todo label Oct 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants