This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
363 additions
and
97 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
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,28 @@ | ||
name: Build Dev Image | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'release/1.9.1' | ||
|
||
jobs: | ||
docker: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v2 | ||
- 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: Build and push | ||
id: docker_build | ||
uses: docker/build-push-action@v3 | ||
with: | ||
push: true | ||
tags: eolinker/eoapi-remote-server:1.9.1 | ||
- name: Image digest | ||
run: echo ${{ steps.docker_build.outputs.digest }} |
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,28 @@ | ||
name: Build Latest Image | ||
|
||
on: | ||
push: | ||
branches: | ||
- 'main' | ||
|
||
jobs: | ||
docker: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up QEMU | ||
uses: docker/setup-qemu-action@v2 | ||
- 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: Build and push | ||
id: docker_build | ||
uses: docker/build-push-action@v3 | ||
with: | ||
push: true | ||
tags: eolinker/eoapi-remote-server:latest | ||
- name: Image digest | ||
run: echo ${{ steps.docker_build.outputs.digest }} |
This file was deleted.
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
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,5 @@ | ||
export class MockMatchDto { | ||
req: any; | ||
projectID: number; | ||
mockID: number; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,117 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { MockMatchDto } from '@/app.dto'; | ||
import { ApiData } from '@/entities/apiData.entity'; | ||
import { ApiDataService } from '@/modules/workspace/apiData/apiData.service'; | ||
import { MockService } from '@/modules/workspace/mock/mock.service'; | ||
import { tree2obj } from '@/utils'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor( | ||
private apiDataService: ApiDataService, | ||
private mockService: MockService, | ||
) {} | ||
|
||
getHello(): string { | ||
return 'Eoapi,Hello World!'; | ||
} | ||
|
||
async mockMatch(body: MockMatchDto) { | ||
const { projectID, mockID, req } = body; | ||
if (!Number.isNaN(Number(mockID))) { | ||
try { | ||
const mock = await this.mockService.findOne({ | ||
where: { uuid: Number(mockID) }, | ||
}); | ||
if (mock === null) { | ||
return { | ||
statusCode: 404, | ||
response: { | ||
message: `mockID为${mockID}的mock不存在`, | ||
}, | ||
}; | ||
} | ||
const apiData = await this.apiDataService.findOne({ | ||
where: { uuid: Number(mock.apiDataID) }, | ||
}); | ||
if (apiData === null) { | ||
return { statusCode: 404 }; | ||
} | ||
if (mock?.createWay === 'system') { | ||
return this.matchApiData(apiData, req); | ||
} else { | ||
const result = await this.matchApiData(apiData, req); | ||
if (result.statusCode === 404) { | ||
return result; | ||
} | ||
mock.response ??= this.generateResponse(apiData.responseBody); | ||
} | ||
return mock; | ||
} catch (error) { | ||
return { | ||
response: { | ||
message: error, | ||
}, | ||
}; | ||
} | ||
// Whether the matching request mode is enabled | ||
} else { | ||
const response = await this.batchMatchApiData(projectID, req); | ||
return response; | ||
} | ||
} | ||
|
||
/** | ||
* generate response data | ||
* | ||
* @returns | ||
*/ | ||
generateResponse(responseBody: ApiData['responseBody']) { | ||
return tree2obj([].concat(responseBody), { | ||
key: 'name', | ||
valueKey: 'description', | ||
}); | ||
} | ||
/** | ||
* match apiData by method and url | ||
* | ||
* @param projectID | ||
* @param req | ||
* @returns | ||
*/ | ||
async matchApiData(apiData: ApiData, req?) { | ||
const { restParams, queryParams, method } = apiData; | ||
const { pathname } = new URL(req.url, 'http://localhost:3040'); | ||
let uri = apiData.uri.trim(); | ||
let isQueryMatch = true; | ||
if (Array.isArray(restParams) && restParams.length > 0) { | ||
const restMap = restParams.reduce( | ||
(p, c) => ((p[c.name] = c.example), p), | ||
{}, | ||
); | ||
uri = uri.replace(/\{(.+?)\}/g, (match, p) => restMap[p] ?? match); | ||
} | ||
if (Array.isArray(queryParams) && queryParams.length > 0) { | ||
const query = req.query; | ||
isQueryMatch = queryParams.every((n) => n.example === query[n.name]); | ||
} | ||
const uriReg = new RegExp(`^/?${uri}/?$`); | ||
const isMatch = | ||
method === req.method && uriReg.test(pathname) && isQueryMatch; | ||
return isMatch | ||
? { response: this.generateResponse(apiData.responseBody) } | ||
: { statusCode: 404 }; | ||
} | ||
|
||
async batchMatchApiData(projectID = 1, req) { | ||
const apiDatas = await this.apiDataService.findAll({ projectID }); | ||
let result; | ||
for (const api of apiDatas) { | ||
result = await this.matchApiData(api, req); | ||
if (result?.statusCode !== 404) { | ||
return result; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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
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
Oops, something went wrong.