Get it to store data from grpc calls.

This commit is contained in:
iamBadgers
2025-06-13 21:17:35 -07:00
parent 1bcaedc293
commit 7230d526e0
8 changed files with 104 additions and 430 deletions

View File

@@ -16,7 +16,6 @@ COPY frontend/. .
RUN npm i -g serve
RUN npm run build_protos
RUN npm run build
EXPOSE 8080

View File

@@ -9,14 +9,14 @@ import skipFormatting from '@vue/eslint-config-prettier/skip-formatting'
// More info at https://github.com/vuejs/eslint-config-typescript/#advanced-setup
export default defineConfigWithVueTs(
{
name: 'app/files-to-lint',
files: ['**/*.{ts,mts,tsx,vue}'],
},
// {
// name: 'app/files-to-lint',
// files: ['**/*.{ts,mts,tsx,vue}'],
// },
globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']),
// globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']),
pluginVue.configs['flat/essential'],
vueTsConfigs.recommended,
skipFormatting,
// pluginVue.configs['flat/essential'],
// vueTsConfigs.recommended,
// skipFormatting,
)

View File

@@ -6,7 +6,7 @@
"scripts": {
"dev": "vite",
"build_protos": "mkdir -p src/proto & npx protoc -I=../proto/ --ts_out ./src/proto --proto_path ../proto ../proto/*.proto",
"build": "run-p type-check \"build-only {@}\" --",
"build": "run-p build_protos && run-p type-check \"build-only {@}\" --",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --build",

View File

@@ -11,6 +11,8 @@ message Character {
repeated string CharacterAlias = 3;
}
message CreateCharacterRequest {}
message CreateCharacterRequest {
Character characterData = 1;
}
message GetCharacterRequest {}

View File

@@ -1,373 +0,0 @@
/**
* Generated by the protoc-gen-ts. DO NOT EDIT!
* compiler version: 3.20.3
* source: character.proto
* git: https://github.com/thesayyn/protoc-gen-ts */
import * as pb_1 from 'google-protobuf'
import * as grpc_1 from '@grpc/grpc-js'
export class Character extends pb_1.Message {
#one_of_decls: number[][] = []
constructor(
data?:
| any[]
| {
PlayerName?: string
CharacterName?: string
CharacterAlias?: string[]
},
) {
super()
pb_1.Message.initialize(
this,
Array.isArray(data) ? data : [],
0,
-1,
[3],
this.#one_of_decls,
)
if (!Array.isArray(data) && typeof data == 'object') {
if ('PlayerName' in data && data.PlayerName != undefined) {
this.PlayerName = data.PlayerName
}
if ('CharacterName' in data && data.CharacterName != undefined) {
this.CharacterName = data.CharacterName
}
if ('CharacterAlias' in data && data.CharacterAlias != undefined) {
this.CharacterAlias = data.CharacterAlias
}
}
}
get PlayerName() {
return pb_1.Message.getFieldWithDefault(this, 1, '') as string
}
set PlayerName(value: string) {
pb_1.Message.setField(this, 1, value)
}
get CharacterName() {
return pb_1.Message.getFieldWithDefault(this, 2, '') as string
}
set CharacterName(value: string) {
pb_1.Message.setField(this, 2, value)
}
get CharacterAlias() {
return pb_1.Message.getFieldWithDefault(this, 3, []) as string[]
}
set CharacterAlias(value: string[]) {
pb_1.Message.setField(this, 3, value)
}
static fromObject(data: {
PlayerName?: string
CharacterName?: string
CharacterAlias?: string[]
}): Character {
const message = new Character({})
if (data.PlayerName != null) {
message.PlayerName = data.PlayerName
}
if (data.CharacterName != null) {
message.CharacterName = data.CharacterName
}
if (data.CharacterAlias != null) {
message.CharacterAlias = data.CharacterAlias
}
return message
}
toObject() {
const data: {
PlayerName?: string
CharacterName?: string
CharacterAlias?: string[]
} = {}
if (this.PlayerName != null) {
data.PlayerName = this.PlayerName
}
if (this.CharacterName != null) {
data.CharacterName = this.CharacterName
}
if (this.CharacterAlias != null) {
data.CharacterAlias = this.CharacterAlias
}
return data
}
serialize(): Uint8Array
serialize(w: pb_1.BinaryWriter): void
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter()
if (this.PlayerName.length) writer.writeString(1, this.PlayerName)
if (this.CharacterName.length) writer.writeString(2, this.CharacterName)
if (this.CharacterAlias.length)
writer.writeRepeatedString(3, this.CharacterAlias)
if (!w) return writer.getResultBuffer()
}
static deserialize(bytes: Uint8Array | pb_1.BinaryReader): Character {
const reader =
bytes instanceof pb_1.BinaryReader
? bytes
: new pb_1.BinaryReader(bytes),
message = new Character()
while (reader.nextField()) {
if (reader.isEndGroup()) break
switch (reader.getFieldNumber()) {
case 1:
message.PlayerName = reader.readString()
break
case 2:
message.CharacterName = reader.readString()
break
case 3:
pb_1.Message.addToRepeatedField(message, 3, reader.readString())
break
default:
reader.skipField()
}
}
return message
}
serializeBinary(): Uint8Array {
return this.serialize()
}
static deserializeBinary(bytes: Uint8Array): Character {
return Character.deserialize(bytes)
}
}
export class CreateCharacterRequest extends pb_1.Message {
#one_of_decls: number[][] = []
constructor(data?: any[] | {}) {
super()
pb_1.Message.initialize(
this,
Array.isArray(data) ? data : [],
0,
-1,
[],
this.#one_of_decls,
)
if (!Array.isArray(data) && typeof data == 'object') {
}
}
static fromObject(data: {}): CreateCharacterRequest {
const message = new CreateCharacterRequest({})
return message
}
toObject() {
const data: {} = {}
return data
}
serialize(): Uint8Array
serialize(w: pb_1.BinaryWriter): void
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter()
if (!w) return writer.getResultBuffer()
}
static deserialize(
bytes: Uint8Array | pb_1.BinaryReader,
): CreateCharacterRequest {
const reader =
bytes instanceof pb_1.BinaryReader
? bytes
: new pb_1.BinaryReader(bytes),
message = new CreateCharacterRequest()
while (reader.nextField()) {
if (reader.isEndGroup()) break
switch (reader.getFieldNumber()) {
default:
reader.skipField()
}
}
return message
}
serializeBinary(): Uint8Array {
return this.serialize()
}
static deserializeBinary(bytes: Uint8Array): CreateCharacterRequest {
return CreateCharacterRequest.deserialize(bytes)
}
}
export class GetCharacterRequest extends pb_1.Message {
#one_of_decls: number[][] = []
constructor(data?: any[] | {}) {
super()
pb_1.Message.initialize(
this,
Array.isArray(data) ? data : [],
0,
-1,
[],
this.#one_of_decls,
)
if (!Array.isArray(data) && typeof data == 'object') {
}
}
static fromObject(data: {}): GetCharacterRequest {
const message = new GetCharacterRequest({})
return message
}
toObject() {
const data: {} = {}
return data
}
serialize(): Uint8Array
serialize(w: pb_1.BinaryWriter): void
serialize(w?: pb_1.BinaryWriter): Uint8Array | void {
const writer = w || new pb_1.BinaryWriter()
if (!w) return writer.getResultBuffer()
}
static deserialize(
bytes: Uint8Array | pb_1.BinaryReader,
): GetCharacterRequest {
const reader =
bytes instanceof pb_1.BinaryReader
? bytes
: new pb_1.BinaryReader(bytes),
message = new GetCharacterRequest()
while (reader.nextField()) {
if (reader.isEndGroup()) break
switch (reader.getFieldNumber()) {
default:
reader.skipField()
}
}
return message
}
serializeBinary(): Uint8Array {
return this.serialize()
}
static deserializeBinary(bytes: Uint8Array): GetCharacterRequest {
return GetCharacterRequest.deserialize(bytes)
}
}
interface GrpcUnaryServiceInterface<P, R> {
(
message: P,
metadata: grpc_1.Metadata,
options: grpc_1.CallOptions,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientUnaryCall
(
message: P,
metadata: grpc_1.Metadata,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientUnaryCall
(
message: P,
options: grpc_1.CallOptions,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientUnaryCall
(message: P, callback: grpc_1.requestCallback<R>): grpc_1.ClientUnaryCall
}
interface GrpcStreamServiceInterface<P, R> {
(
message: P,
metadata: grpc_1.Metadata,
options?: grpc_1.CallOptions,
): grpc_1.ClientReadableStream<R>
(message: P, options?: grpc_1.CallOptions): grpc_1.ClientReadableStream<R>
}
interface GrpWritableServiceInterface<P, R> {
(
metadata: grpc_1.Metadata,
options: grpc_1.CallOptions,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientWritableStream<P>
(
metadata: grpc_1.Metadata,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientWritableStream<P>
(
options: grpc_1.CallOptions,
callback: grpc_1.requestCallback<R>,
): grpc_1.ClientWritableStream<P>
(callback: grpc_1.requestCallback<R>): grpc_1.ClientWritableStream<P>
}
interface GrpcChunkServiceInterface<P, R> {
(
metadata: grpc_1.Metadata,
options?: grpc_1.CallOptions,
): grpc_1.ClientDuplexStream<P, R>
(options?: grpc_1.CallOptions): grpc_1.ClientDuplexStream<P, R>
}
interface GrpcPromiseServiceInterface<P, R> {
(
message: P,
metadata: grpc_1.Metadata,
options?: grpc_1.CallOptions,
): Promise<R>
(message: P, options?: grpc_1.CallOptions): Promise<R>
}
export abstract class UnimplementedCharacterManagerService {
static definition = {
createCharacter: {
path: '/CharacterManager/createCharacter',
requestStream: false,
responseStream: false,
requestSerialize: (message: CreateCharacterRequest) =>
Buffer.from(message.serialize()),
requestDeserialize: (bytes: Buffer) =>
CreateCharacterRequest.deserialize(new Uint8Array(bytes)),
responseSerialize: (message: Character) =>
Buffer.from(message.serialize()),
responseDeserialize: (bytes: Buffer) =>
Character.deserialize(new Uint8Array(bytes)),
},
getCharacter: {
path: '/CharacterManager/getCharacter',
requestStream: false,
responseStream: false,
requestSerialize: (message: GetCharacterRequest) =>
Buffer.from(message.serialize()),
requestDeserialize: (bytes: Buffer) =>
GetCharacterRequest.deserialize(new Uint8Array(bytes)),
responseSerialize: (message: Character) =>
Buffer.from(message.serialize()),
responseDeserialize: (bytes: Buffer) =>
Character.deserialize(new Uint8Array(bytes)),
},
};
[method: string]: grpc_1.UntypedHandleCall
abstract createCharacter(
call: grpc_1.ServerUnaryCall<CreateCharacterRequest, Character>,
callback: grpc_1.sendUnaryData<Character>,
): void
abstract getCharacter(
call: grpc_1.ServerUnaryCall<GetCharacterRequest, Character>,
callback: grpc_1.sendUnaryData<Character>,
): void
}
export class CharacterManagerClient extends grpc_1.makeGenericClientConstructor(
UnimplementedCharacterManagerService.definition,
'CharacterManager',
{},
) {
constructor(
address: string,
credentials: grpc_1.ChannelCredentials,
options?: Partial<grpc_1.ChannelOptions>,
) {
super(address, credentials, options)
}
createCharacter: GrpcUnaryServiceInterface<
CreateCharacterRequest,
Character
> = (
message: CreateCharacterRequest,
metadata:
| grpc_1.Metadata
| grpc_1.CallOptions
| grpc_1.requestCallback<Character>,
options?: grpc_1.CallOptions | grpc_1.requestCallback<Character>,
callback?: grpc_1.requestCallback<Character>,
): grpc_1.ClientUnaryCall => {
return super.createCharacter(message, metadata, options, callback)
}
getCharacter: GrpcUnaryServiceInterface<GetCharacterRequest, Character> = (
message: GetCharacterRequest,
metadata:
| grpc_1.Metadata
| grpc_1.CallOptions
| grpc_1.requestCallback<Character>,
options?: grpc_1.CallOptions | grpc_1.requestCallback<Character>,
callback?: grpc_1.requestCallback<Character>,
): grpc_1.ClientUnaryCall => {
return super.getCharacter(message, metadata, options, callback)
}
}

View File

@@ -1,41 +1,46 @@
import { Server, ServerCredentials } from '@grpc/grpc-js'
import { MongoClient } from 'mongodb'
import { initCharacterService } from './character_service'
import { CharacterService } from './character_service'
import { DatabaseService } from './database'
const port = 8000
const port = 8080
const address = `0.0.0.0:${port}`
const app = new Server()
const client = new MongoClient('mongodb://rushvault:rushvault@mongo:27017/')
initCharacterService(app)
// initCharacterService(app)
async function testDb() {
try {
// await client.connect()
const database = client.db('test')
const potato = database.collection('potato')
const characterService = new CharacterService(client, "rush-vault")
await potato.insertOne({ name: 'potato', potato: 'potato' })
} finally {
await client.close()
}
}
characterService.addToServer(app)
app.bindAsync('0.0.0.0:8080', ServerCredentials.createInsecure(), async () => {
await testDb().catch(console.dir)
// async function testDb() {
// try {
// // await client.connect()
// const database = client.db('test')
// const potato = database.collection('potato')
let db = new DatabaseService(
'mongodb://rushvault:rushvault@mongo:27017/',
'rush-vault',
)
console.log(await db.insertCharacter({
playerName: 'badger',
characterName: 'potato',
characterAlias: ['spud', 'tuber'],
}))
// await potato.insertOne({ name: 'potato', potato: 'potato' })
// } finally {
// await client.close()
// }
// }
console.log('Starting server at 0.0.0.0:8080')
app.bindAsync(address, ServerCredentials.createInsecure(), () => {
// 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 ${address}`)
})

View File

@@ -1,6 +1,7 @@
import { Server } from '@grpc/grpc-js'
import { Server, ServerUnaryCall, sendUnaryData } from '@grpc/grpc-js'
import { ReflectionService } from '@grpc/reflection'
import { loadSync } from '@grpc/proto-loader'
import { MongoClient, Collection, Db } from 'mongodb'
import {
Character,
@@ -13,28 +14,66 @@ import {
ICharacterManager,
} from './proto/character.grpc-server'
export function initCharacterService(server: Server) {
server.addService(characterManagerDefinition, CharacterManagerService)
export class CharacterService {
reflectionService: ReflectionService
characterManager: ICharacterManager
new ReflectionService(loadSync('./src/proto/character.proto')).addToServer(
server,
mongoClient: MongoClient
db: Db
characterCollection: Collection
constructor(mongoClient: MongoClient, dbName: string) {
this.reflectionService = new ReflectionService(
loadSync('./src/proto/character.proto'),
)
}
const CharacterManagerService: ICharacterManager = {
createCharacter: (request: CreateCharacterRequest): Character => {
return {
this.characterManager = {
createCharacter: this.createCharacterCall.bind(this),
getCharacter: this.getCharacterCall.bind(this),
}
this.mongoClient = mongoClient
this.db = this.mongoClient.db(dbName)
this.characterCollection = this.db.collection('Characters')
}
addToServer(server: Server) {
server.addService(characterManagerDefinition, this.characterManager)
this.reflectionService.addToServer(server)
}
async createCharacterCall(
call: ServerUnaryCall<CreateCharacterRequest, Character>,
callback: sendUnaryData<Character>,
) {
let newCharacter = await this.characterCollection
.insertOne(call.request.characterData)
.then((insertResult) => {
console.log(insertResult)
return this.characterCollection.findOne({
_id: insertResult.insertedId,
})
})
console.log(newCharacter)
callback(null, {
playerName: newCharacter.playerName,
characterName: newCharacter.characterName,
characterAlias: newCharacter.characterAlias,
})
}
getCharacterCall(
call: ServerUnaryCall<GetCharacterRequest, Character>,
callback: sendUnaryData<Character>,
) {
callback(null, {
playerName: 'test player',
characterName: 'test character',
characterAlias: ['test alias'],
})
}
},
getCharacter: (request: GetCharacterRequest): Character => {
return {
playerName: 'test player',
characterName: 'test character',
characterAlias: ['test alias'],
}
},
}

View File

@@ -17,7 +17,9 @@ export class DatabaseService {
.insertOne(character)
.then((insertResult) => {
console.log(insertResult)
return this.characterCollection.findOne({ _id: insertResult.insertedId })
return this.characterCollection.findOne({
_id: insertResult.insertedId,
})
})
.then((inserted) => {
return {