-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
62 lines (59 loc) · 1.78 KB
/
index.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
const grpc = require('grpc')
const notesProto = grpc.load('notes.proto')
const uuidv1 = require('uuid/v1')
const server = new grpc.Server()
const notes = [
{ id: '1', title: 'Note 1', content: 'Content 1'},
{ id: '2', title: 'Note 2', content: 'Content 2'}
]
server.addService(notesProto.NoteService.service, {
list: (_, callback) => {
callback(null, notes)
},
get: (call, callback) => {
let note = notes.find((n) => n.id == call.request.id)
if (note) {
callback(null, note)
} else {
callback({
code: grpc.status.NOT_FOUND,
details: "Not found"
})
}
},
insert: (call, callback) => {
let note = call.request
note.id = uuidv1()
notes.push(note)
callback(null, note)
},
update: (call, callback) => {
let existingNote = notes.find((n) => n.id == call.request.id)
if (existingNote) {
existingNote.title = call.request.title
existingNote.content = call.request.content
callback(null, existingNote)
} else {
callback({
code: grpc.status.NOT_FOUND,
details: "Not found"
})
}
},
delete: (call, callback) => {
let existingNoteIndex = notes.findIndex((n) => n.id == call.request.id)
if (existingNoteIndex != -1) {
notes.splice(existingNoteIndex, 1)
callback(null, {})
} else {
callback({
code: grpc.status.NOT_FOUND,
details: "Not found"
})
}
}
})
server.bind('127.0.0.1:50051',
grpc.ServerCredentials.createInsecure())
console.log('Server running at http://127.0.0.1:50051')
server.start()