-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
68 lines (55 loc) · 1.88 KB
/
index.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
import {APIRunningState, CError, genAssert, genLogger, IApi, turtle} from "../../src/";
import * as Path from "path";
import chalk from "chalk";
/** ApiClass is an implementation of IApi */
class ApiClass implements IApi {
enabled: boolean;
runningRequest: number;
runningState: APIRunningState;
constructor() {
this.runningState = APIRunningState.PREPARED;
}
async close(): Promise<boolean> {
this.runningState = APIRunningState.CLOSING;
console.log(`closed`);
this.runningState = APIRunningState.CLOSED;
return true;
}
async start(port: number): Promise<boolean> {
this.runningState = APIRunningState.STARTING;
console.log(`listen to ${port}`);
this.runningState = APIRunningState.RUNNING;
return true;
}
}
async function start() {
/** 1. set config to turtle */
turtle.setConf(Path.resolve(__dirname, `./config.${process.env.NODE_ENV || "development"}.json`), false);
/** 2. create the api instance (or instances) */
const api = new ApiClass();
// await turtle.initialDrivers([]); // initial drivers if needed
console.log(process.argv);
console.log(process.config);
/** 3. put the api instance to turtle.startAll */
await turtle.startAll(api);
setInterval(() =>
console.log(
chalk.rgb(255 * Math.random(), 255 * Math.random(), 255 * Math.random())("update " + Date.now())
)
, 1000
);
}
start().then(() => {
console.log("service started");
let assert = genAssert("ep-api");
let log = genLogger("ep-api");
log.info("test log");
console.log("test console.log");
try {
assert.cok(false, 123, () => "test assert cok");
} catch (ex) {
console.log((ex as CError).code);
console.log((ex as CError).message);
console.log((ex as CError).name);
}
});