0
0

More than 3 years have passed since last update.

azure app service で kintone アプリデータ公開その2(環境変数利用)

Last updated at Posted at 2020-03-02

kintone のアプリデータをazure app service で公開その2です。
kintone への接続情報を環境変数で管理してみます。

概要

azure app service で kintone アプリデータ公開その1の続きです。
kintone 接続用の情報を環境変数に設定して、利用してみます。

環境変数の参考情報

npm dotenv を使うとよさそうです。

環境変数の準備

  • dotenv をインストール
console.log
$ npm install dotenv --save
  • kintone への接続情報を .env ファイルにまとめておきます。
# kintone connection
kbrowser_domainName=xxxxxxxxx.cybozu.com
kbrowser_basicUserName=
kbrowser_basicPassword=
kbrowser_userName=xxxxxxxxx
kbrowser_userPassword=xxxxxxxxx

環境変数の利用方法

app.js に require('dotenv').config(); を追加すると、他の js でも process.env.kbrowser_????? が利用できるようになります。

  • app.js の変更
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
require('dotenv').config();
...
  • kintoneConnections.js 追加

環境変数のkintone接続情報で、kintoneConnection を設定します。

kintoneConnections.js
const kintone = require('@kintone/kintone-js-sdk');

const kintoneAuth = new kintone.Auth();

if (process.env.kbrowser_basicUserName) {
    const basicUserName = process.env.kbrowser_basicUserName
    const basicPassword = process.env.kbrowser_basicPassword
    kintoneAuth.setBasicAuth({ username: basicUserName, password: basicPassword });
}

const username = process.env.kbrowser_userName;
const password = process.env.kbrowser_userPassword;
kintoneAuth.setPasswordAuth({ username: username, password: password });    

const domainName = process.env.kbrowser_domainName;
const kintoneConnection = new kintone.Connection({ domain: domainName, auth: kintoneAuth });

module.exports = kintoneConnection;
  • customer.js で kintoneConnection.js を利用するように変更します。

まだ、アプリの appID がリテラルです。
アプリ数が増えると変数管理が面倒なので、これも環境変数に持っていくかどうか検討要です。

customer.js
var express = require('express');
var router = express.Router();
const kintone = require('@kintone/kintone-js-sdk');
const kintoneConnection = require('./kintoneConnection');

/* GET customers listing. */
router.get('/', function (req, res, next) {

    const kintoneRecord = new kintone.Record({ connection: kintoneConnection });

    const finfos = [ 
        { label: '会社名', fcode: '会社名'},
        { label: '担当者名', fcode: '担当者名' },
        { label: 'メールアドレス', fcode: 'メールアドレス' }
    ];
    const fields = finfos.map((finfo) => {
        return finfo.fcode;
    });

    const appId = 549; // target appID
    const parm = {
        app: appId,
        query: 'order by $id',
        fields: fields
    };

    kintoneRecord.getRecords(parm).then((rsp) => {
        console.log('rsp', rsp);
        res.render('customer', {
            title: '顧客一覧',
            finfos: finfos,
            records: rsp.records
        });

    }).catch((err) => {
        // The promise function always reject with KintoneAPIExeption
        console.log(err);
        // res.render('forum', { title: 'rex0220 forum', user: 'error' });
    });

});

module.exports = router;

azure app service の環境変数

app service の構成で、環境変数を設定できます。

2020-03-02_11h15_42.png

vscode によるazure app service の環境変数の管理

Application Setting で、azure app service の環境変数をダウンロード・アップロードできます。
ローカルPCの環境変数ファイル「.env」と別に、azure の環境変数ファイル「.azure_prod_env」などで管理できます。

vscode の Application Setting で、右クリックするとダウンロード・アップロードのメニューが表示されます。

2020-03-02_11h28_49.png

次にやること

あとがき

kintone 接続情報を環境変数で管理出来ましたが、アプリIDやAPITokenの情報も環境変数で管理すると対象のアプリ数が増えると管理・運用が大変そうです。
個別のアプリ情報は、できれば kintone アプリで管理したいと思いますが、kintone の API 回数制限などが課題です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0