Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for portal / server abstraction #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/controllers/internal.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ import authenticate from '../server/middleware/authenticate.internal.middleware'

const app = express()

/**
* Assign New Portal ID to Room
*/
app.post('/portal', authenticate, async (req, res) => {
const { id, roomId } = req.body as { id: string, roomId: string }

try {
const room = await new Room().load(roomId)
await room.setPortalId(id)

res.sendStatus(200)
} catch(error) {
handleError(error, res)
}
})

/**
* Existing Portal Status Update
*/
Expand Down
5 changes: 4 additions & 1 deletion src/drivers/portals.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export const createPortal = (room: Room) => new Promise(async (resolve, reject)
const headers = await generateHeaders(room)
log(`Sending request to ${url}create with room id: ${room.id}`, [ { content: 'portals', color: 'MAGENTA' }])

await axios.post(`${url}create`, { roomId: room.id }, { headers })
const { data: portal } = await axios.post(`${url}create`, { roomId: room.id }, { headers }),
{ id, status } = portal

await room.setPortalId(id, status)

resolve()
} catch(error) {
Expand Down
12 changes: 5 additions & 7 deletions src/models/room/defs.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Document } from 'mongoose'

/**
* open - The portal is open
* starting - The portal has been created and is now starting
* creating - The portal is currently being created by the microservice
* in-queue - The portal is in a queue to be created by the microservice
* requested - A portal has been requested and is being allocated by the microservice
* waiting - The room is waiting until the right conditions are met for the microservice to be contacted
* open - The portal is connected to a server
* starting - The portal is being connected to a server
* in-queue - The room is waiting to be connected with a VM
* waiting - The room is waiting for the right conditions to be met to request a VM
*
* error - An error occured
* closed - The portal was closed for one reason or another
*/

export type PortalAllocationStatus = 'waiting' | 'requested' | 'in-queue' | 'creating' | 'starting' | 'open' | 'closed' | 'error'
export type PortalAllocationStatus = 'open' | 'starting' | 'in-queue' | 'waiting' | 'closed' | 'error'

export interface PortalAllocation {
id?: string
Expand Down
6 changes: 3 additions & 3 deletions src/models/room/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import StoredUser from '../../schemas/user.schema'
import StoredInvite from '../../schemas/invite.schema'
import StoredMessage from '../../schemas/message.schema'

import IRoom, { PortalAllocation } from './defs'
import IRoom, { PortalAllocation, PortalAllocationStatus } from './defs'
import StoredRoom from '../../schemas/room.schema'
import { destroyPortal, createPortal } from '../../drivers/portals.driver'

Expand Down Expand Up @@ -246,11 +246,11 @@ export default class Room {
}
})

setPortalId = (id: string) => new Promise<Room>(async (resolve, reject) => {
setPortalId = (id: string, status: PortalAllocationStatus) => new Promise<Room>(async (resolve, reject) => {
try {
const allocation: PortalAllocation = {
id,
status: 'creating',
status,
lastUpdatedAt: Date.now()
}

Expand Down
3 changes: 0 additions & 3 deletions src/server/websocket/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import WebSocket, { Server } from 'ws'

import Room from '../../models/room'

import WSEvent from './models/event'
import WSSocket from './models/socket'
import WSMessage from './models/message'
Expand All @@ -10,7 +8,6 @@ import log from '../../utils/log.utils'
import client, { createPubSubClient } from '../../config/redis.config'

import handleMessage from './handlers/message'
import { PortalEvent } from '../../models/portal/defs'
import handleInternalMessage, { WSInternalEvent } from './handlers/internal'
import { extractUserId, extractRoomId, UNALLOCATED_PORTALS_KEYS } from '../../utils/helpers.utils'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/helpers.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { InviteResolvable } from '../models/invite'
import { TargetResolvable } from '../models/invite/defs'
import { PortalAllocationStatus } from '../models/room/defs'

export const UNALLOCATED_PORTALS_KEYS: PortalAllocationStatus[] = ['waiting', 'requested', 'error', 'closed']
export const UNALLOCATED_PORTALS_KEYS: PortalAllocationStatus[] = ['waiting', 'closed', 'error']

export const extractUserId = (user: UserResolvable) => user ? (typeof user === 'string' ? user : user.id) : null
export const extractTargetId = (target: TargetResolvable) => target ? (typeof target === 'string' ? target : target.id) : null
Expand Down