From ccb1838bb36ccd48b44a10d4b2c7e4a2b905da55 Mon Sep 17 00:00:00 2001 From: pabloFuente Date: Wed, 29 Mar 2023 14:10:29 +0200 Subject: [PATCH] openvidu-basic-ruby --- openvidu-basic-ruby/.gitignore | 56 ++++++++++++++++++++++++++++ openvidu-basic-ruby/Gemfile | 6 +++ openvidu-basic-ruby/Gemfile.lock | 33 ++++++++++++++++ openvidu-basic-ruby/README.md | 27 +++++++++++++- openvidu-basic-ruby/app.rb | 64 ++++++++++++++++++++++++++++++++ openvidu-basic-ruby/env.rb | 3 ++ 6 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 openvidu-basic-ruby/.gitignore create mode 100644 openvidu-basic-ruby/Gemfile create mode 100644 openvidu-basic-ruby/Gemfile.lock create mode 100644 openvidu-basic-ruby/app.rb create mode 100644 openvidu-basic-ruby/env.rb diff --git a/openvidu-basic-ruby/.gitignore b/openvidu-basic-ruby/.gitignore new file mode 100644 index 00000000..33c0f8ad --- /dev/null +++ b/openvidu-basic-ruby/.gitignore @@ -0,0 +1,56 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +# Ignore Byebug command history file. +.byebug_history + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Used by RuboCop. Remote config files pulled in from inherit_from directive. +# .rubocop-https?--* \ No newline at end of file diff --git a/openvidu-basic-ruby/Gemfile b/openvidu-basic-ruby/Gemfile new file mode 100644 index 00000000..a56e2bcb --- /dev/null +++ b/openvidu-basic-ruby/Gemfile @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +gem 'sinatra' +gem 'sinatra-cors' +gem 'faraday' +gem 'json' \ No newline at end of file diff --git a/openvidu-basic-ruby/Gemfile.lock b/openvidu-basic-ruby/Gemfile.lock new file mode 100644 index 00000000..98fc9712 --- /dev/null +++ b/openvidu-basic-ruby/Gemfile.lock @@ -0,0 +1,33 @@ +GEM + remote: https://rubygems.org/ + specs: + faraday (2.7.4) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.2) + json (2.6.3) + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) + rack (2.2.6.4) + rack-protection (3.0.5) + rack + ruby2_keywords (0.0.5) + sinatra (3.0.5) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.0.5) + tilt (~> 2.0) + sinatra-cors (1.2.0) + tilt (2.1.0) + +PLATFORMS + x86_64-linux + +DEPENDENCIES + faraday + json + sinatra + sinatra-cors + +BUNDLED WITH + 2.4.10 diff --git a/openvidu-basic-ruby/README.md b/openvidu-basic-ruby/README.md index dc005dc0..fc9c178e 100644 --- a/openvidu-basic-ruby/README.md +++ b/openvidu-basic-ruby/README.md @@ -1,3 +1,28 @@ # openvidu-basic-ruby -Coming soon... \ No newline at end of file +This is a minimal OpenVidu server application sample built for Ruby with [Sinatra](https://sinatrarb.com/) and [Faraday](https://lostisland.github.io/faraday/). Visit [Application server](https://docs.openvidu.io/en/stable/application-server/) documentation for further context. + +## Prerequisites + +- [Ruby](https://www.ruby-lang.org/en/downloads/) + +## Run + +Download repository + +``` +git clone git@github.com:OpenVidu/openvidu-tutorials.git +cd openvidu-tutorials/openvidu-basic-ruby +``` + +Install gems + +``` +bundle install +``` + +Run the application + +``` +ruby app.rb +``` \ No newline at end of file diff --git a/openvidu-basic-ruby/app.rb b/openvidu-basic-ruby/app.rb new file mode 100644 index 00000000..2707a9ad --- /dev/null +++ b/openvidu-basic-ruby/app.rb @@ -0,0 +1,64 @@ +#!/usr/bin/ruby + +require 'sinatra' +require 'sinatra/cors' +require 'faraday' +require 'json' +require './env.rb' + +# Load env variables +SERVER_PORT = ENV['SERVER_PORT'] +OPENVIDU_URL = ENV['OPENVIDU_URL'] +OPENVIDU_SECRET = ENV['OPENVIDU_SECRET'] + +set :port, SERVER_PORT + +register Sinatra::Cors +set :allow_origin, "*" +set :allow_methods, "POST,OPTIONS" +set :allow_headers, "content-type" + +post '/api/sessions' do + begin + body = request.body.read + response = Faraday.post do |req| + req.url "#{OPENVIDU_URL}openvidu/api/sessions" + req.headers['Content-Type'] = 'application/json' + req.headers['Authorization'] = "Basic #{Base64.encode64("OPENVIDUAPP:#{OPENVIDU_SECRET}").strip}" + req.body = body + end + if response.success? + (JSON.parse response.body)['sessionId'] + else + if response.status == 409 + # Session already exists in OpenVidu + (JSON.parse body)['customSessionId'] + else + status response.status + body response.body + end + end + rescue Faraday::Error => err + err.response + end +end + +post '/api/sessions/:sessionId/connections' do + begin + body = request.body.read + response = Faraday.post do |req| + req.url "#{OPENVIDU_URL}openvidu/api/sessions/#{params['sessionId']}/connection" + req.headers['Content-Type'] = 'application/json' + req.headers['Authorization'] = "Basic #{Base64.encode64("OPENVIDUAPP:#{OPENVIDU_SECRET}").strip}" + req.body = body + end + if response.success? + (JSON.parse response.body)['token'] + else + status response.status + body response.body + end + rescue Faraday::Error => err + err.response + end +end \ No newline at end of file diff --git a/openvidu-basic-ruby/env.rb b/openvidu-basic-ruby/env.rb new file mode 100644 index 00000000..2e720793 --- /dev/null +++ b/openvidu-basic-ruby/env.rb @@ -0,0 +1,3 @@ +ENV['SERVER_PORT'] = '5000' +ENV['OPENVIDU_URL'] = 'http://localhost:4443/' +ENV['OPENVIDU_SECRET'] = 'MY_SECRET' \ No newline at end of file