require('dotenv').config();
var https = require('https');
var app = require('express')();
var ringcentral = require('ringcentral');

// Configure Mustache
var mustacheExpress = require('mustache-express');
app.engine('mustache', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');

// Start HTTP server
var server = null;
var port = process.env.MY_APP_PORT;
var useTls = process.env.MY_APP_TLS_ENABLED > 0 ? true : false;

if (useTls) {
  var tls = require('tls'),
      fs = require('fs');
  server = https.createServer({
    key: fs.readFileSync(process.env.MY_APP_TLS_PRIVATE_KEY),
    cert: fs.readFileSync(process.env.MY_APP_TLS_PUBLIC_CERT)
  }, app).listen(port, function() {
    console.log('LISTEN_HTTPS ' + port);    
  });
} else {
  server = require('http').Server(app);
  server.listen(port, function() {
    console.log('LISTEN_HTTP ' + port);    
  });
}

// Start RingCentral SDK
var rcsdk = new ringcentral({
  server: process.env.RC_APP_SERVER_URL,
  appKey: process.env.RC_APP_KEY,
  appSecret: process.env.RC_APP_SECRET
});

app.get('/', function(req, res) {
  // Get token for display after OAuth
  token = rcsdk.platform().auth().data();
  token_json = token['access_token'] ? JSON.stringify(token, null, ' ') : '';

  // Render home page with params
  res.render('index', {
    authorize_uri: rcsdk.platform().authUrl({
      redirectUri: process.env.RC_APP_REDIRECT_URL
    }),
    redirect_uri: process.env.RC_APP_REDIRECT_URL,
    token_json: token_json
  });
});

app.get('/callback', function(req, res) {
  if (req.query.code) {
    rcsdk.platform()
      .login({
        code: req.query.code,
        redirectUri: process.env.RC_APP_REDIRECT_URL
      })
      .then(function(response) {
        console.log('logged_in');
        res.send('');
      })
      .catch(function(e) {
        console.log('ERR ' + e.message  || 'Server cannot authorize user');
        res.send('');
      });
  } else {
    res.send('');
  }
});

@@ -25,38 +25,8 @@
 ();%0A
-var https = require('https');%0A
 var 
@@ -450,18 +450,17 @@
 %7B%0A  var 
-tl
+f
 s = requ
@@ -468,23 +468,22 @@
 re('
-tls'),%0A      fs
+fs');%0A  server
  = r
@@ -494,30 +494,20 @@
 re('
-f
+http
 s')
-;
 %0A  
-server = https
+  
 .cre
@@ -518,16 +518,18 @@
 erver(%7B%0A
+  
     key:
@@ -582,16 +582,18 @@
 E_KEY),%0A
+  
     cert
@@ -646,16 +646,18 @@
 C_CERT)%0A
+  
   %7D, app
@@ -657,16 +657,21 @@
  %7D, app)
+%0A    
 .listen(
@@ -685,32 +685,34 @@
 unction() %7B%0A    
+  
 console.log('LIS
@@ -732,24 +732,26 @@
  port);    %0A
+  
   %7D);%0A%7D else
@@ -779,16 +779,21 @@
 ('http')
+%0A    
 .Server(
@@ -796,26 +796,21 @@
 ver(app)
-;
 %0A  
-server
+  
 .listen(
@@ -824,24 +824,26 @@
 unction() %7B%0A
+  
     console.
@@ -874,16 +874,18 @@
 t);    %0A
+  
   %7D);%0A%7D%0A

update style