WIP: build attempted

This commit is contained in:
Suyono 2024-02-22 23:42:04 +11:00
parent a1cba242e9
commit 5c8a3f56dd
5 changed files with 56 additions and 37 deletions

8
Dockerfile Normal file
View File

@ -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

View File

@ -1,9 +1,9 @@
import { promisePool } from "@/backend/db"; import { getPromisePool } from "@/backend/db";
import {RowDataPacket} from "mysql2"; import {RowDataPacket} from "mysql2";
async function query() { async function query() {
try { try {
const [rows, fields] = await promisePool.query<RowDataPacket[]>('select slug from post limit 1;') const [rows, fields] = await getPromisePool().query<RowDataPacket[]>('select slug from post limit 1;')
return(rows[0]['slug'] as string) return(rows[0]['slug'] as string)
} catch (e) { } catch (e) {
console.log(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( return(
<div className={`flex flex-col`}> <div className={`flex flex-col`}>
<p>Env: { process.env.MYSQL_HOST }</p> <p>Env: { process.env.MYSQL_HOST }</p>
<p>Result: { await query() }</p> <p>Result: { await query() }</p>
<p>Flag: { flag }</p>
</div> </div>
) )
} }

View File

@ -1,20 +1,13 @@
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 fs from 'fs';
import * as appEnv from "./env"; import * as appEnv from "./env";
if (typeof process.env.MYSQL_SSL_CA === 'undefined') { let pool: Pool | undefined;
throw new Error("missing MYSQL_SSL_CA") let promisePool: pPool | undefined;
}
if (typeof process.env.MYSQL_SSL_KEY === 'undefined') { export function getPool(): Pool {
throw new Error("missing MYSQL_SSL_KEY") const access: PoolOptions = {
}
if (typeof process.env.MYSQL_SSL_CERT === 'undefined') {
throw new Error("missing MYSQL_SSL_CERT")
}
const access: PoolOptions = {
host: appEnv.getMysqlHost(), host: appEnv.getMysqlHost(),
port: appEnv.getMysqlPort(), port: appEnv.getMysqlPort(),
user: appEnv.getMysqlUser(), user: appEnv.getMysqlUser(),
@ -32,8 +25,18 @@ const access: PoolOptions = {
key: fs.readFileSync(appEnv.getMysqlSslKeyFile()), key: fs.readFileSync(appEnv.getMysqlSslKeyFile()),
cert: fs.readFileSync(appEnv.getMysqlSslCertFile()) cert: fs.readFileSync(appEnv.getMysqlSslCertFile())
} }
}
if (typeof pool === 'undefined') {
pool = mysql.createPool(access)
}
return pool
} }
export const pool = mysql.createPool(access) export function getPromisePool(): pPool {
if (typeof promisePool === 'undefined') {
export const promisePool = pool.promise() promisePool = getPool().promise()
}
return promisePool
}

View File

@ -1,9 +1,9 @@
import { RowDataPacket } from "mysql2"; import { RowDataPacket } from "mysql2";
import { promisePool } from "@/backend/db"; import { getPromisePool } from "@/backend/db";
export async function getPost(slug: string): Promise<string> { export async function getPost(slug: string): Promise<string> {
try { try {
const [rows, fields] = await promisePool.query<RowDataPacket[]>( const [rows, fields] = await getPromisePool().query<RowDataPacket[]>(
'select content from post where slug = ?', [slug]) 'select content from post where slug = ?', [slug])
return rows[0]['content'] return rows[0]['content']
} catch (e) { } catch (e) {

View File

@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
output: "standalone",
webpack: (config) => { webpack: (config) => {
config.externals = [...config.externals, "jsdom"]; config.externals = [...config.externals, "jsdom"];
return config; return config;