Skip to content

Commit

Permalink
resolve hashgraph#1923,resolve hashgraph#1693
Browse files Browse the repository at this point in the history
Integrate multi env feature
Service Env can Override
configs folder in dockerfile & managed GUARDIAN_ENV
docs

Signed-off-by: otherNet <[email protected]>
  • Loading branch information
otherNet committed Apr 14, 2023
1 parent 2e39f47 commit a1e81a0
Show file tree
Hide file tree
Showing 58 changed files with 23,751 additions and 29,124 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## GUARDIAN ECOSYSTEM ENVIRONMENT NAME
# if you already started to use actively Guardian leave GUARDIAN_ENV empty
# when you want to use it as usual
# if you want to use a new environment configure GUARDIAN_ENV with a significative environment name
GUARDIAN_ENV=""
14 changes: 0 additions & 14 deletions .env.example

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tsconfig.tsbuildinfo
.pnp.*
node_modules
runtime-data/
000_*

# OSX related metadata removal and IDE nonsense
.DS_store
Expand Down
49 changes: 33 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ When building the reference implementation, you can [manually build every compon

* [Docker](https://www.docker.com)

If you build with docker [MongoDB V6](https://www.mongodb.com), [NodeJS V16](https://nodejs.org) and [Nats 1.12.2](https://nats.io/) will be installed and configured automatically. Make sure you use Docker Compose V2 (comes with Docker Desktop > 3.6.0) as at https://docs.docker.com/compose/install/
If you build with docker [MongoDB V6](https://www.mongodb.com), [NodeJS V16](https://nodejs.org) and [Nats 1.12.2](https://nats.io/) will be installed and configured automatically.

### Installation

Expand All @@ -47,18 +47,14 @@ If you build with docker [MongoDB V6](https://www.mongodb.com), [NodeJS V16](htt
git clone https://github.com/hashgraph/guardian.git
```

2. Update the following files with your Hedera Testnet account info (see prerequisites) as indicated. Please check complete steps to generate Operation ID and Operator Key by looking at link : [How to Create Operator ID and Operator Key](https://docs.hedera.com/guardian/getting-started/getting-started/how-to-create-operator-id-and-operator-key). There will be other steps in the Demo Usage Guide that will require the generation of Operator IDs and Operator Keys. It is important to mention that the Operator IDs and Operator Keys in the .env will be used to generate demo accounts.
2. Update the following files with your Hedera Testnet account info (see prerequisites) as indicated. Please check complete steps to generate Operation ID and Operator Key by looking at link : [How to Create Operator ID and Operator Key](https://docs.hedera.com/guardian/getting-started/getting-started/how-to-create-operator-id-and-operator-key). There will be other steps in the Demo Usage Guide that will require the generation of Operator IDs and Operator Keys. It is important to mention that the Operator IDs and Operator Keys in the ./guardian/configs/.env.\<GUARDIAN_ENV\>.guardian.system will be used to generate demo accounts.

For example:

in `guardian-service/.env`:

```plaintext
OPERATOR_ID=""
OPERATOR_KEY=""
```
in ./guardian/.env you may choose name of the Guardian platform. Leave the field empty or unspecified if you update a production environment to keep previous data (for more details read at https://github.com/IntellectEU/guardian/blob/feature/environment/docs/environments/Ecosystem-Envitonment.md)
GUARDIAN_ENV="develop"

in `guardian-service/.env.docker`:
in ./guardian/configs/.env.develop.guardian.system

```plaintext
OPERATOR_ID=""
Expand All @@ -75,20 +71,20 @@ If you build with docker [MongoDB V6](https://www.mongodb.com), [NodeJS V16](htt

For example: https://github.com/yeasy/docker-ipfs

3.1.2 For setup IPFS local node you need to set variables in `worker-service` folder
3.1.2 For setup IPFS local node you need to set variables in the same file ./guardian/configs/.env.develop.guardian.system

```
IPFS_NODE_ADDRESS="..." # Default IPFS_NODE_ADDRESS="http://localhost:5002"
IPFS_PUBLIC_GATEWAY="..." # Default IPFS_PUBLIC_GATEWAY="https://localhost:8080/ipfs/${cid}"
IPFS_PUBLIC_GATEWAY='...' # Default IPFS_PUBLIC_GATEWAY='https://localhost:8080/ipfs/${cid}'
IPFS_PROVIDER="local"
```
**Note**
1. Default IPFS_NODE_ADDRESS="http://localhost:5002"
2. Default IPFS_PUBLIC_GATEWAY="https://localhost:8080/ipfs/${cid}"
2. Default IPFS_PUBLIC_GATEWAY='https://localhost:8080/ipfs/${cid}'

**3.2 Setting up IPFS Web3Storage node:**

3.2.1 For setup IPFS web3storage node you need to set variables in `worker-service` folder:
3.2.1 For setup IPFS web3storage node you need to set variables in the same file ./guardian/configs/.env.develop.guardian.system

```
IPFS_STORAGE_API_KEY="..."
Expand All @@ -100,8 +96,10 @@ If you build with docker [MongoDB V6](https://www.mongodb.com), [NodeJS V16](htt
4. Build and launch with Docker. Please note that this build is meant to be used in production and will not contain any debug information. From the project's root folder:
```shell
docker-compose up -d --build
docker compose up -d --build
```
**Note**. about docker-compose: from the end of June 2023 Compose V1 won’t be supported anymore and will be removed from all Docker Desktop versions. Make sure you use Docker Compose V2 (comes with Docker Desktop > 3.6.0) as at https://docs.docker.com/compose/install/
5. Browse to <http://localhost:3000> and complete the setup.
Expand All @@ -118,6 +116,25 @@ If you want to manually build every component with debug information, then build
### Build and start each component
Install, configure and start all the prerequisites, then build and start each component.
Configure .env file in each service
For example:
in `guardian-service/.env`:
```plaintext
GUARDIAN_ENV="develop"
```
If need to configure OVERRIDE variables add it in .env file.
```plaintext
OVERRIDE="false"
```
in `guardian-service/configs/.env.guardian.develop`:
```plaintext
OPERATOR_ID=""
OPERATOR_KEY=""
```
**Note: Once you start each service, please wait for the initialization process to be completed.**
Expand Down Expand Up @@ -329,7 +346,7 @@ Install, configure and start all the prerequisites, then build and start each co
2. Start local development using docker compose

```shell
docker-compose -f docker-compose-dev.yml up --build
docker compose -f docker-compose-dev.yml up --build
```

3. Access local development using <http://localhost:3000> or <http://localhost:4200>
Expand All @@ -345,7 +362,7 @@ Install, configure and start all the prerequisites, then build and start each co
**To run by cleaning Docker cache**:

```shell
docker-compose build --no-cache
docker compose build --no-cache
```

([back to top](readme))
Expand Down
10 changes: 1 addition & 9 deletions api-gateway/.env
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
ENV="develop"
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="api-gateway"
MRV_ADDRESS="http://localhost:3003/mrv"
#MQ_MESSAGE_CHUNK=5000000
#RAW_REQUEST_LIMIT="1gb"
#JSON_REQUEST_LIMIT="1mb"
GUARDIAN_ENV=""
3 changes: 2 additions & 1 deletion api-gateway/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ COPY --from=interfacesModuleBuilder /usr/local/interfaces/guardian-interfaces-*.
COPY --from=commonModuleBuilder /usr/local/common/guardian-common-*.tgz /tmp/common.tgz
COPY --from=apiGatewayBuilder /usr/local/api-gateway/yarn.lock ./
COPY ./api-gateway/package.json ./
# COPY ./api-gateway/.env.docker ./.env
RUN mkdir -p ./configs
COPY ./api-gateway/configs/* ./configs
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/interfaces']='file:/tmp/interfaces.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/common']='file:/tmp/common.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN yarn install --frozen-lockfile
Expand Down
4 changes: 2 additions & 2 deletions api-gateway/.env.docker → api-gateway/configs/.env.gateway
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="message-broker"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="api-gateway"
MRV_ADDRESS="http://message-broker:3003/mrv"
MRV_ADDRESS="http://localhost:3003/mrv"
#MQ_MESSAGE_CHUNK=5000000
#RAW_REQUEST_LIMIT="1gb"
#JSON_REQUEST_LIMIT="1mb"
8 changes: 8 additions & 0 deletions api-gateway/configs/.env.gateway.develop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="api-gateway"
MRV_ADDRESS="http://localhost:3003/mrv"
#MQ_MESSAGE_CHUNK=5000000
#RAW_REQUEST_LIMIT="1gb"
#JSON_REQUEST_LIMIT="1mb"
8 changes: 8 additions & 0 deletions api-gateway/configs/.env.gateway.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
HEDERA_NET=""
PREUSED_HEDERA_NET=""
MQ_ADDRESS=""
SERVICE_CHANNEL="api-gateway"
MRV_ADDRESS="http://localhost:3003/mrv"
#MQ_MESSAGE_CHUNK=5000000
#RAW_REQUEST_LIMIT="1gb"
#JSON_REQUEST_LIMIT="1mb"
22 changes: 22 additions & 0 deletions api-gateway/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moduleAlias from 'module-alias';
import dotenv from 'dotenv';
import 'reflect-metadata'
import fs from 'fs';

moduleAlias.addAliases({
'@api': __dirname + '/api',
Expand All @@ -10,3 +11,24 @@ moduleAlias.addAliases({
});

dotenv.config();

const envPath = process.env.GUARDIAN_ENV ? `./configs/.env.gateway.${process.env.GUARDIAN_ENV}` : './configs/.env.gateway';

if (!process.env.OVERRIDE || process.env.OVERRIDE === 'false'){
console.log('reading from', envPath, 'not overriding');
dotenv.config({ path: envPath});
}else{
try {
const envConfig = dotenv.parse(fs.readFileSync(envPath));
for (const k of Object.keys(envConfig)) {
process.env[k] = envConfig[k]
}
} catch (err) {
if (err.code === 'ENOENT') {
console.log('WARN: Specific environment not loaded');
} else {
throw err;
}
}
}
console.log('Charged Environment',process.env,'\r\n___ . ___');
17 changes: 1 addition & 16 deletions auth-service/.env
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
ENV="develop"
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="auth-service"
ACCESS_TOKEN_SECRET="youraccesstokensecret"
DB_HOST="localhost"
DB_DATABASE="auth_db"
VAULT_PROVIDER="database"
HASHICORP_TOKEN="1234"
HASHICORP_ADDRESS="http://localhost:8200"
HASHICORP_NAMESPACE="admin"
HASHICORP_ENCRIPTION_ALG="sha512"
#HASHICORP_USEAL_KEY=""
#IMPORT_KEYS_FROM_DB=1
#MQ_MESSAGE_CHUNK=5000000
GUARDIAN_ENV=""
3 changes: 2 additions & 1 deletion auth-service/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ COPY --from=interfacesModuleBuilder /usr/local/interfaces/guardian-interfaces-*.
COPY --from=commonModuleBuilder /usr/local/common/guardian-common-*.tgz /tmp/common.tgz
COPY --from=authServiceBuilder /usr/local/auth-service/yarn.lock ./
COPY ./auth-service/package.json ./
# COPY ./auth-service/.env.docker ./.env
RUN mkdir -p ./configs
COPY ./auth-service/configs/* ./configs
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/interfaces']='file:/tmp/interfaces.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/common']='file:/tmp/common.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN yarn install --frozen-lockfile
Expand Down
8 changes: 4 additions & 4 deletions auth-service/.env.docker → auth-service/configs/.env.auth
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="message-broker"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="auth-service"
ACCESS_TOKEN_SECRET="youraccesstokensecret"
DB_HOST="mongo"
DB_HOST="localhost"
DB_DATABASE="auth_db"
VAULT_PROVIDER="database"
HASHICORP_TOKEN="1234"
HASHICORP_ADDRESS="http://vault:8200"
HASHICORP_ADDRESS="http://localhost:8200"
HASHICORP_NAMESPACE="admin"
HASHICORP_ENCRIPTION_ALG="sha512"
#HASHICORP_UNSEAL_KEY=""
#HASHICORP_USEAL_KEY=""
#IMPORT_KEYS_FROM_DB=1
#MQ_MESSAGE_CHUNK=5000000
15 changes: 15 additions & 0 deletions auth-service/configs/.env.auth.develop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
HEDERA_NET="testnet"
PREUSED_HEDERA_NET="testnet"
MQ_ADDRESS="localhost"
SERVICE_CHANNEL="auth-service"
ACCESS_TOKEN_SECRET="youraccesstokensecret"
DB_HOST="localhost"
DB_DATABASE="auth_db"
VAULT_PROVIDER="database"
HASHICORP_TOKEN="1234"
HASHICORP_ADDRESS="http://localhost:8200"
HASHICORP_NAMESPACE="admin"
HASHICORP_ENCRIPTION_ALG="sha512"
#HASHICORP_USEAL_KEY=""
#IMPORT_KEYS_FROM_DB=1
#MQ_MESSAGE_CHUNK=5000000
15 changes: 15 additions & 0 deletions auth-service/configs/.env.auth.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
HEDERA_NET=""
PREUSED_HEDERA_NET=""
MQ_ADDRESS=""
SERVICE_CHANNEL="auth-service"
ACCESS_TOKEN_SECRET="youraccesstokensecret"
DB_HOST=""
DB_DATABASE="auth_db"
VAULT_PROVIDER="database"
HASHICORP_TOKEN="1234"
HASHICORP_ADDRESS="http://localhost:8200"
HASHICORP_NAMESPACE="admin"
HASHICORP_ENCRIPTION_ALG="sha512"
#HASHICORP_USEAL_KEY=""
#IMPORT_KEYS_FROM_DB=1
#MQ_MESSAGE_CHUNK=5000000
22 changes: 22 additions & 0 deletions auth-service/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moduleAlias from 'module-alias';
import dotenv from 'dotenv';
import fs from 'fs';

moduleAlias.addAliases({
'@api': __dirname + '/api',
Expand All @@ -8,3 +9,24 @@ moduleAlias.addAliases({
});

dotenv.config();

const envPath = process.env.GUARDIAN_ENV ? `./configs/.env.auth.${process.env.GUARDIAN_ENV}` : './configs/.env.auth';

if (!process.env.OVERRIDE || process.env.OVERRIDE === 'false'){
console.log('reading from', envPath, 'not overriding');
dotenv.config({ path: envPath});
}else{
try {
const envConfig = dotenv.parse(fs.readFileSync(envPath));
for (const k of Object.keys(envConfig)) {
process.env[k] = envConfig[k]
}
} catch (err) {
if (err.code === 'ENOENT') {
console.log('WARN: Specific environment not loaded');
} else {
throw err;
}
}
}
console.log('Charged Environment',process.env,'\r\n___ . ___');
6 changes: 3 additions & 3 deletions common/src/helpers/db-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { GridFSBucket } from 'mongodb';
export const COMMON_CONNECTION_CONFIG: any = {
type: 'mongo',
namingStrategy: DataBaseNamingStrategy,
dbName: (process.env.ENV||(process.env.HEDERA_NET!==process.env.PREUSED_HEDERA_NET))?
`${process.env.ENV}_${process.env.HEDERA_NET}_${process.env.DB_DATABASE}`:
process.env.DB_DATABASE,
dbName: (process.env.GUARDIAN_ENV||(process.env.HEDERA_NET!==process.env.PREUSED_HEDERA_NET))?
`${process.env.GUARDIAN_ENV}_${process.env.HEDERA_NET}_${process.env.DB_DATABASE}`:
process.env.DB_DATABASE,
clientUrl:`mongodb://${process.env.DB_HOST}`,
entities: [
'dist/entity/*.js'
Expand Down
Loading

0 comments on commit a1e81a0

Please sign in to comment.