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 http mode to library microservice #480

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f1a47ee
Implementation of cloudcmd
MadsKelberg Dec 3, 2023
6f0213b
Socket.io is not working yet
MadsKelberg Dec 6, 2023
d4a6492
Implemented cloudcmd with a working socket
MadsKelberg Dec 8, 2023
62bdd77
Syncing options file now
MadsKelberg Dec 8, 2023
66dd8cb
Moved cloudcmd into an external function
MadsKelberg Dec 8, 2023
31764b5
Updated cloudcmd config & added .env default
MadsKelberg Dec 11, 2023
79c2150
Added function parameter LOCAL_PATH to cloudCMD function
MadsKelberg Jan 17, 2024
593c9d3
Made tests cloudcmd
MadsKelberg Jan 17, 2024
a8ef58e
Moved the test command into package.json
MadsKelberg Jan 18, 2024
7ac9190
Removed resolveJsonModule from tsconfig
MadsKelberg Jan 18, 2024
3c70121
Removed cloudcmd test from all
MadsKelberg Jan 18, 2024
bf16e80
Adds pm2-based yarn commands to libms
prasadtalasila Jan 21, 2024
a24887e
Adds pm2-based yarn commands to libms
prasadtalasila Jan 21, 2024
58b40ef
Adds pm2-based yarn commands to libms
prasadtalasila Jan 21, 2024
897f2e7
fix libms tests
prasadtalasila Jan 21, 2024
1079487
fix libms tests
prasadtalasila Jan 21, 2024
2b8d546
fix libms tests
prasadtalasila Jan 22, 2024
a0fbb5d
fix libms tests
prasadtalasila Jan 22, 2024
008e04a
Updates the config flags and README
prasadtalasila Jan 22, 2024
bc1e426
Checks libms.yml
prasadtalasila Jan 22, 2024
d7bc2ef
Fixes yarn commands and updates READMEs
prasadtalasila Jan 22, 2024
804aeea
Fix lib tests in pre-commit config file
prasadtalasila Jan 23, 2024
390e0b8
Updates E2E tests
prasadtalasila Jan 24, 2024
cf18ec9
Check libms tests
prasadtalasila Jan 24, 2024
3f88765
fix linting errors
prasadtalasila Jan 24, 2024
b319c35
fix libms github actions
prasadtalasila Jan 24, 2024
09f6a5c
libms tests
prasadtalasila Jan 27, 2024
cd927e4
Improve cloudcmd tests
prasadtalasila Jan 27, 2024
eded659
fix libms tests
prasadtalasila Jan 27, 2024
d89c712
fix libms tests
prasadtalasila Jan 27, 2024
6d10bb6
fix libms tests and updates diagrams
prasadtalasila Jan 27, 2024
9d7ef7a
fix libms tests
prasadtalasila Jan 27, 2024
7e3c5eb
fix codeclimate issues
prasadtalasila Jan 27, 2024
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
20 changes: 19 additions & 1 deletion .github/workflows/lib-ms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ jobs:
- name: Setup node
uses: actions/setup-node@v3
with:
node-version: 18
node-version: 20
cache: "yarn"
cache-dependency-path: "**/yarn.lock"

# - name: Install pm2
# run: |
# npm install -g pm2

- name: Run the linting checks
run: |
cd servers/lib
Expand All @@ -43,6 +47,7 @@ jobs:
run: |
cd servers/lib
yarn install
yarn build
yarn test:all
env:
PORT: 4001
Expand All @@ -51,6 +56,19 @@ jobs:
LOG_LEVEL: debug
APOLLO_PATH: /lib

# - name: Run http mode tests
# run: |
# cd servers/lib
# yarn install
# yarn build
# yarn test:http-github
# env:
# PORT: 4002
# LOCAL_PATH: ${{ github.workspace }}/files
# MODE: local
# LOG_LEVEL: debug
# APOLLO_PATH: /lib

