#!/usr/bin/env node

/**
 * An HTTP server to serve classification requests
 *
 * Usage:
 * % export MAPBOX_ACCESS_TOKEN="pk.foo" ./server.js
 * Routes:
 * GET /classify?tile=z/x/y
 *
 * NOTES:
 * - should run inside rodowi/inception_serving container
 * - listens in port 3000
 *
 */

const execFile = require('child_process').execFile
const express = require('express')
const app = express()

const port = 3000
const url = 'https://b.tiles.mapbox.com/v4/mapbox.satellite'
const token = process.env.MAPBOX_ACCESS_TOKEN

app.get('/', function (req, res) {
  var zxy = req.query.tile
  download(zxy, (err, filename) => {
    if (err) return res.send(err)
    predict(filename, (err, label) => {
      if (err) return res.send(err)
      res.send('CAT\n')
    })
  })
})

function download (tile, done) {
  var filename = `${process.env.PWD}/imagery/${tile.replace(/\//g, '-')}.jpg`
  var command = `curl ${url}/${tile}@2x.png?access_token=${token} > ${filename}`
  command = 'curl'
  var params = [
    `${url}/${tile}@2x.png?access_token=${token}`,
    '-o',
    filename
  ]
  execFile(command, params, (err, stdout, stderr) => {
    done(err, filename)
  })
}

function predict (filename, done) {
  var command = '/serving/bazel-bin/tensorflow_serving/example' +
    '/inception_client --server=localhost:9000 ' +
    '--image=' + filename
  execFile(`${command}`, (err, stdout, stderr) => {
    done(err, stderr + stdout)
  })
}

app.listen(port)
console.log('Listening for tile requests in port', port)

@@ -893,85 +893,8 @@
 nd =
- %60curl $%7Burl%7D/$%7Btile%7D@2x.png?access_token=$%7Btoken%7D %3E $%7Bfilename%7D%60%0A  command =
  'cu
@@ -1118,29 +1118,25 @@
 e) %7B%0A  var c
-omman
+m
 d = '/servin
@@ -1173,25 +1173,16 @@
 /example
-' +%0A    '
 /incepti
@@ -1190,17 +1190,40 @@
 n_client
- 
+'%0A  var params = %5B%0A    '
 --server
@@ -1241,12 +1241,10 @@
 9000
- ' +
+',
 %0A   
@@ -1266,16 +1266,20 @@
 ilename%0A
+  %5D%0A
   execFi
@@ -1285,20 +1285,19 @@
 ile(
-%60$%7Bcommand%7D%60
+cmd, params
 , (e

Fix execFile invocation