-
Notifications
You must be signed in to change notification settings - Fork 0
/
restkit.ts
71 lines (55 loc) · 1.7 KB
/
restkit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
declare var require: any;
declare var process: any;
import {RouteManager} from './route';
import {RestkitServer} from './server';
import {IStaticUriPath} from './route';
import {fatal} from './error';
export interface IRestkitConfig {
server: RestkitServer;
port?: number;
timezone?: string;
staticFiles?: IStaticUriPath[];
staticPaths?: IStaticUriPath[];
middleware?: any[];
}
export class Restkit {
public static server: any;
public static config: IRestkitConfig;
public static initDefaultRestkitConfig(config: IRestkitConfig) {
this.config = config;
config.port = config.port || 8000;
config.timezone = config.timezone || 'Z';
config.staticFiles = config.staticFiles || [];
config.staticPaths = config.staticPaths || [];
this.server = config.server;
config.middleware = config.middleware || [];
}
/**
* Starts an application instance
*/
public static start(config: IRestkitConfig): Promise<any> {
return new Promise((resolve, reject) => {
this.initDefaultRestkitConfig(config);
process.env.TZ = config.timezone;
config.middleware.forEach((middleware) => {
this.server.use(middleware);
});
RouteManager.bindStaticPaths(this.server, config.staticPaths);
RouteManager.bindStaticFiles(this.server, config.staticFiles);
RouteManager.bindRoutes(this.server);
this.server.listen(config.port, () => {
console.log(`Started server on port ${config.port}`);
resolve();
})
}).catch((err: any) => {
fatal(err);
});
}
/**
* Closes the application instance
*/
public static stop() {
console.log('closed application');
this.server.close();
}
}