diff --git a/.gitignore b/.gitignore index 27022db..8abc793 100644 --- a/.gitignore +++ b/.gitignore @@ -1,27 +1,134 @@ -# dependencies +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore +# Logs +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data +pids +_.pid +_.seed +\*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +\*.lcov + +# nyc test coverage +.nyc_output + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories node_modules/ -# bun -bun.lockb +# Optional eslint cache +.eslintcache -# environments -*.env -*.env.* -!.env.example +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ -# logs -*.log +# Optional REPL history -# system files -.DS_Store -Thumbs.db +.node_repl_history -# builds -dist/ -build/ +# Output of 'npm pack' -# ides -.idea/ -.vscode/ -*.sublime-workspace -*.sublime-project +\*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.cache +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +.cache/ + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp +.cache + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.\* + +.DS_Store \ No newline at end of file diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 004e083..0000000 --- a/.npmignore +++ /dev/null @@ -1,24 +0,0 @@ -# docs -docs/ - -# web -website/ - -# examples -examples/ - -# test -tests/ - -# dev -node_modules/ -*.log -*.env -*.bun - -# system -.DS_Store -Thumbs.db - -# bun -bun.lockb diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 62e0c41..0000000 --- a/LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) 2023 Benjamin Wegener - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 3786048..c7c3799 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,15 @@ # layc -> simplified node/bun web framework +To install dependencies: -**just imagine this**: you're building a web... okay, maybe don't. +```bash +bun install +``` + +To run: + +```bash +bun run lib/index.js +``` + +This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. diff --git a/bun.lockb b/bun.lockb index 36292b7..c410cf0 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/examples/index.js b/examples/index.js index e9683a8..1fde172 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,4 +1,5 @@ -import { LayServer } from '../src'; +import { LayServer } from '../lib'; +import { google } from 'googleapis'; class InfoController { static namespace = 'info'; @@ -27,6 +28,44 @@ class InfoController { } } +class oAuthController { + static namespace = 'oauth'; + constructor(server) { + server.get('/:provider', this.auth.bind(this)); + server.get('/callback/:provider', this.callback.bind(this)); + } + + async auth(req, res) { + if (req.params.get('provider') != 'google') { + return { + messages: 'invalid provider', + data: [...req.params.entries()], + }; + } + + const scopes = 'https://www.googleapis.com/auth/youtube'; + const url = oauth2Client.generateAuthUrl({ + // 'online' (default) or 'offline' (gets refresh_token) + access_type: 'offline', + + // If you only need one scope you can pass it as a string + scope: scopes, + }); + + console.log(url); + + return Response.redirect(url, 301); + } + + callback(req, res) { + if (!req.query.get('code')) { + return new Response('/', { status: 302 }); + } + + return new Response(`/${req.query.get('code')}`); + } +} + const server = new LayServer({ controllers: [ function Main(server) { @@ -71,8 +110,9 @@ const server = new LayServer({ }); }); }, + oAuthController, InfoController, ], }); -server.listen(42069); +server.listen(Bun.env.APP_PORT ?? Bun.env.PORT ?? 42169); diff --git a/src/IO/LayRequest.js b/lib/IO/LayRequest.js similarity index 100% rename from src/IO/LayRequest.js rename to lib/IO/LayRequest.js diff --git a/src/IO/LayResponse.js b/lib/IO/LayResponse.js similarity index 100% rename from src/IO/LayResponse.js rename to lib/IO/LayResponse.js diff --git a/src/IO/MimeTypeMap.js b/lib/IO/MimeTypeMap.js similarity index 100% rename from src/IO/MimeTypeMap.js rename to lib/IO/MimeTypeMap.js diff --git a/src/LayServer.js b/lib/LayServer.js similarity index 99% rename from src/LayServer.js rename to lib/LayServer.js index 9180a20..14fb806 100644 --- a/src/LayServer.js +++ b/lib/LayServer.js @@ -136,7 +136,7 @@ export default class LayServer { return await this.router.handle(req); } - listen(port = 3000) { + listen(port = 8080) { try { this.proc = Bun.serve({ port, diff --git a/src/Routing/BaseController.js b/lib/Routing/BaseController.js similarity index 100% rename from src/Routing/BaseController.js rename to lib/Routing/BaseController.js diff --git a/src/Routing/LayRouting.js b/lib/Routing/LayRouting.js similarity index 100% rename from src/Routing/LayRouting.js rename to lib/Routing/LayRouting.js diff --git a/src/Routing/Router.js b/lib/Routing/Router.js similarity index 100% rename from src/Routing/Router.js rename to lib/Routing/Router.js diff --git a/src/Utils/Logger.js b/lib/Utils/Logger.js similarity index 100% rename from src/Utils/Logger.js rename to lib/Utils/Logger.js diff --git a/src/Utils/RequestParser.js b/lib/Utils/RequestParser.js similarity index 100% rename from src/Utils/RequestParser.js rename to lib/Utils/RequestParser.js diff --git a/src/Views/HtmlEngine.js b/lib/Views/HtmlEngine.js similarity index 100% rename from src/Views/HtmlEngine.js rename to lib/Views/HtmlEngine.js diff --git a/src/index.js b/lib/index.js similarity index 100% rename from src/index.js rename to lib/index.js diff --git a/package.json b/package.json index 5dba6b5..999b949 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,19 @@ { - "name": "layc", - "version": "1.0.0", - "main": "lib/layc.cjs", - "module": "lib/layc.js", - "scripts": { - "test": "bun test", - "examples": "bun run ./examples/index.js" - }, - "type": "module" + "name": "layc", + "version": "1.0.0", + "main": "lib/layc.cjs.js", + "module": "lib/layc.js", + "devDependencies": { + "bun-types": "latest", + "chokidar": "^3.5.3" + }, + "scripts": { + "test": "bun test", + "examples": "bun run ./examples/index.js" + }, + "type": "module", + "dependencies": { + "@googleapis/youtube": "^12.0.0", + "googleapis": "^127.0.0" + } }