Skip to content

Commit

Permalink
server, env
Browse files Browse the repository at this point in the history
  • Loading branch information
Pridestalkerr committed Dec 27, 2023
1 parent 42bef38 commit 71715c5
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 12 deletions.
5 changes: 5 additions & 0 deletions apps/server/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"watch": ["../../packages", "src"],
"ext": "ts,json",
"exec": "node --loader esbuild-register/loader -r esbuild-register ./src/index.ts"
}
39 changes: 39 additions & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@edge/server",
"version": "0.0.0",
"type": "module",
"main": "./src/index.mjs",
"types": "./src/index.mjs",
"scripts": {
"build": "tsc",
"dev": "nodemon",
"typecheck": "tsc --noEmit",
"clean": "rm -rf .turbo && rm -rf node_modules",
"lint": "eslint . --ext .ts,.tsx,.mjs",
"lint:fix": "pnpm lint --fix",
"format": "prettier --write '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern",
"format:check": "prettier --check '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern"
},
"devDependencies": {
"@edge/eslint-config": "^0.0.0",
"@edge/prettier-config": "^0.0.0",
"@edge/tsconfig": "^0.0.0",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"esbuild-register": "^3.5.0",
"nodemon": "^3.0.2",
"typescript": "^5.1.6"
},
"eslintConfig": {
"root": true,
"extends": [
"@edge/eslint-config/base"
]
},
"prettier": "@edge/prettier-config/base",
"dependencies": {
"@edge/env": "^0.0.0",
"cors": "^2.8.5",
"express": "^4.18.2"
}
}
26 changes: 26 additions & 0 deletions apps/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from "express";
import { createServer } from "http";
import cors from "cors";
import { env } from "@edge/env";

const main = async () => {
console.log(env, "XD");
const app = express();
app.use(
cors({
// The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'
origin: [env.CLIENT_HOST, /http:\/\/localhost:\d{4}\/?.*$/],
credentials: true,
}),
);

const server = createServer(app);
server.listen(env.API_PORT, () => {
console.log(`Listening on port ${env.API_PORT}`);
});
};

main().catch((err) => {
console.error(err);
process.exit(1);
});
4 changes: 4 additions & 0 deletions apps/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@edge/tsconfig/base.json",
"include": ["src"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import "dotenv/config";
export const env = createEnv({
server: {
NODE_ENV: z.enum(["development", "production"]),
API_PORT: z.coerce.number(),
DATABASE_URI: z.string().url(),
CLIENT_HOST: z.string().url(), // useful for CORS
},
clientPrefix: "NEXT_PUBLIC_",
client: {
NEXT_PUBLIC_API_HOST: z.string().url(),
NEXT_PUBLIC_API_ENDPOINT: z.string(),
},
runtimeEnv: {
...process.env,
NEXT_PUBLIC_API_HOST: process.env.API_HOST,
NEXT_PUBLIC_API_ENDPOINT: process.env.API_ENDPOINT,
},
});
4 changes: 3 additions & 1 deletion apps/web/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import "@edge/env";
//TODO: wait for next.config.ts support...
// import "@edge/env"
import "@edge/env/src/index.mjs"

/** @type {import('next').NextConfig} */
const nextConfig = {}
Expand Down
4 changes: 2 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"clean": "rm -rf .turbo && rm -rf node_modules",
"lint": "next lint",
"lint:fix": "next lint --fix",
"format": "prettier --write '*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern",
"format:check": "prettier --check '*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern"
"format": "prettier --write '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern",
"format:check": "prettier --check '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern"
},
"dependencies": {
"@edge/env": "^0.0.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "*.mjs"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "*.mjs", "*.mts", "next.config.mjs"],
"exclude": ["node_modules"]
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
},
"packageManager": "[email protected]",
"scripts": {
"build": "turbo build",
"dev": "turbo dev",
"build": "cross-env NODE_ENV=production dotenv -- turbo build",
"dev": "cross-env NODE_ENV=development dotenv -- turbo dev --parallel",
"typecheck": "turbo typecheck",
"clean": "turbo run clean && rm -rf node_modules",
"lint": "turbo lint && manypkg check",
Expand All @@ -25,6 +25,8 @@
"dependencies": {
"@manypkg/cli": "^0.21.1",
"@types/node": "^20.10.3",
"cross-env": "^7.0.3",
"dotenv-cli": "^7.3.0",
"prettier": "^3.1.1",
"turbo": "^1.10.16",
"typescript": "^5.3.2"
Expand Down
12 changes: 7 additions & 5 deletions packages/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
"name": "@edge/env",
"version": "0.0.0",
"type": "module",
"main": "./src/index.mjs",
"types": "./src/index.mjs",
"main": "./src/index.ts",
"types": "./src/index.ts",
"files": [
"src"
],
"scripts": {
"typecheck": "tsc --noEmit",
"clean": "rm -rf .turbo && rm -rf node_modules",
"lint": "eslint . --ext .ts,.tsx,.mjs",
"lint:fix": "pnpm lint --fix",
"format": "prettier --write '*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern",
"format:check": "prettier --check '*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern"
"format": "prettier --write '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern",
"format:check": "prettier --check '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-unknown --no-error-on-unmatched-pattern"
},
"dependencies": {
"@t3-oss/env-core": "^0.6.1",
"dotenv": "^16.3.1",
"zod": "^3.21.4"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/env/src/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// this only exists for build time validation, just copy paste your OG file here
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";
import "dotenv/config";
Expand Down
21 changes: 21 additions & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createEnv } from "@t3-oss/env-core";
import { z } from "zod";

export const env = createEnv({
server: {
NODE_ENV: z.enum(["development", "production"]),
API_PORT: z.coerce.number(),
DATABASE_URI: z.string().url(),
CLIENT_HOST: z.string().url(), // useful for CORS
},
clientPrefix: "NEXT_PUBLIC_",
client: {
NEXT_PUBLIC_API_HOST: z.string().url(),
NEXT_PUBLIC_API_ENDPOINT: z.string(),
},
runtimeEnv: {
...process.env,
NEXT_PUBLIC_API_HOST: process.env.API_HOST,
NEXT_PUBLIC_API_ENDPOINT: process.env.API_ENDPOINT,
},
});
2 changes: 1 addition & 1 deletion packages/env/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "@edge/tsconfig/base.json",
"include": ["src", "src/**/*.mjs"]
"include": ["src"]
}
1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages:
- apps/web
- apps/server
- packages/config/eslint
- packages/config/tsconfig
- packages/config/prettier
Expand Down

0 comments on commit 71715c5

Please sign in to comment.