From 95d34d6ad3afa9f912f4b424bdc68ee38a2d6c74 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Tue, 22 Sep 2020 07:00:32 -0500 Subject: [PATCH] initial commit 2 --- .env | 7 +++++ .gitignore | 1 + app/assets/.gitkeep | 2 ++ app/configs/app.config.ts | 27 ++++++++++++++++++ app/configs/db.config.ts | 17 ++++++++++++ app/configs/server.config.ts | 18 ++++++++++++ app/http/controllers/Home.controller.ts | 10 +++++++ app/http/middleware/Test.middleware.ts | 9 ++++++ app/http/routes/home.routes.ts | 11 ++++++++ app/http/views/home.hbs | 9 ++++++ app/http/views/partials/header.hbs | 37 +++++++++++++++++++++++++ app/models/LoginAttempt.model.ts | 24 ++++++++++++++++ app/models/User.model.ts | 27 ++++++++++++++++++ app/models/http/Session.model.ts | 19 +++++++++++++ app/services/.gitkeep | 0 bundle/daton.ts | 7 +++++ bundle/daton_orm.ts | 1 + bundle/daton_units.ts | 14 ++++++++++ daton | 27 ++++++++++++++++++ import-map.json | 10 +++++++ import-map.json.dev | 10 +++++++ index.ts | 23 +++++++++++++++ tsconfig.json | 6 ++++ units.ts | 31 +++++++++++++++++++++ 24 files changed, 347 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 app/assets/.gitkeep create mode 100644 app/configs/app.config.ts create mode 100644 app/configs/db.config.ts create mode 100644 app/configs/server.config.ts create mode 100644 app/http/controllers/Home.controller.ts create mode 100644 app/http/middleware/Test.middleware.ts create mode 100644 app/http/routes/home.routes.ts create mode 100644 app/http/views/home.hbs create mode 100644 app/http/views/partials/header.hbs create mode 100644 app/models/LoginAttempt.model.ts create mode 100644 app/models/User.model.ts create mode 100644 app/models/http/Session.model.ts create mode 100644 app/services/.gitkeep create mode 100644 bundle/daton.ts create mode 100644 bundle/daton_orm.ts create mode 100644 bundle/daton_units.ts create mode 100755 daton create mode 100644 import-map.json create mode 100644 import-map.json.dev create mode 100755 index.ts create mode 100644 tsconfig.json create mode 100644 units.ts diff --git a/.env b/.env new file mode 100644 index 0000000..78c6773 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +DATON_LOGGING_LEVEL=6 +APP_NAME="My First Daton App!" + +DB_USERNAME=garrettmills +DB_PASSWORD=garrettmills +DB_DATABASE=garrettmills + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42cc410 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea* \ No newline at end of file diff --git a/app/assets/.gitkeep b/app/assets/.gitkeep new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/app/assets/.gitkeep @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/app/configs/app.config.ts b/app/configs/app.config.ts new file mode 100644 index 0000000..3d75ddc --- /dev/null +++ b/app/configs/app.config.ts @@ -0,0 +1,27 @@ +import { Daton } from '../../bundle/daton.ts' +const { env, ViewEngine } = Daton + +export default { + name: env('APP_NAME', 'Daton'), + + views: { + /* + * View engine that should be used to render templates. + * Options are Handlebars, Ejs, or Denjuck. + */ + engine: ViewEngine.Handlebars, + + /* + * Relative path from the app directory to the base directory where + * view files should be looked up. + */ + base_dir: 'http/views', + + /* + * If using Handlebars, optionally, the path to the directory within the + * base_dir that contains the partials. They will be automatically registered + * with Handlebars. + */ + partials_dir: 'partials', + }, +} diff --git a/app/configs/db.config.ts b/app/configs/db.config.ts new file mode 100644 index 0000000..56f4e8b --- /dev/null +++ b/app/configs/db.config.ts @@ -0,0 +1,17 @@ +import { Daton } from '../../bundle/daton.ts' +const { env } = Daton + +export default { + + connections: { + default: { + type: env('DB_TYPE', 'postgres'), + user: env('DB_USERNAME', 'daton'), + password: env('DB_PASSWORD'), + database: env('DB_DATABASE', 'daton'), + hostname: env('DB_HOSTNAME', 'localhost'), + port: env('DB_PORT', 5432), + } + } + +} diff --git a/app/configs/server.config.ts b/app/configs/server.config.ts new file mode 100644 index 0000000..3ad5062 --- /dev/null +++ b/app/configs/server.config.ts @@ -0,0 +1,18 @@ +export default { + port: 8080, + use_ssl: false, + prefix: '/', + allow_mount_without_prefix: false, + + request_timeout: 15000, // 15 seconds + + powered_by: { + enable: true, + text: 'Daton', + }, + + session: { + driver: 'database', // memory | database + model: 'http:Session', // required for database + }, +} diff --git a/app/http/controllers/Home.controller.ts b/app/http/controllers/Home.controller.ts new file mode 100644 index 0000000..f8dec8a --- /dev/null +++ b/app/http/controllers/Home.controller.ts @@ -0,0 +1,10 @@ +import {Controller, Request, view, Injectable} from '../../../bundle/daton.ts' + +@Injectable() +export default class HomeController extends Controller { + + async get_home(request: Request) { + return view('home', { greeting: 'Hello' }) + } + +} diff --git a/app/http/middleware/Test.middleware.ts b/app/http/middleware/Test.middleware.ts new file mode 100644 index 0000000..eb287ef --- /dev/null +++ b/app/http/middleware/Test.middleware.ts @@ -0,0 +1,9 @@ +import { Middleware, Request, http, HTTPStatus } from '../../../bundle/daton.ts' + +export default class TestMiddleware extends Middleware { + public async handleRequest(request: Request) { + if ( Math.random() >= 0.5 ) { + return http(HTTPStatus.FORBIDDEN, 'Well, you were unlucky.') + } + } +} diff --git a/app/http/routes/home.routes.ts b/app/http/routes/home.routes.ts new file mode 100644 index 0000000..06fae5d --- /dev/null +++ b/app/http/routes/home.routes.ts @@ -0,0 +1,11 @@ +import { RouterDefinition } from '../../../bundle/daton.ts' + +export default { + prefix: '/', + middleware: [], + get: { + '/': 'controller::Home.get_home', + '/maybe': ['middleware::Test', 'controller::Home.get_home'], + '/statics/**': { handler: 'daton::static', arg: 'assets' }, + }, +} as RouterDefinition diff --git a/app/http/views/home.hbs b/app/http/views/home.hbs new file mode 100644 index 0000000..842cd09 --- /dev/null +++ b/app/http/views/home.hbs @@ -0,0 +1,9 @@ + + {{> header }} + +
+

Welcome to Daton

+

Daton is an opinionated application framework for Deno

+
+ + \ No newline at end of file diff --git a/app/http/views/partials/header.hbs b/app/http/views/partials/header.hbs new file mode 100644 index 0000000..fbe8abe --- /dev/null +++ b/app/http/views/partials/header.hbs @@ -0,0 +1,37 @@ + + + + + Daton + + + + + diff --git a/app/models/LoginAttempt.model.ts b/app/models/LoginAttempt.model.ts new file mode 100644 index 0000000..f6cba86 --- /dev/null +++ b/app/models/LoginAttempt.model.ts @@ -0,0 +1,24 @@ +import { Model, Type, Field, Relation } from '../../bundle/daton_orm.ts' +import User from "./User.model.ts"; + +export default class LoginAttempt extends Model { + protected static table = 'daton_login_attempts' + protected static key = 'daton_login_attempt_id' + + protected static readonly CREATED_AT = 'attempt_date' + protected static readonly UPDATED_AT = null + + @Field(Type.int) + protected daton_login_attempt_id?: number + + @Field(Type.int) + protected user_id!: number + + @Field(Type.timestamp) + protected attempt_date!: Date + + @Relation() + public user() { + return this.belongs_to_one(User, 'login_attempts') + } +} diff --git a/app/models/User.model.ts b/app/models/User.model.ts new file mode 100644 index 0000000..d6f1089 --- /dev/null +++ b/app/models/User.model.ts @@ -0,0 +1,27 @@ +import { Model, Type, Field, Relation } from '../../bundle/daton_orm.ts' +import LoginAttemptModel from './LoginAttempt.model.ts' + +export default class User extends Model { + protected static table = 'daton_users' + protected static key = 'user_id' + + protected static readonly CREATED_AT = 'created_at' + protected static readonly UPDATED_AT = 'updated_at' + + @Field(Type.int) + protected user_id?: number + + @Field(Type.varchar) + protected first_name!: String + + @Field(Type.varchar) + protected last_name!: String + + @Field(Type.bool) + protected active!: Boolean + + @Relation() + public login_attempts() { + return this.has_many(LoginAttemptModel) + } +} diff --git a/app/models/http/Session.model.ts b/app/models/http/Session.model.ts new file mode 100644 index 0000000..4bb6ee7 --- /dev/null +++ b/app/models/http/Session.model.ts @@ -0,0 +1,19 @@ +import { SessionModel } from '../../../bundle/daton.ts' +import { Field, Type } from '../../../bundle/daton_orm.ts' + +export default class Session extends SessionModel { + protected static table = 'sessions' + protected static key = 'session_key' + + protected static readonly CREATED_AT = 'start_time' + protected static readonly UPDATED_AT = null // No updated at + + @Field(Type.varchar) + protected session_key!: string + + @Field(Type.int) + protected user_id?: number + + @Field(Type.timestamp) + protected start_time!: Date +} diff --git a/app/services/.gitkeep b/app/services/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/bundle/daton.ts b/bundle/daton.ts new file mode 100644 index 0000000..7178be1 --- /dev/null +++ b/bundle/daton.ts @@ -0,0 +1,7 @@ +export * from 'daton-lib/module.ts' +export * from 'daton/di/module.ts' + +import * as std from 'daton-external/std.ts' +export { std } + +export * as Daton from 'daton-lib/mod.ts' diff --git a/bundle/daton_orm.ts b/bundle/daton_orm.ts new file mode 100644 index 0000000..0ac1864 --- /dev/null +++ b/bundle/daton_orm.ts @@ -0,0 +1 @@ +export * from 'daton-orm/module.ts' diff --git a/bundle/daton_units.ts b/bundle/daton_units.ts new file mode 100644 index 0000000..d547b02 --- /dev/null +++ b/bundle/daton_units.ts @@ -0,0 +1,14 @@ +export { default as ConfigUnit } from 'daton-lib/unit/Config.ts' +export { DatabaseUnit } from 'daton-orm/DatabaseUnit.ts' +export { default as ControllerUnit } from 'daton-lib/unit/Controllers.ts' +export { default as MiddlewareUnit } from 'daton-lib/unit/Middlewares.ts' +export { default as RoutesUnit } from 'daton-lib/unit/Routes.ts' +export { default as HttpKernelUnit } from 'daton-lib/unit/HttpKernel.ts' +export { default as ModelsUnit } from 'daton-orm/ModelsUnit.ts' +export { default as HttpServerUnit } from 'daton-lib/unit/HttpServer.ts' +export { default as RoutingUnit } from 'daton-lib/unit/Routing.ts' +export { default as ServicesUnit } from 'daton-lib/unit/Services.ts' +export { default as ViewEngineUnit } from 'daton-lib/unit/ViewEngine.ts' +export { default as DatonMiddlewareUnit } from 'daton-lib/unit/DatonMiddleware.ts' +export { default as CLIAppUnit } from 'daton-cli/CLIAppUnit.ts' +export { default as CLIUnit } from 'daton-cli/CLIUnit.ts' diff --git a/daton b/daton new file mode 100755 index 0000000..97b55c2 --- /dev/null +++ b/daton @@ -0,0 +1,27 @@ +#!/usr/bin/env -S deno run -c ./tsconfig.json --unstable --allow-read --allow-env --allow-net --importmap=./import-map.json +/* Main executable for the Daton app. */ + +import { make, Scaffolding, Application } from './bundle/daton.ts' +import { CLIAppUnit } from './bundle/daton_units.ts' +import units from './units.ts' + +units.pop() +units.push(CLIAppUnit) + +/* + * Let's get up and running. The scaffolding provides the bare minimum + * amount of support required to get Daton up and running. The app handles + * the rest. + * + * Daton will automatically load and process application resources, which is + * why we need to pass in the base path of this script. + */ +const scaffolding = make(Scaffolding, import.meta.url) +await scaffolding.up() + +/* + * Now, import the units and start the application. The units define each + * modular piece of functionality that is managed by the Daton app. + */ +const app = make(Application, units) +await app.run() diff --git a/import-map.json b/import-map.json new file mode 100644 index 0000000..621d501 --- /dev/null +++ b/import-map.json @@ -0,0 +1,10 @@ +{ + "imports": { + "daton/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/", + "daton-lib/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/lib/src/", + "daton-di/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/di/src/", + "daton-orm/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/orm/src/", + "daton-cli/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/cli/src/", + "daton-external/": "https://code.garrettmills.dev/garrettmills/daton/raw/branch/master/lib/src/external/" + } +} diff --git a/import-map.json.dev b/import-map.json.dev new file mode 100644 index 0000000..626b284 --- /dev/null +++ b/import-map.json.dev @@ -0,0 +1,10 @@ +{ + "imports": { + "daton/": "file:///home/garrettmills/Projects/daton/", + "daton-lib/": "file:///home/garrettmills/Projects/daton/lib/src/", + "daton-di/": "file:///home/garrettmills/Projects/daton/di/src/", + "daton-orm/": "file:///home/garrettmills/Projects/daton/orm/src/", + "daton-cli/": "file:///home/garrettmills/Projects/daton/cli/src/", + "daton-external/": "file:///home/garrettmills/Projects/daton/lib/src/external/" + } +} diff --git a/index.ts b/index.ts new file mode 100755 index 0000000..ae6abee --- /dev/null +++ b/index.ts @@ -0,0 +1,23 @@ +#!/usr/bin/env -S deno run -c ./tsconfig.json --unstable --allow-read --allow-env --allow-net --importmap=./import-map.json +/* Main executable for the Daton app. */ + +import { make, Scaffolding, Application } from './bundle/daton.ts' +import units from './units.ts' + +/* + * Let's get up and running. The scaffolding provides the bare minimum + * amount of support required to get Daton up and running. The app handles + * the rest. + * + * Daton will automatically load and process application resources, which is + * why we need to pass in the base path of this script. + */ +const scaffolding = make(Scaffolding, import.meta.url) +await scaffolding.up() + +/* + * Now, import the units and start the application. The units define each + * modular piece of functionality that is managed by the Daton app. + */ +const app = make(Application, units) +await app.run() diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..6dacb8c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "experimentalDecorators": true, + "emitDecoratorMetadata": true + } +} diff --git a/units.ts b/units.ts new file mode 100644 index 0000000..c4715e9 --- /dev/null +++ b/units.ts @@ -0,0 +1,31 @@ +import { + ConfigUnit, + DatabaseUnit, + ControllerUnit, + MiddlewareUnit, + RoutesUnit, + HttpKernelUnit, + ModelsUnit, + HttpServerUnit, + RoutingUnit, + ServicesUnit, + ViewEngineUnit, + DatonMiddlewareUnit, + CLIUnit, +} from './bundle/daton_units.ts' + +export default [ + ServicesUnit, + ConfigUnit, + CLIUnit, + DatabaseUnit, + ModelsUnit, + ViewEngineUnit, + HttpKernelUnit, + DatonMiddlewareUnit, + MiddlewareUnit, + ControllerUnit, + RoutesUnit, + RoutingUnit, + HttpServerUnit, +]