Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #84 from watson-developer-cloud/up
Browse files Browse the repository at this point in the history
fix: Update dependencies
  • Loading branch information
germanattanasio authored Nov 7, 2019
2 parents 4347ef3 + 2ffc4e8 commit 8c46613
Show file tree
Hide file tree
Showing 14 changed files with 1,047 additions and 3,904 deletions.
8 changes: 2 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# see https://console.bluemix.net/docs/services/watson/getting-started-credentials.html#service-credentials-for-watson-services
LANGUAGE_TRANSLATOR_USERNAME=<username>
LANGUAGE_TRANSLATOR_PASSWORD=<password>

LANGUAGE_TRANSLATOR_IAM_APIKEY=<password>

# see https://cloud.ibm.com/docs/services/watson?topic=watson-iam
LANGUAGE_TRANSLATOR_URL=https://gateway.watsonplatform.net/language-translator/api
LANGUAGE_TRANSLATOR_IAM_APIKEY=
35 changes: 16 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
language: node_js
sudo: true
node_js: 8
node_js: 12
script:
- npm run lint
- npm run codecov
- npm run lint
- npm run codecov
env:
global:
- BX_APP=language-translator-demo
- BX_API=https://api.ng.bluemix.net
- BX_ORGANIZATION=WatsonPlatformServices
- BX_SPACE=demos
- BX_APP=language-translator-demo
- BX_API=https://api.ng.bluemix.net
- BX_ORGANIZATION=WatsonPlatformServices
- BX_SPACE=demos
before_deploy: npm install -g bx-blue-green
deploy:
- provider: script
script:
- bx-blue-green-travis
on:
branch: master
repo: watson-developer-cloud/language-translator-nodejs
skip_cleanup: true
- provider: script
skip_cleanup: true
script: npx semantic-release
on:
node: 8
- provider: script
script: bx-blue-green-travis
on:
branch: master
repo: watson-developer-cloud/language-translator-nodejs
skip_cleanup: true
- provider: script
skip_cleanup: true
script: npx semantic-release
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Give it a try! Click the button below to fork into IBM DevOps Services and deplo

