From a48ab93b088bee421c53614f441e3e0cee537168 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 9 Mar 2023 23:43:26 -0500 Subject: [PATCH] Added start of an API to return some basic stats of the app --- app/routes/api_v1.rb | 19 +++++++++++++++++++ server.rb | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 app/routes/api_v1.rb diff --git a/app/routes/api_v1.rb b/app/routes/api_v1.rb new file mode 100644 index 0000000..de4bd16 --- /dev/null +++ b/app/routes/api_v1.rb @@ -0,0 +1,19 @@ +class StageManager + class ApiV1Controller + + get '/health' do + json :status => 'success' + end + + get '/channels' do + channels = Channel.all().map!(&:to_hash) + json channels + end + + get '/videos' do + videos = Video.all().map!(&:to_hash) + json videos + end + + end +end \ No newline at end of file diff --git a/server.rb b/server.rb index 1c2b985..0a69e43 100644 --- a/server.rb +++ b/server.rb @@ -2,6 +2,7 @@ require 'logger' require 'sequel' require 'sqlite3' require 'sinatra/base' +require 'sinatra/json' require 'rack/protection' # Load the Sequel timestamps plugin @@ -9,8 +10,7 @@ Sequel::Model.plugin :timestamps # Initialize Sequel gem for database actions DB = Sequel.connect(adapter: $conf.get('database.adapter'), database: $conf.get('database.database')) # Load models -require_relative 'app/models/channel.rb' -require_relative 'app/models/video.rb' +Dir.glob('./app/models/*.rb').each { |f| require f } class StageManager < Sinatra::Base @@my_app = {} @@ -48,6 +48,10 @@ class StageManager < Sinatra::Base class VideoController < StageManager map '/video' end + # API controllers + class ApiV1Controller < StageManager + map '/api/v1' + end end # Load controllers