require 'faye'
require 'pry'
#require 'pry'
# started using (with extra server contexts of cours like full paths):
#   thin start -d -R faye/config.ru -p 9296 --pid tmp/pids/faye.pid
# serve nginx config:
#   server {
#     listen 443 ssl http2;
#     listen [::]:443 ssl http2;
#
#   	server_name events.mozo.bar;
#
#     ssl_certificate /etc/letsencrypt/live/mozo.bar/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/mozo.bar/privkey.pem;
#     location / {
#         proxy_pass http://127.0.0.1:9296;
#         proxy_set_header Host $host;
#         proxy_set_header X-Real-IP $remote_addr;
#         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#         proxy_set_header X-Forwarded-Proto https;
#     }
#   }
#
#require File.expand_path('../config/initializers/faye_token.rb', __FILE__)
Process.setproctitle('mozo_faye') if Process.respond_to?(:setproctitle)
VALID_TOKENS = ['6be65f9b5e7d21b8ca8de4ccfad5ba24cf40d440b370af79']
# Comment
class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      message['error'] = 'Invalid authentication token' unless message['ext'] && VALID_TOKENS.include?(message['ext']['auth_token'])
    end
    callback.call(message)
  end
end

Faye::WebSocket.load_adapter('thin')
faye_server = Faye::RackAdapter.new(mount: '/faye', timeout: 45)
#faye_server.listen 9296
faye_server.add_extension(ServerAuth.new)
run faye_server
