get mongo running
This commit is contained in:
@@ -56,8 +56,6 @@ services:
|
||||
MONGO_INITDB_DATABASE: DB
|
||||
networks:
|
||||
- rush-character-net
|
||||
ports:
|
||||
- 27017:27017
|
||||
mongo-express:
|
||||
image: mongo-express
|
||||
restart: always
|
||||
|
||||
@@ -1,13 +1,41 @@
|
||||
import { Server, ServerCredentials } from '@grpc/grpc-js'
|
||||
import { MongoClient } from 'mongodb'
|
||||
|
||||
import { initCharacterService } from './character_service'
|
||||
import { DatabaseService } from './database'
|
||||
|
||||
const port = 8000
|
||||
|
||||
const app = new Server()
|
||||
|
||||
const client = new MongoClient('mongodb://rushvault:rushvault@mongo:27017/')
|
||||
|
||||
initCharacterService(app)
|
||||
|
||||
app.bindAsync('0.0.0.0:8080', ServerCredentials.createInsecure(), () => {
|
||||
async function testDb() {
|
||||
try {
|
||||
// await client.connect()
|
||||
const database = client.db('test')
|
||||
const potato = database.collection('potato')
|
||||
|
||||
await potato.insertOne({ name: 'potato', potato: 'potato' })
|
||||
} finally {
|
||||
await client.close()
|
||||
}
|
||||
}
|
||||
|
||||
app.bindAsync('0.0.0.0:8080', ServerCredentials.createInsecure(), async () => {
|
||||
await testDb().catch(console.dir)
|
||||
|
||||
let db = new DatabaseService(
|
||||
'mongodb://rushvault:rushvault@mongo:27017/',
|
||||
'rush-vault',
|
||||
)
|
||||
console.log(await db.insertCharacter({
|
||||
playerName: 'badger',
|
||||
characterName: 'potato',
|
||||
characterAlias: ['spud', 'tuber'],
|
||||
}))
|
||||
|
||||
console.log('Starting server at 0.0.0.0:8080')
|
||||
})
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
Server,
|
||||
ServerUnaryCall,
|
||||
sendUnaryData,
|
||||
loadPackageDefinition,
|
||||
} from '@grpc/grpc-js'
|
||||
import { Server } from '@grpc/grpc-js'
|
||||
import { ReflectionService } from '@grpc/reflection'
|
||||
import { loadSync } from '@grpc/proto-loader'
|
||||
|
||||
@@ -13,45 +8,33 @@ import {
|
||||
CreateCharacterRequest,
|
||||
} from './proto/character'
|
||||
|
||||
import { characterManagerDefinition } from './proto/character.grpc-server'
|
||||
import {
|
||||
characterManagerDefinition,
|
||||
ICharacterManager,
|
||||
} from './proto/character.grpc-server'
|
||||
|
||||
export function initCharacterService(server: Server) {
|
||||
server.addService(characterManagerDefinition, {
|
||||
createCharacter: createCharacter_Call,
|
||||
getCharacter: getCharacter_Call,
|
||||
})
|
||||
server.addService(characterManagerDefinition, CharacterManagerService)
|
||||
|
||||
new ReflectionService(loadSync('./src/proto/character.proto')).addToServer(
|
||||
server,
|
||||
)
|
||||
}
|
||||
|
||||
function createCharacter_Call(
|
||||
call: ServerUnaryCall<CreateCharacterRequest, Character>,
|
||||
callback: sendUnaryData<Character>,
|
||||
) {
|
||||
callback(null, createCharacter(call.request))
|
||||
}
|
||||
|
||||
function createCharacter(request: CreateCharacterRequest): Character {
|
||||
const CharacterManagerService: ICharacterManager = {
|
||||
createCharacter: (request: CreateCharacterRequest): Character => {
|
||||
return {
|
||||
playerName: 'test player',
|
||||
characterName: 'test character',
|
||||
characterAlias: ['test alias'],
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
function getCharacter_Call(
|
||||
call: ServerUnaryCall<GetCharacterRequest, Character>,
|
||||
callback: sendUnaryData<Character>,
|
||||
) {
|
||||
callback(null, getCharacter(call.request))
|
||||
}
|
||||
|
||||
function getCharacter(request: GetCharacterRequest): Character {
|
||||
getCharacter: (request: GetCharacterRequest): Character => {
|
||||
return {
|
||||
playerName: 'test player',
|
||||
characterName: 'test character',
|
||||
characterAlias: ['test alias'],
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
38
vault/src/database.ts
Normal file
38
vault/src/database.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { MongoClient, Db, Collection } from 'mongodb'
|
||||
import { Character } from './proto/character'
|
||||
|
||||
export class DatabaseService {
|
||||
private readonly client: MongoClient
|
||||
private readonly db: Db
|
||||
private readonly characterCollection: Collection
|
||||
|
||||
constructor(databaseAddress: string, databaseName: string) {
|
||||
this.client = new MongoClient(databaseAddress)
|
||||
this.db = this.client.db(databaseName)
|
||||
this.characterCollection = this.db.collection('characters')
|
||||
}
|
||||
|
||||
insertCharacter(character: Character): Promise<Character> {
|
||||
return this.characterCollection
|
||||
.insertOne(character)
|
||||
.then((insertResult) => {
|
||||
console.log(insertResult)
|
||||
return this.characterCollection.findOne({ _id: insertResult.insertedId })
|
||||
})
|
||||
.then((inserted) => {
|
||||
return {
|
||||
playerName: inserted.playerName,
|
||||
characterName: inserted.characterName,
|
||||
characterAlias: inserted.characterAlias,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fetchCharactersForPlayer(playerName: string): Array<Character> {
|
||||
return []
|
||||
}
|
||||
|
||||
fetchCharactersForCharacterName(playerName: string): Array<Character> {
|
||||
return []
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user