- name: Upload test coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ repos:
files: "^servers/execution/runner/.*"
args: ["-c", "cd servers/execution/runner && yarn test:nocov"]
stages: [pre-push]
# - id: yarn-test-lib
# name: yarn test lib
# entry: bash
# language: system
# files: "^servers/lib/.*"
# args: ["-c", "cd servers/lib && yarn jest . --coverage=false"]
# stages: [pre-push]
- id: yarn-test-lib
name: yarn test lib
entry: bash
language: system
files: "^servers/lib/.*"
args: ["-c", "cd servers/lib && yarn test:nocov"]
stages: [pre-push]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.1.0
Expand Down
531 changes: 490 additions & 41 deletions docs/developer/system/DTaaS.drawio

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions script/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ fi
sudo apt-get update -y
sudo apt-get install -y yarn
sudo npm install -g serve
sudo npm install -g pm2
11 changes: 7 additions & 4 deletions servers/lib/DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ yarn syntax # Analyze code for errors and style issues
yarn format #format .ts[x] and .js[x] files with prettier
yarn graph # Generate dependency graphs in the code
yarn build # Compile ES6 to ES5 and copy JS files to build/ directory
yarn test:unit # Run all tests
yarn test:e2e # Run end-to-end tests
yarn test:unit # Run unit tests
yarn test:int # Run integration tests
yarn test:all # Run unit tests
yarn test:e2e # Run end-to-end tests
yarn test:http # Run tests for HTTP file service provided by cloudcmd
yarn test:all # Run all tests except http tests
yarn start -h # List of all the CLI commands
yarn start # Start the application
yarn start:pm2 # Start the application with process manager 2
yarn stop:pm2 # Stop the application managed by process manager 2
yarn clean # Deletes "build", "coverage", "dist" and other temp files
```

**NOTE:** The integration and end-to-end tests require a valid
**NOTE:** The integration, end-to-end and http tests require a valid
`.env` file. Here is a sample file.

```ini
Expand Down
47 changes: 41 additions & 6 deletions servers/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ GRAPHQL_PLAYGROUND='false' or 'true'

The `LOCAL_PATH` variable is the absolute filepath to the
location of the local directory which will be served to users
by the Library microservice.
by the Library microservice. This
[sample configuration file](./config/.env.default) can be used.

Replace the default values the appropriate values for your setup.

Expand All @@ -49,10 +50,24 @@ Replace the default values the appropriate values for your setup.
Display help.

```bash
libms -h
$libms -h
Usage: libms [options]

The lib microservice is a file server. It supports file transfer
over GraphQL and HTTP protocols.

Options:
-c, --config <file> provide the config file (default .env)
-H, --http <file> enable the HTTP server with the specified config
-h, --help display help for libms
```

The config is saved `.env` file by convention. The **libms** looks for
Both the options are not mandatory.

### Configuration file

