diff --git a/bun.lockb b/bun.lockb index 7af1cbc..08f5dea 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/eslint.config.js b/eslint.config.js index 1190c20..277aeba 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,15 +1,18 @@ import eslint from '@eslint/js'; import tseslint from 'typescript-eslint'; -export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.strict, { - rules: { - '@typescript-eslint/ban-ts-comment': [ - 'error', - { - 'ts-expect-error': false, - 'ts-nocheck': false, - 'ts-check': true, - }, - ], - }, -}); +export default tseslint.config( + eslint.configs.recommended, + ...tseslint.configs.strict, + { + rules: { + '@typescript-eslint/ban-ts-comment': [ + 'error', { + 'ts-expect-error': false, + 'ts-nocheck': false, + 'ts-check': true, + }, + ], + }, + } +); diff --git a/index.ts b/index.ts index 3f2f3f8..63a193c 100644 --- a/index.ts +++ b/index.ts @@ -1 +1,62 @@ -console.log('Hello via Bun!'); +import { join } from 'path'; +import { env } from 'bun'; +import express, { json, urlencoded } from 'express'; +import chalk from 'chalk' +import compression from 'compression'; +import cookieParser from 'cookie-parser'; +import debug from 'debug'; +import errorhandler from 'errorhandler'; +import helmet from 'helmet'; +import morgan from 'morgan'; +import serveFavicon from 'serve-favicon'; +import serveStatic from 'serve-static'; + +import { indexRouter } from './routes/index.ts'; + +const BASE_DIR = join(import.meta.dir, 'public'); +const PORT = env.PORT || 3000; + +const debugLogger = debug('app:server'); +const isDev = env.NODE_ENV === 'development'; + +const app = express(); +app.enable('strict routing'); + +debugLogger('Booting server.'); + +app.use( + helmet({ + contentSecurityPolicy: false, + }) +); + +app.use(morgan('combined')); +app.use(json()); +app.use(urlencoded({ extended: false })); + +app.use(cookieParser()); + +app.use(compression()); + +if (isDev) app.use(errorhandler()); + +app.use(serveFavicon(join(BASE_DIR, 'resources/images/icons/favicon.webp'), { maxAge: 86400 })); +app.use(serveStatic(BASE_DIR)); + +app.use('/', indexRouter); + +app.use(async (req, res, next) => { + res.status(404).sendFile(join(BASE_DIR, '404.html')); + next(); +}); + +app.use(async (err, req, res, next) => { + console.error(err.stack); + res.status(500).send('500 Internal Server Error'); + next(); +}); + +app.listen(PORT, async () => { + debugLogger('Server started.'); + console.log(chalk.green(`Server is running on port ${PORT}`)); +}); diff --git a/package.json b/package.json index 2088d2b..6c29a3b 100644 --- a/package.json +++ b/package.json @@ -4,17 +4,32 @@ "module": "index.ts", "type": "module", "scripts": { - "start": "bun run ./index.ts", + "start": "NODE_ENV=production bun run ./index.ts", + "dev": "NODE_ENV=development DEBUG=app:* bun --watch run ./index.ts", "lint": "eslint ./src/", "lint:fix": "eslint --fix ./src/", "build": "bun run lint; rm -rf ./public/resources/scripts/ ./public/assets.json; tsc; javascript-obfuscator ./public/resources/scripts/ --output ./public/resources/scripts/ --options-preset high-obfuscation; bun run ./build.ts" }, "dependencies": { - "@types/bun": "latest" + "chalk": "^5.3.0", + "compression": "^1.7.4", + "cookie-parser": "^1.4.6", + "debug": "^4.3.6", + "errorhandler": "^1.5.1", + "express": "^4.19.2", + "helmet": "^7.1.0", + "morgan": "^1.10.0", + "serve-favicon": "^2.5.0" }, "devDependencies": { "@tsconfig/bun": "^1.0.7", "@tsconfig/strictest": "^2.0.5", + "@types/bun": "^1.1.8", + "@types/compression": "^1.7.5", + "@types/debug": "^4.1.12", + "@types/errorhandler": "^1.5.3", + "@types/morgan": "^1.9.9", + "@types/serve-favicon": "^2.5.7", "eslint": "^9.9.1", "javascript-obfuscator": "^4.1.1", "typescript-eslint": "^8.3.0" diff --git a/routes/index.ts b/routes/index.ts new file mode 100644 index 0000000..b8fedc7 --- /dev/null +++ b/routes/index.ts @@ -0,0 +1,8 @@ +import { Router } from 'express'; +const indexRouter = Router(); + +indexRouter.get('/', async (req, res) => { + // router for future use +}); + +export { indexRouter };