Skip to content

Commit

Permalink
feat: basic framework
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiming committed Feb 27, 2018
1 parent 7e2fc46 commit fc50e38
Show file tree
Hide file tree
Showing 9 changed files with 693 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"semi": false,
"semi": true,
"singleQuote": true
}
10 changes: 10 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
database:
name: tinylog
host: 127.0.0.1
port: 3306
username: root
password:
logging: true

env: development
port: 8080
13 changes: 13 additions & 0 deletions ormconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "mysql",
"host": "127.0.0.1",
"port": 3306,
"username": "root",
"database": "tinylog",
"entities": ["src/entities/**/*.ts"],
"migrations": ["src/migrations/**/*.ts"],
"cli": {
"entitiesDir": "./src/entities",
"migrationsDir": "./src/migrations"
}
}
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@
"commitmsg": "validate-commit-msg",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
},
"dependencies": {},
"dependencies": {
"config": "^1.30.0",
"koa": "^2.5.0",
"koa-jwt": "^3.3.0",
"koa-logger": "^3.1.0",
"reflect-metadata": "^0.1.12",
"routing-controllers": "^0.7.6",
"typedi": "^0.7.0",
"typeorm": "^0.1.13"
},
"devDependencies": {
"@types/koa": "^2.0.44",
"@types/koa-logger": "^3.1.0",
"@types/mocha": "^2.2.41",
"@types/node": "^9.3.0",
"@types/node": "^9.4.6",
"@types/power-assert": "^1.4.29",
"@types/supertest": "^2.0.0",
"codecov": "^3.0.0",
Expand Down
84 changes: 33 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,33 @@
/**
* .,:,,, .::,,,::.
* .::::,,;;, .,;;:,,....:i:
* :i,.::::,;i:. ....,,:::::::::,.... .;i:,. ......;i.
* :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.
* ;;..,::::;;;;ri,,,. ..,,:;s1s1ssrr;,.;r,
* :;. ,::;ii;:, . ................... .;iirri;;;,,;i,
* ,i. .;ri:. ... ............................ .,,:;:,,,;i:
* :s,.;r:... ....................................... .::;::s;
* ,1r::. .............,,,.,,:,,........................,;iir;
* ,s;........... ..::.,;:,,. ...............,;1s
* :i,..,. .,:,,::,. .......... .......;1,
* ir,....:rrssr;:, ,,.,::. .r5S9989398G95hr;. ....,.:s,
* ;r,..,s9855513XHAG3i .,,,,,,,. ,S931,.,,.;s;s&BHHA8s.,..,..:r:
* :r;..rGGh, :SAG;;G@BS:.,,,,,,,,,.r83: hHH1sXMBHHHM3..,,,,.ir.
* ,si,.1GS, sBMAAX&MBMB5,,,,,,:,,.:&8 3@HXHBMBHBBH#X,.,,,,,,rr
* ;1:,,SH: .A@&&B#&8H#BS,,,,,,,,,.,5XS, 3@MHABM&59M#As..,,,,:,is,
* .rr,,,;9&1 hBHHBB&8AMGr,,,,,,,,,,,:h&&9s; r9&BMHBHMB9: . .,,,,;ri.
* :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr. ...,:rs.
* ;s. .:sS8G8GG889hi. ....,,:;:,.:irssrriii:,. ...,,i1,
* ;1, ..,....,,isssi;, .,,. ....,.i1,
* ;h: i9HHBMBBHAX9: . ...,,,rs,
* ,1i.. :A#MBBBBMHB##s ....,,,;si.
* .r1,.. ,..;3BMBBBHBB#Bh. .. ....,,,,,i1;
* :h;.. .,..;,1XBMMMMBXs,.,, .. :: ,. ....,,,,,,ss.
* ih: .. .;;;, ;;:s58A3i,.. ,. ,.:,,. ...,,,,,:,s1,
* .s1,.... .,;sh, ,iSAXs;. ,. ,,.i85 ...,,,,,,:i1;
* .rh: ... rXG9XBBM#M#MHAX3hss13&&HHXr .....,,,,,,,ih;
* .s5: ..... i598X&&A&AAAAAA&XG851r: ........,,,,:,,sh;
* . ihr, ... . .. ........,,,,,;11:.
* ,s1i. ... ..,,,..,,,.,,.,,.,.. ........,,.,,.;s5i.
* .:s1r,...................... ..............;shs,
* . .:shr:. .... ..............,ishs.
* .,issr;,... ...........................,is1s;.
* .,is1si;:,....................,:;ir1sr;,
* ..:isssssrrii;::::::;;iirsssssr;:..
* .,::iiirsssssssssrri;;:.
*
* For the brave souls who get this far: You are the chosen ones,
* the valiant knights of programming who toil away, without rest,
* fixing our most awful code. To you, true saviors, kings of men,
* I say this: never gonna give you up, never gonna let you down,
* never gonna run around and desert you. Never gonna make you cry,
* never gonna say goodbye. Never gonna tell a lie and hurt you.
*
* 致终于来到这里的勇敢的人:
* 你是被上帝选中的人,是英勇的、不敌辛苦的、不眠不休的来修改我们这最棘手的代码的编程骑士。
* 你,我们的救世主,人中之龙,我要对你说:永远不要放弃,永远不要对自己失望,永远不要逃走,辜负了自己,
* 永远不要哭啼,永远不要说再见,永远不要说谎来伤害自己。
*/
console.log('Hello World!')
import 'reflect-metadata';
import * as Koa from 'koa';
import * as logger from 'koa-logger';
import * as config from 'config';
import { useContainer as useContainerForRoute, useKoaServer } from 'routing-controllers';
import { Container } from 'typedi';
import { useContainer as useContainerForOrm } from 'typeorm';
import { database } from './libraries/database';

const { port } = config;

useContainerForRoute(Container);
useContainerForOrm(Container);

const app = new Koa();

app.use(logger());

useKoaServer(app, {
cors: true,
routePrefix: '/v1',
controllers: [`${__dirname}/controllers/*.{js,ts}`],
defaultErrorHandler: true
});

export const connection = database().then(async c => {
return new Promise(resolve => {
app.listen(port, () => {
console.log(`[APP] Listen on ${port} in ${config.env} enviroment`);
resolve(app.callback());
});
});
});
31 changes: 31 additions & 0 deletions src/libraries/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'reflect-metadata';
import * as config from 'config';
import * as path from 'path';
import { Connection, createConnection } from 'typeorm';

const dbConfig = config.database;

const root = path.resolve(__dirname, '..');
const entityPath = `${root}/entities/*.{js,ts}`;
const migrationPath = `${root}/migrations/*.{js,ts}`;

export const database: () => Promise<Connection> = async () =>
createConnection({
type: 'mysql',
host: dbConfig.host,
port: dbConfig.port,
username: dbConfig.username,
password: dbConfig.password,
database: dbConfig.name,
entities: [entityPath],
migrations: [migrationPath],
migrationsRun: true,
cli: {
migrationsDir: path.resolve(__dirname, '..', 'migrations')
},
charset: 'utf8mb4_general_ci',
logging: dbConfig.logging
}).then(c => {
console.log('[DB] Database Connected');
return c;
});
14 changes: 14 additions & 0 deletions src/utils/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module 'config' {
export const database: {
name: string;
host: string;
port: number;
username: string;
password: string;
sync: boolean;
logging: boolean;
};

export const env: string;
export const port: number;
}
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"defaultSeverity": "warning",
"extends": ["tslint:recommended", "tslint-eslint-rules", "tslint-config-prettier"],
"rules": {
"no-implicit-dependencies": [true, "dev"]
"no-implicit-dependencies": [true, "dev"],
"ordered-imports": false,
"object-literal-sort-keys": false,
"semicolon": [true, "always"]
}
}
Loading

0 comments on commit fc50e38

Please sign in to comment.