This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorz.js
110 lines (87 loc) · 2.56 KB
/
orz.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class Orz {
async init ({ mongodbURI, baseURL, schema, secret, brokerURI, graphqlURI, serviceName }) {
// Model layer
await this.initModel(mongodbURI)
// Server layer
const { app } = await this.initServer(baseURL, schema)
// App layer
await this.initRoute(app, secret)
// Network layer
this._broker = await this.initBroker(brokerURI)
this._broker && this._broker.start()
// Service layer
graphqlURI = graphqlURI || `${baseURL}/graphql`
this._worker = await this.initWorker(brokerURI, serviceName)
this._worker && this._worker.initGraphQL(graphqlURI)
this._worker && this._worker.start()
}
async initModel (mongodbURI) {
// Mongoose
if (mongodbURI) {
this._mongoose = await require('./model')(mongodbURI).catch(err => console.error(`MongoDB :`, err))
}
}
async initServer (baseURL, schema) {
// GraphQLServer
if (!baseURL) throw new Error('Required : baseURL')
if (schema) {
const { GraphQLServer } = require('@rabbotio/rainbow')
this._server = new GraphQLServer(baseURL, schema)
} else {
const { Server } = require('@rabbotio/rainbow')
this._server = new Server(baseURL)
}
return this._server
}
async initRoute (app, secret) {
// Route
require('./routes')(app)
// OAuth
require('./oauth').init(app, secret)
return this._server
}
async initBroker (brokerURI) {
if (!brokerURI) return
const { Broker } = require('@rabbotio/rainbow')
const broker = new Broker(brokerURI)
return broker
}
async initWorker (brokerURI, serviceName) {
if (!brokerURI) return
if (!serviceName) return
const { Worker } = require('@rabbotio/rainbow')
const worker = new Worker(brokerURI, serviceName)
return worker
}
async start () {
// Server
await this._server.start()
// Ready
const { version } = require('./package.json')
console.info(`ORZ : v${version} (${process.env.NODE_ENV}) is ready, enjoy! 🚀`) // eslint-disable-line
return true
}
async stop () {
if (this._mongoose) {
await this._mongoose.disconnect()
this._mongoose = null
console.info(`MongoDB : bye!`)
}
if (this._server) {
await this._server.stop()
this._server = null
}
if (this._worker) {
await this._worker.stop()
this._worker = null
console.info(`Worker : bye!`)
}
if (this._broker) {
await this._broker.stop()
this._broker = null
console.info(`Broker : bye!`)
}
return true
}
}
module.exports = Orz