|
1 |
| -# skills-api |
2 |
| -V5 Skills API |
| 1 | +# Skills API |
| 2 | + |
| 3 | +* [Prerequisites](#prerequisites) |
| 4 | +* [Configuration](#configuration) |
| 5 | +* [Local deployment](#local-deployment) |
| 6 | +* [Migrations](#migrations) |
| 7 | +* [Local Deployment with Docker](#local-deployment-with-docker) |
| 8 | +* [NPM Commands](#npm-commands) |
| 9 | +* [JWT Authentication](#jwt-authentication) |
| 10 | +* [Documentation](#documentation) |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- node 12.x+ |
| 15 | +- npm 6.x+ |
| 16 | +- docker |
| 17 | +- elasticsearch 7.7+ |
| 18 | +- PostgreSQL |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +Configuration for the application is at `config/default.js` and `config/production.js`. The following parameters can be set in config files or in env variables: |
| 23 | + |
| 24 | +- LOG_LEVEL: the log level |
| 25 | +- PORT: the server port |
| 26 | +- AUTH_SECRET: TC Authentication secret |
| 27 | +- VALID_ISSUERS: valid issuers for TC authentication |
| 28 | +- PAGE_SIZE: the default pagination limit |
| 29 | +- MAX_PAGE_SIZE: the maximum pagination size |
| 30 | +- API_VERSION: the API version |
| 31 | +- DB_NAME: the database name |
| 32 | +- DB_USERNAME: the database username |
| 33 | +- DB_PASSWORD: the database password |
| 34 | +- DB_HOST: the database host |
| 35 | +- DB_PORT: the database port |
| 36 | +- ES_HOST: Elasticsearch host |
| 37 | +- ES_REFRESH: Should elastic search refresh. Default is 'true'. Values can be 'true', 'wait_for', 'false' |
| 38 | +- ELASTICCLOUD_ID: The elastic cloud id, if your elasticsearch instance is hosted on elastic cloud. DO NOT provide a value for ES_HOST if you are using this |
| 39 | +- ELASTICCLOUD_USERNAME: The elastic cloud username for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud |
| 40 | +- ELASTICCLOUD_PASSWORD: The elastic cloud password for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud |
| 41 | +- ES.DOCUMENTS: Elasticsearch index, type and id mapping for resources. |
| 42 | +- SKILL_INDEX: The Elastic search index for skill. Default is `skill` |
| 43 | +- SKILL_ENRICH_POLICYNAME: The enrich policy for skill. Default is `skill-policy` |
| 44 | +- TAXONOMY_INDEX: The Elastic search index for taxonomy. Default is `taxonomy` |
| 45 | +- TAXONOMY_PIPELINE_ID: The pipeline id for enrichment with taxonomy. Default is `taxonomy-pipeline` |
| 46 | +- TAXONOMY_ENRICH_POLICYNAME: The enrich policy for taxonomy. Default is `taxonomy-policy` |
| 47 | +- MAX_BATCH_SIZE: Restrict number of records in memory during bulk insert (Used by the db to es migration script) |
| 48 | +- MAX_BULK_SIZE: The Bulk Indexing Maximum Limits. Default is `100` (Used by the db to es migration script) |
| 49 | + |
| 50 | + |
| 51 | +## Local deployment |
| 52 | + |
| 53 | +Setup your Postgresql DB and Elasticsearch instance and ensure that they are up and running. |
| 54 | + |
| 55 | +- Follow *Configuration* section to update config values, like database, ES host etc .. |
| 56 | +- Goto *skills-api*, run `npm i` |
| 57 | +- Create database using `npm run create-db`. |
| 58 | +- Run the migrations - `npm run migrations up`. This will create the tables. |
| 59 | +- Then run `npm run insert-data` and insert mock data into the database. |
| 60 | +- Run `npm run migrate-db-to-es` to sync data with ES. |
| 61 | +- Startup server `npm run start` |
| 62 | + |
| 63 | +## Migrations |
| 64 | + |
| 65 | +Migrations are located under the `./scripts/db/` folder. Run `npm run migrations up` and `npm run migrations down` to execute the migrations or remove the earlier ones |
| 66 | + |
| 67 | +## Local Deployment with Docker |
| 68 | + |
| 69 | +- Navigate to the directory `docker-pgsql-es` folder. Rename `sample.env` to `.env` and change any values if required. |
| 70 | +- Run `docker-compose up -d` to have docker instances of pgsql and elasticsearch to use with the api |
| 71 | + |
| 72 | +- Create database using `npm run create-db`. |
| 73 | +- Run the migrations - `npm run migrations up`. This will create the tables. |
| 74 | +- Then run `npm run insert-data` and insert mock data into the database. |
| 75 | +- Run `npm run migrate-db-to-es` to sync data with ES. |
| 76 | + |
| 77 | +- Navigate to the directory `docker` |
| 78 | + |
| 79 | +- Rename the file `sample.env` to `.env` |
| 80 | + |
| 81 | +- Set the required DB configurations and ElasticSearch host in the file `.env` |
| 82 | + |
| 83 | +- Once that is done, run the following command |
| 84 | + |
| 85 | + ```bash |
| 86 | + docker-compose up |
| 87 | + ``` |
| 88 | + |
| 89 | +- When you are running the application for the first time, It will take some time initially to download the image and install the dependencies |
| 90 | + |
| 91 | +## NPM Commands |
| 92 | + |
| 93 | +| Command | Description | |
| 94 | +|--------------------|--| |
| 95 | +| `npm run start` | Start app | |
| 96 | +| `npm run start:dev` | Start app on any changes (useful during development). | |
| 97 | +| `npm run lint` | Check for for lint errors. | |
| 98 | +| `npm run lint:fix` | Check for for lint errors and fix error automatically when possible. | |
| 99 | +| `npm run create-db` | Create the database | |
| 100 | +| `npm run insert-data` | Insert data into the database | |
| 101 | +| `npm run migrate-db-to-es` | Migrate data into elastic search from database | |
| 102 | +| `npm run delete-data` | Delete the data from the database | |
| 103 | +| `npm run migrations up` | Run up migration | |
| 104 | +| `npm run migrations down` | Run down migration | |
| 105 | +| `npm run generate:doc:permissions` | Generate [permissions.html](docs/permissions.html) | |
| 106 | +| `npm run generate:doc:permissions:dev` | Generate [permissions.html](docs/permissions.html) on any changes (useful during development). | |
| 107 | + |
| 108 | +## JWT Authentication |
| 109 | +Authentication is handled via Authorization (Bearer) token header field. Token is a JWT token. |
| 110 | + |
| 111 | +Here is a sample user token that is valid for a very long time for a user with administrator role. |
| 112 | + |
| 113 | +``` |
| 114 | +<provide_in_forums> |
| 115 | + |
| 116 | +# here is the payload data decoded from the token |
| 117 | +{ |
| 118 | + "roles": [ |
| 119 | + "Topcoder User", |
| 120 | + "administrator" |
| 121 | + ], |
| 122 | + "iss": "https://api.topcoder.com", |
| 123 | + "handle": "tc-Admin", |
| 124 | + "exp": 1685571460, |
| 125 | + "userId": "23166768", |
| 126 | + "iat": 1585570860, |
| 127 | + |
| 128 | + "jti": "0f1ef1d3-2b33-4900-bb43-48f2285f9630" |
| 129 | +} |
| 130 | +``` |
| 131 | +
|
| 132 | +and this is a sample M2M token with scopes `all:connect_project`, `all:projects` and `write:projects`. |
| 133 | +
|
| 134 | +``` |
| 135 | +<provided_in_forums> |
| 136 | + |
| 137 | +# here is the payload data decoded from the token |
| 138 | +{ |
| 139 | + "iss": "https://topcoder-dev.auth0.com/", |
| 140 | + "sub": "enjw1810eDz3XTwSO2Rn2Y9cQTrspn3B@clients", |
| 141 | + "aud": "https://m2m.topcoder-dev.com/", |
| 142 | + "iat": 1550906388, |
| 143 | + "exp": 2147483648, |
| 144 | + "azp": "enjw1810eDz3XTwSO2Rn2Y9cQTrspn3B", |
| 145 | + "scope": "all:connect_project all:projects write:projects", |
| 146 | + "gty": "client-credentials" |
| 147 | +} |
| 148 | +``` |
| 149 | +
|
| 150 | +These tokens have been signed with the secret `CLIENT_SECRET`. This secret should match the `AUTH_SECRET` entry in `config/default.js`. You can modify the payload of these tokens to generate tokens with different roles or different scopes using https://jwt.io |
| 151 | +
|
| 152 | +**Note** Please check with `src/constants.js` for all available user roles and M2M scopes. |
| 153 | +
|
| 154 | +## Documentation |
| 155 | +
|
| 156 | +- [permissions.html](docs/permissions.html) - the list of all permissions in Skills API. |
| 157 | +- [swagger.yaml](docs/swagger.yaml) - the Swagger API Definition. |
0 commit comments