The config is saved `.env` file by convention. If `-c` is not specified
The **libms** looks for
`.env` file in the working directory from which it is run.
If you want to run **libms** without explicitly specifying the configuration
file, run
Expand All @@ -69,15 +84,35 @@ libms --config FILE-PATH
```

If the environment file is named something other than `.env`,
for example as `.env.development`, you can run
for example as `config/.env.default`, you can run

```sh
libms -c ".env.development"
libms -c "config/.env.default"
```

You can press `Ctl+C` to halt the application.

The microservice is available at: 'localhost:PORT/lib'
### Protocol Support

The **libms** supports GraphQL protocol by default.
It is possible to enable the HTTP protocol by setting
the `-H` option.

To run **libms** with a custom config for HTTP protocol, use

```bash
libms -H FILE-PATH
libms --http FILE-PATH
```

A sample configuration is [available](./config/http.json).

### Accessible URLs

The microservice is available at:

**GraphQL protocol**: 'localhost:PORT/lib'
**HTTP protocol**: 'localhost:PORT/lib/files'

<!-- markdownlint-disable-next-line MD013 -->
The [API](https://into-cps-association.github.io/DTaaS/development/user/servers/lib/LIB-MS.html) page shows sample queries and responses.
6 changes: 6 additions & 0 deletions servers/lib/config/.env.default
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PORT='4001'
MODE='local' or 'gitlab'
LOCAL_PATH ='/Users/<Username>/DTaaS/files'
LOG_LEVEL='debug'
APOLLO_PATH='/lib' or ''
GRAPHQL_PLAYGROUND='false' or 'true'
30 changes: 30 additions & 0 deletions servers/lib/config/http.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "DTaaS Fileserver",
"auth": false,
"editor": "edward",
"packer": "zip",
"diff": true,
"zip": true,
"buffer": true,
"dirStorage": true,
"online": false,
"open": false,
"oneFilePanel": true,
"keysPanel": false,
"prefix": "/lib/files",
"confirmCopy": true,
"confirmMove": true,
"showConfig": false,
"showFileName": true,
"contact": false,
"configDialog": false,
"console": false,
"terminal": false,
"vim": false,
"columns": "name-size-date-owner-mode",
"export": false,
"import": false,
"dropbox": false,
"dropboxToken": "",
"log": true
}
14 changes: 12 additions & 2 deletions servers/lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@into-cps-association/libms",
"version": "0.3.2",
"version": "0.4.0",
"description": "microservices that handles request by fetching and returning the file-names and folders of given directory",
"author": "[email protected]",
"contributors": [
Expand All @@ -16,12 +16,18 @@
"format": "prettier --ignore-path ../.gitignore --write \"**/*.{ts,tsx,css,scss}\"",
"graph": "npx madge --image src.svg src && npx madge --image test.svg test",
"start": "node dist/src/main.js",
"start:pm2": "pm2 start pm2.config.js",
"stop:pm2": "pm2 delete libms",
"syntax": "npx eslint . --fix",
"pretest": "npx shx cp test/data/user2/tools/README.md ../../files/user2/tools/README.md",
"posttest": "npx rimraf ../../files/user2/tools/README.md",
"test:all": "npx cross-env LOCAL_PATH=test/data jest --coverage",
"test:all": "npx cross-env LOCAL_PATH=test/data jest --testPathIgnorePatterns=cloudcmd --coverage",
"test:e2e": "npx cross-env LOCAL_PATH=test/data jest --config ./test/jest-e2e.json --coverage",
"test:http": "yarn build && pm2 start -f --name libms-test dist/src/main.js -- -c .env -H ./config/http.json && jest test/cloudcmd --coverage --coverageThreshold=\"{}\" && pm2 delete libms-test",
"test:http-nocov": "yarn build && pm2 start -f --name libms-test dist/src/main.js -- -c .env -H ./config/http.json && jest test/cloudcmd --coverage=false && pm2 delete libms-test",
"test:http-github": "yarn build && yarn start:pm2 && jest test/cloudcmd --coverage --coverageThreshold=\"{}\" && pm2 delete libms",
"test:int": "npx cross-env LOCAL_PATH=test/data jest ../test/integration --coverage",
"test:nocov": "yarn test:http-nocov && npx cross-env LOCAL_PATH=test/data jest --testPathIgnorePatterns=cloudcmd --coverage=false",
"test:unit": "npx cross-env LOCAL_PATH=test/data jest ../test/unit --coverage"
},
"bin": "./dist/src/main.js",
Expand All @@ -34,12 +40,15 @@
"@nestjs/core": "^10.3.0",
"@nestjs/graphql": "^12.0.11",
"@nestjs/platform-express": "^10.3.0",
"axios": "^1.5.1",
"cloudcmd": "^16.17.7",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"graphql": "^16.8.1",
"mock-fs": "^5.2.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"socket.io": "^4.7.2",
"type-graphql": "^2.0.0-beta.3"
},
"devDependencies": {
Expand All @@ -52,6 +61,7 @@
"@types/supertest": "^2.0.13",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"cross-fetch": "^4.0.0",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
10 changes: 10 additions & 0 deletions servers/lib/pm2.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
apps : [
{
name: "libms",
script: "./dist/src/main.js",
watch: false,
args: "-H ./config/http.json"
}
]
}
8 changes: 8 additions & 0 deletions servers/lib/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import * as dotenv from 'dotenv';
import AppModule from './app.module';
import cloudCMD from './cloudcmd/cloudcmd';

type BootstrapOptions = {
config?: string;
httpServer?: string;
runHelp?: CallableFunction;
};

Expand All @@ -22,8 +24,14 @@ export default async function bootstrap(options?: BootstrapOptions) {
process.exit(1);
}
}

const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const port = configService.get<number>('PORT');

if (options.httpServer) {
cloudCMD(app, options.httpServer, configService.get<string>('LOCAL_PATH'));
}

await app.listen(port);
}
32 changes: 32 additions & 0 deletions servers/lib/src/cloudcmd/cloudcmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { INestApplication } from '@nestjs/common';
import { Server } from 'socket.io';
import * as cloudcmd from 'cloudcmd';
import { join } from 'path';

const runCloudCMD = (
app: INestApplication,
optionsPath: string,
filesPath: string,
) => {
const { createConfigManager } = cloudcmd;
const configManager = createConfigManager({
configPath: join(process.cwd(), optionsPath),
});

configManager('root', filesPath);

const server = app.getHttpServer();

const socket = new Server(server, {
path: `${configManager('prefix')}/socket.io`,
});
app.use(
configManager('prefix'),
cloudcmd({
configManager,
socket,
}),
);
};

export default runCloudCMD;
19 changes: 12 additions & 7 deletions servers/lib/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import bootstrap from './bootstrap';

type ProgramOptions = {
config?: string;
http?: string;
};

const program = new Command();

program
.description(
'The lib microservice is responsible for handling and serving the contents of library assets of the DTaaS platform. It provides API endpoints for clients to query, and fetch these assets.',
'The lib microservice is a file server. It supports file transfer over GraphQL and HTTP protocols.',
)
.option('-c, --config <file>', 'provide the config file (default .env)')
.option(
'-H, --http <file>',
'enable the HTTP server with the specified config',
)
.option('-c, --config <path>', 'set the config path (default .env)')
.helpOption('-h, --help', 'display help for libms')
.showHelpAfterError();

program.parse(process.argv);

const options: ProgramOptions = program.opts();

if (options.config) {
bootstrap({ config: options.config, runHelp: () => program.help() });
} else {
bootstrap({ runHelp: () => program.help() });
}
bootstrap({
config: options.config,
httpServer: options.http,
runHelp: () => program.help(),
});
Loading
Loading