Skip to content

Commit

Permalink
기존 구현 교체
Browse files Browse the repository at this point in the history
  • Loading branch information
if1live committed Sep 30, 2023
1 parent 8fd1980 commit 9053f24
Show file tree
Hide file tree
Showing 25 changed files with 4,291 additions and 2,877 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ indent_size = 2
[*.{html,htm}]
indent_style = space
indent_size = 2

[*.sql]
indent_style = space
indent_size = 4
23 changes: 18 additions & 5 deletions .env.localhost
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
MYSQL_DATABASE_URL="mysql://root:my-secret-pw@localhost/ayane_dev"
POSTGRES_DATABASE_URL="postgres://postgres:postgres@localhost/ayane_dev"
REDIS_URL="redis://localhost:6379/0"
NODE_ENV=development
REDIS_URL="redis://127.0.0.1:6379/0"

UPSTASH_REDIS_REST_URL=""
UPSTASH_REDIS_REST_TOKEN=""
AYANE_PLANETSCALE_LABEL="planetscale"
AYANE_PLANETSCALE_TYPE="mysql"
AYANE_PLANETSCALE_ARG_1="mysql://root:my-secret-pw@localhost/ayane_dev"

AYANE_SUPABASE_LABEL="supabase"
AYANE_SUPABASE_TYPE="postgres"
AYANE_SUPABASE_ARG_1="postgres://postgres:postgres@localhost/ayane_dev"

AYANE_REDISLAB_LABEL="redislab"
AYANE_REDISLAB_TYPE="redis_native"
AYANE_REDISLAB_ARG_1="redis://localhost:6379/0"

AYANE_UPSTASH_REDIS_LABEL="upstash_redis"
AYANE_UPSTASH_REDIS_TYPE="upstash_redis"
AYANE_UPSTASH_REDIS_ARG_1="TODO:UPSTASH_REDIS_REST_URL"
AYANE_UPSTASH_REDIS_ARG_2="TODO:UPSTASH_REDIS_REST_TOKEN"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,4 @@ dist
.esbuild
.env.development
output
artifact
14 changes: 13 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@ services:
mysql:
image: mysql:8.0-debian
environment:
MYSQL_DATABASE: ayane_dev
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_DATABASE: ayane_dev
ports:
- 3306:3306

redis:
image: redis:7.0-alpine
ports:
- 6379:6379

adminer:
image: adminer:latest
ports:
- 8080:8080

redis-commander:
image: rediscommander/redis-commander:latest
environment:
- REDIS_HOSTS=local:redis:6379
ports:
- "8081:8081"
63 changes: 29 additions & 34 deletions scripts/build.ts → etc/build_artifact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import esbuild from "esbuild";
import fs from "fs/promises";
import path from "path";
import fs from "node:fs/promises";

const banner_js_esm = `
import { createRequire as topLevelCreateRequire } from 'module';
Expand All @@ -9,32 +8,34 @@ const __dirname = '.';
`.trim();