[![Greenkeeper badge](https://badges.greenkeeper.io/watson-developer-cloud/language-translator-nodejs.svg)](https://greenkeeper.io/)

1. Sign up for an [IBM Cloud account](https://console.bluemix.net/registration/).
1. Download the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview).
1. Sign up for an [IBM Cloud account](https://cloud.ibm.com/registration/).
1. Download the [IBM Cloud CLI](https://cloud.ibm.com/docs/cli/index.html#overview).
1. Create an instance of the Language Translator service and get your credentials:
- Go to the [Language Translator](https://console.bluemix.net/catalog/services/language-translator) page in the IBM Cloud Catalog.
- Go to the [Language Translator](https://cloud.ibm.com/catalog/services/language-translator) page in the IBM Cloud Catalog.
- Log in to your IBM Cloud account.
- Click **Create**.
- Click **Show** to view the service credentials.
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
- Copy the `apikey` value.
- Copy the `url` value.

## Configuring the application
Expand All @@ -51,15 +51,6 @@ Give it a try! Click the button below to fork into IBM DevOps Services and deplo
LANGUAGE_TRANSLATOR_URL=https://gateway-wdc.watsonplatform.net/language-translator/api
```
- If your service instance uses `username` and `password` credentials, add the `LANGUAGE_TRANSLATOR_USERNAME` and `LANGUAGE_TRANSLATOR_PASSWORD` variables to the *.env* file.
Example *.env* file that configures the `username`, `password`, and `url` for a Language Translator service instance hosted in the Sydney region:
```
LANGUAGE_TRANSLATOR_USERNAME=522be-7b41-ab44-dec3-g1eab2ha73c6
LANGUAGE_TRANSLATOR_PASSWORD=A4Z5BdGENrwu8
LANGUAGE_TRANSLATOR_URL=https://gateway-syd.watsonplatform.net/language-translator/api
```
## Running locally
1. Install the dependencies
Expand All @@ -78,7 +69,7 @@ Give it a try! Click the button below to fork into IBM DevOps Services and deplo
## Deploying to IBM Cloud as a Cloud Foundry Application
1. Login to IBM Cloud with the [IBM Cloud CLI](https://console.bluemix.net/docs/cli/index.html#overview)
1. Login to IBM Cloud with the [IBM Cloud CLI](https://cloud.ibm.com/docs/cli/index.html#overview)
```
ibmcloud login
Expand Down Expand Up @@ -117,4 +108,4 @@ Find more open source projects on the
[IBM Github Page](http://ibm.github.io/).
[service_url]: https://www.ibm.com/watson/services/language-translator/
[docs]: https://console.bluemix.net/docs/services/language-translator/index.html#about
[docs]: https://cloud.ibm.com/docs/services/language-translator?topic=language-translator-about#about
41 changes: 23 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@

'use strict';

var express = require('express');
var app = express();
var LanguageTranslatorV3 = require('watson-developer-cloud/language-translator/v3');
const express = require('express');
const app = express();
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const { IamAuthenticator } = require('ibm-watson/auth');

// Bootstrap application settings
require('./config/express')(app);

var translator = new LanguageTranslatorV3({
// If unspecified here, the LANGUAGE_TRANSLATOR_USERNAME and LANGUAGE_TRANSLATOR_PASSWORD environment properties will be checked
// LANGUAGE_TRANSLATOR_IAM_APIKEY if apikey is present
// After that, the SDK will fall back to the ibm-cloud-provided VCAP_SERVICES environment property
// username: '<username>',
// password: '<password>'
version: '2018-05-01',
// Create the service wrapper
const translator = new LanguageTranslatorV3({
version: '2019-10-10',
authenticator: new IamAuthenticator({
apikey: process.env.LANGUAGE_TRANSLATOR_IAM_APIKEY,
}),
url: process.env.LANGUAGE_TRANSLATOR_URL,
headers: {
'X-Watson-Technology-Preview': '2018-05-01',
'X-Watson-Learning-Opt-Out': true,
Expand All @@ -47,29 +48,33 @@ app.get('/', function(req, res) {

app.get('/api/models', function(req, res, next) {
console.log('/v3/models');
translator.listModels()
.then(models => res.json(models))
translator
.listModels()
.then(({ result }) => res.json(result))
.catch(error => next(error));
});

app.post('/api/identify', function(req, res, next) {
console.log('/v3/identify');
translator.identify(req.body)
.then(models => res.json(models))
translator
.identify(req.body)
.then(({ result }) => res.json(result))
.catch(error => next(error));
});

app.get('/api/identifiable_languages', function(req, res, next) {
console.log('/v3/identifiable_languages');
translator.listIdentifiableLanguages(req.body)
.then(models => res.json(models))
translator
.listIdentifiableLanguages(req.body)
.then(({ result }) => res.json(result))
.catch(error => next(error));
});

app.post('/api/translate', function(req, res, next) {
console.log('/v3/translate');
translator.translate(req.body)
.then(models => res.json(models))
translator
.translate(req.body)
.then(({ result }) => res.json(result))
.catch(error => next(error));
});

Expand Down
2 changes: 1 addition & 1 deletion config/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = function(app) {
app.use(function(err, req, res, next) {
var error = {
code: err.code || 500,
error: err.message || err.error
error: err.message || err.error,
};
console.log('error:', error);
res.status(error.code).json(error);
Expand Down
9 changes: 4 additions & 5 deletions config/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
'use strict';

// Module dependencies
var express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser');
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');

module.exports = function (app) {
module.exports = function(app) {
// Configure Express
app.set('view engine', 'ejs');
app.set('views', __dirname + '/../views');
Expand All @@ -38,5 +38,4 @@ module.exports = function (app) {

// Setup static public directory
app.use(express.static(__dirname + '/../public'));

};
24 changes: 12 additions & 12 deletions config/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@
'use strict';

// security.js
var secure = require('express-secure-only');
var rateLimit = require('express-rate-limit');
var helmet = require('helmet');
const secure = require('express-secure-only');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');

module.exports = function(app) {

// 1. redirects http to https
app.enable('trust proxy'); // required when running on bluemix or similar to know if users originally came in on HTTPS and avoid endless redirects
app.use(secure());
app.use(helmet({
frameguard: false
}));
app.use(
helmet({
frameguard: false,
})
);

// 3. rate limiting
var translateLimiter = rateLimit({
const translateLimiter = rateLimit({
windowMs: 60 * 1000,
max: 10
max: 10,
});
app.use('/api/translate', translateLimiter);

var identifyLimiter = rateLimit({
const identifyLimiter = rateLimit({
windowMs: 60 * 1000,
max: 10
max: 10,
});
app.use('/api/identify', identifyLimiter);

};
6 changes: 0 additions & 6 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
declared-services:
language-translator-service:
label: language_translator
plan: lite
applications:
- services:
- language-translator-service
name: language-translator-demo
command: npm start
path: .
Expand Down
Loading

0 comments on commit 8c46613

Please sign in to comment.