diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7259361 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:lts-alpine as builder + +USER 1000:1000 +ADD --chown=1000:1000 . /home/node/nextts +WORKDIR /home/node/nextts +RUN wget -qO- https://get.pnpm.io/install.sh | ENV="/home/node/.shrc" SHELL="$(which sh)" sh - +ENV PATH=/home/node/.local/share/pnpm:$PATH +RUN pnpm install && pnpm run build diff --git a/app/dbcheck/page.tsx b/app/dbcheck/page.tsx index 4edc830..775ebcb 100644 --- a/app/dbcheck/page.tsx +++ b/app/dbcheck/page.tsx @@ -1,9 +1,9 @@ -import { promisePool } from "@/backend/db"; +import { getPromisePool } from "@/backend/db"; import {RowDataPacket} from "mysql2"; async function query() { try { - const [rows, fields] = await promisePool.query('select slug from post limit 1;') + const [rows, fields] = await getPromisePool().query('select slug from post limit 1;') return(rows[0]['slug'] as string) } catch (e) { console.log(e) @@ -11,11 +11,18 @@ async function query() { } } -export default async function DbCheck() { +export default async function DbCheck({ searchParams }: { searchParams: { [key: string]: string | string[] | undefined }}) { + let flag = "empty"; + + if (typeof searchParams["flag"] === 'string') { + flag = searchParams["flag"] + } + return(

Env: { process.env.MYSQL_HOST }

Result: { await query() }

+

Flag: { flag }

) } \ No newline at end of file diff --git a/backend/db.ts b/backend/db.ts index c1255d4..c11642b 100644 --- a/backend/db.ts +++ b/backend/db.ts @@ -1,39 +1,42 @@ -import mysql, { PoolOptions } from "mysql2"; +import mysql, { PoolOptions, Pool } from "mysql2"; +import { Pool as pPool } from "mysql2/promise" import * as fs from 'fs'; import * as appEnv from "./env"; -if (typeof process.env.MYSQL_SSL_CA === 'undefined') { - throw new Error("missing MYSQL_SSL_CA") -} +let pool: Pool | undefined; +let promisePool: pPool | undefined; -if (typeof process.env.MYSQL_SSL_KEY === 'undefined') { - throw new Error("missing MYSQL_SSL_KEY") -} - -if (typeof process.env.MYSQL_SSL_CERT === 'undefined') { - throw new Error("missing MYSQL_SSL_CERT") -} - -const access: PoolOptions = { - host: appEnv.getMysqlHost(), - port: appEnv.getMysqlPort(), - user: appEnv.getMysqlUser(), - password: appEnv.getMysqlPassword(), - database: appEnv.getMysqlDatabase(), - waitForConnections: true, - connectionLimit: 10, - maxIdle: 10, - idleTimeout: 60000, - queueLimit: 0, - enableKeepAlive: true, - keepAliveInitialDelay: 0, - ssl: { - ca: fs.readFileSync(appEnv.getMysqlSslCaFile()), - key: fs.readFileSync(appEnv.getMysqlSslKeyFile()), - cert: fs.readFileSync(appEnv.getMysqlSslCertFile()) +export function getPool(): Pool { + const access: PoolOptions = { + host: appEnv.getMysqlHost(), + port: appEnv.getMysqlPort(), + user: appEnv.getMysqlUser(), + password: appEnv.getMysqlPassword(), + database: appEnv.getMysqlDatabase(), + waitForConnections: true, + connectionLimit: 10, + maxIdle: 10, + idleTimeout: 60000, + queueLimit: 0, + enableKeepAlive: true, + keepAliveInitialDelay: 0, + ssl: { + ca: fs.readFileSync(appEnv.getMysqlSslCaFile()), + key: fs.readFileSync(appEnv.getMysqlSslKeyFile()), + cert: fs.readFileSync(appEnv.getMysqlSslCertFile()) + } } + + if (typeof pool === 'undefined') { + pool = mysql.createPool(access) + } + + return pool } -export const pool = mysql.createPool(access) - -export const promisePool = pool.promise() \ No newline at end of file +export function getPromisePool(): pPool { + if (typeof promisePool === 'undefined') { + promisePool = getPool().promise() + } + return promisePool +} diff --git a/backend/post.ts b/backend/post.ts index 91decd3..e66a474 100644 --- a/backend/post.ts +++ b/backend/post.ts @@ -1,9 +1,9 @@ import { RowDataPacket } from "mysql2"; -import { promisePool } from "@/backend/db"; +import { getPromisePool } from "@/backend/db"; export async function getPost(slug: string): Promise { try { - const [rows, fields] = await promisePool.query( + const [rows, fields] = await getPromisePool().query( 'select content from post where slug = ?', [slug]) return rows[0]['content'] } catch (e) { diff --git a/next.config.js b/next.config.js index 1f1cac0..21db591 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { + output: "standalone", webpack: (config) => { config.externals = [...config.externals, "jsdom"]; return config;