// https://github.com/evanw/esbuild/issues/946#issuecomment-911869872
const common: esbuild.BuildOptions = {
const opts: esbuild.BuildOptions = {
// identifiers까지 minifiy 적용하면 소스맵에 들어가는 이름도 뭉개진다
// 적용할 경우 "at Ap (/var/task/src/apis.ts:71:9)" 같이 나온다.
// 적용할 경우 'at Ap (/var/task/src/apis.ts:71:9)' 같이 나온다.
// 타입스크립트 파일명과 라인은 제대로 나와서 못 쓸 물건은 아니다 이름까지 제대로 나오면 좋겠다
minifyIdentifiers: false,
minifySyntax: true,
minifyWhitespace: false, //true,
// minifyIdentifiers: false,
// minifySyntax: true,
// 번들 자체를 뜯어보기 편해서. 프로덕션에서는 true로 써야한다.
// minifyWhitespace: false,

// for production
minify: true,

bundle: true,
sourcemap: true,
treeShaking: true,
platform: "node",
external: [],
target: "node18",
external: ["aws-sdk", "pg-native"],
format: "esm",
mainFields: ["module", "main"],
banner: {
js: banner_js_esm,
},
};

async function writeEsmPackageJson(dirname: string) {
const packageJson = {
type: "module",
};
const file = path.resolve(dirname, "package.json");
const data = JSON.stringify(packageJson, null, 2);
await fs.writeFile(file, data);
function readableSize(val: number) {
const mb = val / 1024 / 1024;
return mb.toPrecision(2);
}

async function mybuild(opts: esbuild.BuildOptions) {
Expand All @@ -43,11 +44,6 @@ async function mybuild(opts: esbuild.BuildOptions) {
const fp_bundle = opts.outfile!;
const fp_sourcemap = fp_bundle + ".map";

function readableSize(val: number) {
const mb = val / 1024 / 1024;
return mb.toPrecision(2);
}

if (true) {
const stat_bundle = await fs.stat(fp_bundle);
console.log(`${fp_bundle}\t${readableSize(stat_bundle.size)}MB`);
Expand All @@ -58,25 +54,24 @@ async function mybuild(opts: esbuild.BuildOptions) {
console.log(`${fp_sourcemap}\t${readableSize(stat_sourcemap.size)}MB`);
}

// out.mjs 3.4mb
// out.mjs.map 4.0mb
// console.log({
// result,
// });
return result;
}

const fn_main = async () => {
const fp_entrypoint = "./src/handlers.js";
const fp_outfile = "./output/handlers.js";
const build = async (
fp_entrypoint: string,
filename: string,
opts: esbuild.BuildOptions,
) => {
const fp_outfile =
opts.format === "esm"
? `./artifact/${filename.replace(".js", ".mjs")}`
: `./artifact/${filename}`;

await mybuild({
...common,
...opts,
entryPoints: [fp_entrypoint],
outfile: fp_outfile,
});
};

async function main() {
await Promise.allSettled([fn_main()]);
await writeEsmPackageJson("./output");
}
await main();
await Promise.allSettled([build("./src/handlers.ts", "handlers.js", opts)]);
10 changes: 10 additions & 0 deletions infra/schema_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE `ayane_kernel` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`value` TEXT NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updated_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),

UNIQUE INDEX `ayane_kernel_name_key`(`name`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
23 changes: 23 additions & 0 deletions infra/schema_pg.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- https://x-team.com/blog/automatic-timestamps-with-postgresql/
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TABLE "ayane_kernel" (
"id" SERIAL NOT NULL PRIMARY KEY,
"name" VARCHAR NOT NULL,
"value" TEXT NOT NULL,
"created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW(),
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON ayane_kernel
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE UNIQUE INDEX "ayane_kernel_name_key" ON "ayane_kernel"("name");
71 changes: 38 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,53 @@
"version": "1.0.0",
"description": "",
"license": "MIT",
"sideEffects": false,
"type": "module",
"main": "serverless.ts",
"module": "dist/src/index.js",
"types": "./dist/src/index.d.ts",
"files": [
"dist",
"!dist/test"
],
"scripts": {
"dev": "ts-node-esm ./src/dev.ts",
"dev": "node --watch --loader=tsx ./src/dev.ts",
"build": "tsc",
"check:watch": "tsc --watch",
"clean": "rimraf dist output .serverless",
"test": "mocha",
"package": "ts-node-esm ./scripts/build.ts"
"clean": "rimraf dist artifact .turbo .serverless",
"test": "vitest",
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"circularDepCheck": "madge --circular src",
"bundle": "tsx etc/build_artifact.ts"
},
"dependencies": {
"@tinyhttp/dotenv": "^2.0.5",
"@upstash/redis": "^1.19.1",
"ioredis": "^5.2.4",
"itty-router": "^3.0.11",
"mysql2": "^3.0.1",
"pg": "^8.8.0"
"@upstash/redis": "^1.22.1",
"dotenv": "^16.3.1",
"hono": "^3.7.3",
"ioredis": "^5.3.2",
"liquidjs": "^10.9.2",
"mysql2": "^3.6.1",
"pg": "^8.11.3",
"serverless-standalone": "^0.0.7"
},
"devDependencies": {
"@swc/core": "^1.3.26",
"@swc/helpers": "^0.4.14",
"@types/assert": "^1.5.6",
"@types/aws-lambda": "^8.10.71",
"@types/ioredis-mock": "^8.2.1",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/pg": "^8.6.6",
"assert": "^2.0.0",
"esbuild": "^0.14.11",
"expect": "^29.3.1",
"ioredis-mock": "^8.2.2",
"mocha": "^10.2.0",
"regenerator-runtime": "^0.13.11",
"rimraf": "^4.0.4",
"serverless": "^3.0.0",
"serverless-offline": "^12.0.4",
"serverless-scriptable-plugin": "^1.2.2",
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.3"
"@types/aws-lambda": "^8.10.122",
"@types/ioredis-mock": "^8.2.3",
"@types/node": "^18.18.1",
"@types/pg": "^8.10.3",
"cross-env": "^7.0.3",
"esbuild": "^0.14.54",
"ioredis-mock": "^8.9.0",
"madge": "^6.1.0",
"prettier": "^3.0.3",
"rimraf": "^4.4.1",
"serverless": "^3.35.2",
"serverless-scriptable-plugin": "^1.3.1",
"tsx": "^3.13.0",
"typescript": "^5.2.2",
"vitest": "^0.33.0"
},
"engines": {
"node": ">=18.0.0"
}
},
"packageManager": "pnpm@8.8.0"
}
Loading

0 comments on commit 9053f24

Please sign in to comment.