Skip to content

Commit

Permalink
reat: fix paths on prod and dev
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Mar 28, 2024
1 parent 1cad5ac commit 0c17b11
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "next experimental-compile",
"build:image": "docker buildx build -t getashell:testing .",
"clean:containers": "docker rm -f $(docker ps -aq)",
"clean:keys": "ssh-keygen -f ~/.ssh/known_hosts -R [localhost]:2222",
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/render-shell/render-shell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { remove } from "@/server/actions/remove-actions";
import { remove } from "@/server/actions/remove-action";
import { containerData } from "@/server/types/types";
import { Card, Flex, Text, Button } from "@radix-ui/themes";
import { toast } from "react-toastify";
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions src/server/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const env = process.env.NODE_ENV;

export const getConfig = () => {
let migrationDir = "";
let dataDir = "";
let databasePath = "";
let ncHost = "";

if (env == "development") {
migrationDir = "migrations";
dataDir = "data";
databasePath = "data/sqlite.db";
ncHost = "127.0.0.1";
} else if (env == "production") {
migrationDir = "/app/migrations";
dataDir = "/app/data";
databasePath = "/app/data/sqlite.db";
ncHost = "host.docker.internal";
}

return { migrationDir, dataDir, databasePath, ncHost };
};
9 changes: 6 additions & 3 deletions src/server/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import Database from "better-sqlite3";
import * as schema from "./schema";
import * as fs from "fs";
import { migrateDb } from "./migrator";
import { getConfig } from "../config/config";

if (!fs.existsSync("/app/data")) {
fs.mkdirSync("/app/data");
const { databasePath, dataDir } = getConfig();

if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}

console.log("Running database migrations...");
Expand All @@ -17,7 +20,7 @@ migrateDb();

console.log("Migrations finished! Connecting to databse...");

const sqlite = new Database("/app/data/sqlite.db");
const sqlite = new Database(databasePath);

export const db: BetterSQLite3Database<typeof schema> = drizzle(sqlite, {
schema,
Expand Down
7 changes: 5 additions & 2 deletions src/server/db/migrator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import Database from "better-sqlite3";
import { getConfig } from "../config/config";

export const migrateDb = () => {
const betterSqlite = new Database("/app/data/sqlite.db");
const { databasePath, migrationDir } = getConfig();

const betterSqlite = new Database(databasePath);
const db = drizzle(betterSqlite);

migrate(db, { migrationsFolder: "/app/migrations" });
migrate(db, { migrationsFolder: migrationDir });

betterSqlite.close();
};
6 changes: 4 additions & 2 deletions src/server/utils/port-checker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { exec as execCallback } from "child_process";
import * as util from "util";
import { getConfig } from "../config/config";

const exec = util.promisify(execCallback);

export const availablePortChecker = async (port: number) => {
try {
const { ncHost } = getConfig();
const { stdout: tcpStdout, stderr: tcpStderr } = await exec(
`nc -zv host.docker.internal ${port}`,
`nc -zv ${ncHost} ${port}`,
);
const { stdout: udpStdout, stderr: udpStderr } = await exec(
`nc -zuv host.docker.internal ${port}`,
`nc -zuv ${ncHost} ${port}`,
);
return {
success: false,
Expand Down

0 comments on commit 0c17b11

Please sign in to comment.