<commit_before>express = require 'express'
meshbluAuth = require 'express-meshblu-auth'
MeshbluAuthExpress = require 'express-meshblu-auth/src/meshblu-auth-express'
meshbluHealthcheck = require 'express-meshblu-healthcheck'
TriggerController = require './trigger-controller'
cors = require 'cors'
bodyParser = require 'body-parser'

MESHBLU_HOST          = process.env.MESHBLU_HOST || 'meshblu.octoblu.com'
MESHBLU_PORT          = process.env.MESHBLU_PORT || '443'
MESHBLU_PROTOCOL      = process.env.MESHBLU_PROTOCOL || 'https'
TRIGGER_SERVICE_PORT  = process.env.TRIGGER_SERVICE_PORT || 80
TRIGGER_SERVICE_UUID  = process.env.TRIGGER_SERVICE_UUID
TRIGGER_SERVICE_TOKEN = process.env.TRIGGER_SERVICE_TOKEN

triggerController = new TriggerController
  server: MESHBLU_HOST
  port: MESHBLU_PORT
  protocol: MESHBLU_PROTOCOL

app = express()
app.use cors()
app.use meshbluHealthcheck()
app.use bodyParser.urlencoded limit: '50mb', extended : true
app.use bodyParser.json limit : '50mb'

meshbluOptions =
  server: MESHBLU_HOST
  port: MESHBLU_PORT
  protocol: MESHBLU_PROTOCOL

app.use '/triggers', meshbluAuth meshbluOptions
app.use '/mytriggers', meshbluAuth meshbluOptions

app.use '/flows/:flowId/triggers/:triggerId', (request, response, next) ->
  meshbluAuthExpress = new MeshbluAuthExpress meshbluOptions
  meshbluAuthExpress.getFromAnywhere request

  defaultAuth =
    uuid: TRIGGER_SERVICE_UUID
    token: TRIGGER_SERVICE_TOKEN

  {uuid, token} = request.meshbluAuth ? defaultAuth
  return response.status(401).end() unless uuid? && token?
  meshbluAuthExpress.authDeviceWithMeshblu uuid, token, (error) ->
    if error?
      console.error error
      return response.status(401).end()
    next()

app.options '*', cors()

app.get '/triggers', triggerController.getTriggers
app.get '/mytriggers', triggerController.getMyTriggers

app.post '/flows/:flowId/triggers/:triggerId', triggerController.trigger

app.get '/flows/:flowId/triggers/:triggerId', (request, response) ->
  response.status(405).send('Method Not Allowed: POST required')

server = app.listen TRIGGER_SERVICE_PORT, ->
  host = server.address().address
  port = server.address().port

  console.log "Server running on #{host}:#{port}"<commit_msg>Make triggers return only my triggers<commit_after>express = require 'express'
meshbluAuth = require 'express-meshblu-auth'
MeshbluAuthExpress = require 'express-meshblu-auth/src/meshblu-auth-express'
meshbluHealthcheck = require 'express-meshblu-healthcheck'
TriggerController = require './trigger-controller'
cors = require 'cors'
bodyParser = require 'body-parser'

MESHBLU_HOST          = process.env.MESHBLU_HOST || 'meshblu.octoblu.com'
MESHBLU_PORT          = process.env.MESHBLU_PORT || '443'
MESHBLU_PROTOCOL      = process.env.MESHBLU_PROTOCOL || 'https'
TRIGGER_SERVICE_PORT  = process.env.TRIGGER_SERVICE_PORT || 80
TRIGGER_SERVICE_UUID  = process.env.TRIGGER_SERVICE_UUID
TRIGGER_SERVICE_TOKEN = process.env.TRIGGER_SERVICE_TOKEN

triggerController = new TriggerController
  server: MESHBLU_HOST
  port: MESHBLU_PORT
  protocol: MESHBLU_PROTOCOL

app = express()
app.use cors()
app.use meshbluHealthcheck()
app.use bodyParser.urlencoded limit: '50mb', extended : true
app.use bodyParser.json limit : '50mb'

meshbluOptions =
  server: MESHBLU_HOST
  port: MESHBLU_PORT
  protocol: MESHBLU_PROTOCOL

app.use '/triggers', meshbluAuth meshbluOptions
app.use '/mytriggers', meshbluAuth meshbluOptions

app.use '/flows/:flowId/triggers/:triggerId', (request, response, next) ->
  meshbluAuthExpress = new MeshbluAuthExpress meshbluOptions
  meshbluAuthExpress.getFromAnywhere request

  defaultAuth =
    uuid: TRIGGER_SERVICE_UUID
    token: TRIGGER_SERVICE_TOKEN

  {uuid, token} = request.meshbluAuth ? defaultAuth
  return response.status(401).end() unless uuid? && token?
  meshbluAuthExpress.authDeviceWithMeshblu uuid, token, (error) ->
    if error?
      console.error error
      return response.status(401).end()
    next()

app.options '*', cors()

app.get '/triggers', triggerController.getMyTriggers
app.get '/mytriggers', triggerController.getMyTriggers

app.post '/flows/:flowId/triggers/:triggerId', triggerController.trigger

app.get '/flows/:flowId/triggers/:triggerId', (request, response) ->
  response.status(405).send('Method Not Allowed: POST required')

server = app.listen TRIGGER_SERVICE_PORT, ->
  host = server.address().address
  port = server.address().port

  console.log "Server running on #{host}:#{port}"
{'fraction_non_alphanumeric': 0.10922112802148612, 'fraction_numerical': 0.008057296329453895, 'mean_word_length': 5.172651933701657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1819678', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1538, 'n_words': 294}