-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(socket-io): add competition controller and docker compose file
- Loading branch information
1 parent
05771ff
commit 5376636
Showing
12 changed files
with
243 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: Build Socket.IO Docker Image | ||
on: | ||
push: | ||
branches: | ||
- master | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./src/sub-app/socket-io | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
# - uses: actions/setup-node@v3 | ||
# with: | ||
# node-version: 16 | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
- name: Login to TCR | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ccr.ccs.tencentyun.com | ||
username: ${{ secrets.TCR_USERNAME }} | ||
password: ${{ secrets.TCR_TOKEN }} | ||
- name: Build and Push Image | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: ./src/sub-app/socket-io | ||
file: ./src/sub-app/socket-io/Dockerfile | ||
push: true | ||
tags: | | ||
${{ secrets.DOCKERHUB_USERNAME }}/onlinejudge3-socket-io:1 | ||
${{ secrets.DOCKERHUB_USERNAME }}/onlinejudge3-socket-io:latest | ||
ccr.ccs.tencentyun.com/${{ secrets.TCR_NSP }}/onlinejudge3-socket-io:1 | ||
ccr.ccs.tencentyun.com/${{ secrets.TCR_NSP }}/onlinejudge3-socket-io:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM sdutacm/nodebase:16.15.0 | ||
|
||
WORKDIR /app | ||
|
||
COPY package.json ./ | ||
COPY package-lock.json ./ | ||
COPY .node-version ./ | ||
COPY tsconfig.json ./ | ||
COPY tsconfig.prod.json ./ | ||
COPY src ./src | ||
RUN mv src/config/config.prod.from-file.ts src/config/config.prod.ts | ||
RUN npm i -g nodeinstall | ||
RUN nodeinstall --install-alinode 7.6.0 | ||
RUN npm ci | ||
RUN npm run build | ||
|
||
CMD npm run start:foreground |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
version: '3' | ||
services: | ||
io-1: | ||
image: sdutacm/onlinejudge3-socket-io | ||
network_mode: host | ||
env_file: | ||
- ./docker.env | ||
environment: | ||
- PORT=7011 | ||
volumes: | ||
- ./logs/onlinejudge3-be-socket-io-1:/root/logs/onlinejudge3-be-socket-io | ||
- ./config.js:/app/dist/config/config.dynamic.js:ro | ||
|
||
io-2: | ||
image: sdutacm/onlinejudge3-socket-io | ||
network_mode: host | ||
env_file: | ||
- ./docker.env | ||
environment: | ||
- PORT=7012 | ||
volumes: | ||
- ./logs/onlinejudge3-be-socket-io-2:/root/logs/onlinejudge3-be-socket-io | ||
- ./config.js:/app/dist/config/config.dynamic.js:ro | ||
|
||
io-3: | ||
image: sdutacm/onlinejudge3-socket-io | ||
network_mode: host | ||
env_file: | ||
- ./docker.env | ||
environment: | ||
- PORT=7013 | ||
volumes: | ||
- ./logs/onlinejudge3-be-socket-io-3:/root/logs/onlinejudge3-be-socket-io | ||
- ./config.js:/app/dist/config/config.dynamic.js:ro | ||
|
||
io-4: | ||
image: sdutacm/onlinejudge3-socket-io | ||
network_mode: host | ||
env_file: | ||
- ./docker.env | ||
environment: | ||
- PORT=7014 | ||
volumes: | ||
- ./logs/onlinejudge3-be-socket-io-4:/root/logs/onlinejudge3-be-socket-io | ||
- ./config.js:/app/dist/config/config.dynamic.js:ro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
TZ=Asia/Shanghai |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/sub-app/socket-io/src/app/io/controller/competition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Application } from 'midway'; | ||
import { checkEmitAuth } from '../utils/auth'; | ||
|
||
module.exports = (app: Application) => { | ||
class CompetitionController extends app.Controller { | ||
async subscribe() { | ||
const [competitionId, userId] = this.ctx.args; | ||
if (!(competitionId > 0 && userId > 0)) { | ||
return; | ||
} | ||
console.log(`[competition] client subscribe: ${competitionId} ${userId}`); | ||
const room = `competition:${competitionId}_${userId}`; | ||
this.ctx.socket.join(room); | ||
this.ctx.socket.emit('res', 'subscribed'); | ||
} | ||
|
||
async innerHttpAcceptPushData() { | ||
if (!checkEmitAuth(this.ctx, this.config)) { | ||
this.ctx.status = 403; | ||
this.ctx.body = { | ||
success: false, | ||
code: -1, | ||
msg: '403', | ||
}; | ||
return; | ||
} | ||
|
||
const { competitionId, userId, data } = this.ctx.request.body as { | ||
competitionId: number; | ||
userId: number; | ||
data: any; | ||
}; | ||
this.ctx.logger.info('[competition] innerHttpAcceptPushData:', competitionId, userId, data); | ||
this.ctx.app.io | ||
.of('/competition') | ||
.to(`competition:${competitionId}_${userId}`) | ||
.emit('d', data); | ||
this.ctx.body = { | ||
success: true, | ||
data: {}, | ||
}; | ||
} | ||
} | ||
|
||
return CompetitionController; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Context } from 'midway'; | ||
import { IAppConfig } from '@/config/config.interface'; | ||
|
||
export function checkEmitAuth(ctx: Context, config: IAppConfig) { | ||
if (config.emitAuthKey && config.emitAuthKey === ctx.request.headers['x-emit-auth']) { | ||
return true; | ||
} | ||
// if (isPrivateIp(ctx.ip)) { | ||
// return true; | ||
// } | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.