Skip to content

Commit

Permalink
Dockerize server (#18)
Browse files Browse the repository at this point in the history
* update packages

* add dockerfile and update server bundle

* update tsconfig

* rename parse config

* publish packages

* use console logger in cli
  • Loading branch information
gladwindos committed Mar 13, 2024
1 parent 9315638 commit c92f393
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.git
.gitignore
*.md
dist
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:20-slim AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

FROM base AS build
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run -r build
RUN pnpm deploy --filter=@gateweaver/server --prod /prod/server

FROM base
COPY --from=build /prod/server /prod/server
WORKDIR /prod/server
EXPOSE 8080
CMD [ "pnpm", "start" ]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"description": "",
"scripts": {
"dev": "turbo build:watch & nodemon packages/server/src/server/index.ts",
"dev": "turbo build:watch & nodemon packages/server/src/index.ts",
"build": "turbo build",
"build:watch": "turbo build:watch",
"test": "turbo test",
Expand Down
27 changes: 27 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# @gateweaver/cli

## 0.0.4

### Patch Changes

- Publish packages
- Updated dependencies
- @gateweaver/server@0.0.4
- @gateweaver/utils@0.0.4

## 0.0.3

### Patch Changes

- Update server dist
- Updated dependencies
- @gateweaver/server@0.0.3
- @gateweaver/utils@0.0.3

## 0.0.2

### Patch Changes

- Small patch
- Updated dependencies
- @gateweaver/server@0.0.2
- @gateweaver/utils@0.0.2

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gateweaver/cli",
"version": "0.0.1",
"version": "0.0.4",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
59 changes: 30 additions & 29 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
#! /usr/bin/env node

// eslint-disable-next-line @typescript-eslint/no-var-requires
require("dotenv").config({ path: ".env.gateweaver" });
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { config } from "dotenv";
config({ path: ".env.gateweaver" });
import { program } from "commander";
import { startServer } from "@gateweaver/server";
import { logger } from "@gateweaver/utils";
import { startServer } from "@gateweaver/server/start";
import { generateApiKeyAction } from "./generate-api-key";

const packageJson = JSON.parse(
readFileSync(resolve(__dirname, "../package.json"), "utf8"),
);
const setupCLI = async () => {
const packageJson = await import("../package.json", {
assert: { type: "json" },
});

program
.name("gateweaver")
.description("A CLI tool for managing gateweaver gateways")
.version(packageJson.version);
program
.name("gateweaver")
.description("A CLI tool for managing gateweaver gateways")
.version(packageJson.default.version);

program
.command("generate-api-key")
.description("Generate a new API key and hash")
.action(generateApiKeyAction);
program
.command("generate-api-key")
.description("Generate a new API key and hash")
.action(generateApiKeyAction);

program
.command("start")
.description("Start the gateweaver server")
.action(() => {
try {
startServer();
} catch (error) {
logger.error(error);
process.exit(1);
}
});
program
.command("start")
.description("Start the gateweaver server")
.action(() => {
try {
startServer();
} catch (error) {
console.error(error);
process.exit(1);
}
});

program.parse();
};

program.parse();
setupCLI().catch(console.error);
24 changes: 24 additions & 0 deletions packages/policies/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# @gateweaver/policies

## 0.0.4

### Patch Changes

- Publish packages
- Updated dependencies
- @gateweaver/utils@0.0.4

## 0.0.3

### Patch Changes

- Update server dist
- Updated dependencies
- @gateweaver/utils@0.0.3

## 0.0.2

### Patch Changes

- Small patch
- Updated dependencies
- @gateweaver/utils@0.0.2

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/policies/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gateweaver/policies",
"version": "0.0.1",
"version": "0.0.4",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
27 changes: 27 additions & 0 deletions packages/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# @gateweaver/server

## 0.0.4

### Patch Changes

- Publish packages
- Updated dependencies
- @gateweaver/policies@0.0.4
- @gateweaver/utils@0.0.4

## 0.0.3

### Patch Changes

- Update server dist
- Updated dependencies
- @gateweaver/policies@0.0.3
- @gateweaver/utils@0.0.3

## 0.0.2

### Patch Changes

- Small patch
- Updated dependencies
- @gateweaver/policies@0.0.2
- @gateweaver/utils@0.0.2

## 0.0.1

### Patch Changes
Expand Down
20 changes: 15 additions & 5 deletions packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
{
"name": "@gateweaver/server",
"version": "0.0.1",
"version": "0.0.4",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./start": {
"import": "./dist/start-server.mjs",
"require": "./dist/start-server.js",
"types": "./dist/start-server.d.ts"
}
},
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"build:watch": "pnpm build --watch",
"start": "node ./dist/index.js",
"build": "tsup",
"build:watch": "pnpm build --watch",
"lint": "eslint .",
"clean": "rimraf ./dist",
"test": "jest",
Expand Down
10 changes: 5 additions & 5 deletions packages/server/src/config/parse-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from "fs";
import { parseConfigYaml } from "./parse-config";
import { parseConfig } from "./parse-config";
import { validateConfig } from "./validate-config";

jest.mock("fs");
jest.mock("./validate-config");

describe("parseConfigYaml", () => {
describe("parseConfig", () => {
const mockEnv = { TEST_VAR: "value" };

beforeEach(() => {
Expand All @@ -22,7 +22,7 @@ describe("parseConfigYaml", () => {
(fs.readFileSync as jest.Mock).mockReturnValue("endpoints: value");
(validateConfig as jest.Mock).mockReturnValue(mockConfig);

const result = parseConfigYaml(configPath);
const result = parseConfig(configPath);

expect(fs.existsSync).toHaveBeenCalledWith(fullPath);
expect(fs.readFileSync).toHaveBeenCalledWith(fullPath, "utf8");
Expand All @@ -33,7 +33,7 @@ describe("parseConfigYaml", () => {
it("should throw an error if the config file does not exist", () => {
(fs.existsSync as jest.Mock).mockReturnValue(false);

expect(() => parseConfigYaml("missingConfig")).toThrow(
expect(() => parseConfig("missingConfig")).toThrow(
/not found with .yml or .yaml extension/,
);
});
Expand All @@ -46,7 +46,7 @@ describe("parseConfigYaml", () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue("endpoints: ${TEST_VAR}");

parseConfigYaml(configPath);
parseConfig(configPath);

expect(fs.readFileSync).toHaveBeenCalledWith(fullPath, "utf8");
expect(validateConfig).toHaveBeenCalledWith(mockConfig);
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/config/parse-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import YAML from "yaml";
import { validateConfig } from "./validate-config";
import { Config } from "./config.types";

export const parseConfigYaml = (filePath: string): Config => {
export const parseConfig = (filePath: string): Config => {
const extensions = [".yml", ".yaml"];

let finalPath;
Expand Down
12 changes: 11 additions & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export { startServer } from "./server/start-server";
import { config } from "dotenv";
config({ path: ".env.gateweaver" });
import { logger } from "@gateweaver/utils";
import { startServer } from "./start-server";

try {
startServer();
} catch (error) {
logger.error(error);
process.exit(1);
}
11 changes: 0 additions & 11 deletions packages/server/src/server/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import express from "express";
import helmet from "helmet";
import { errorHandler, logger } from "@gateweaver/utils";
import { parseConfigYaml } from "../config/parse-config";
import { createRouter } from "../router";
import { parseConfig } from "./config/parse-config";
import { createRouter } from "./router";

export const startServer = () => {
const config = parseConfigYaml("gateweaver");
const config = parseConfig("gateweaver");

const PORT = config.port || process.env.PORT || 8080;

Expand Down
7 changes: 7 additions & 0 deletions packages/server/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts", "src/start-server.ts"],
format: ["cjs", "esm"],
dts: true,
});
18 changes: 18 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# @gateweaver/utils

## 0.0.4

### Patch Changes

- Publish packages

## 0.0.3

### Patch Changes

- Update server dist

## 0.0.2

### Patch Changes

- Small patch

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gateweaver/utils",
"version": "0.0.1",
"version": "0.0.4",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
require("dotenv").config({ path: ".env.gateweaver" });
import { config } from "dotenv";
config({ path: ".env.gateweaver" });
import pino from "pino";

const options =
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
Expand Down

0 comments on commit c92f393

Please sign in to comment.