predator_pics/server.rb

69 lines
1.7 KiB
Ruby
Raw Normal View History

2024-11-13 13:27:23 +00:00
=begin
Copyright (C) 2024 Hexaitos
This file is part of "Predator Pics"
Predator Pics is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Predator Pics is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Foobar. If not, see <https://www.gnu.org/licenses/>.
Contact: me@bateleur.org, me@hexaitos.com.
=end
2024-11-11 22:31:18 +00:00
require 'bundler/setup'
2024-11-10 13:12:42 +00:00
require 'json'
require 'faraday'
require 'sinatra'
require 'sinatra/namespace'
2024-11-11 22:31:18 +00:00
require 'thin'
2024-11-10 13:12:42 +00:00
require_relative 'get_images_by_category.rb'
require_relative 'get_random_image.rb'
set :environment, :production
set :server, %w[thin mongrel webrick]
2024-11-10 13:12:42 +00:00
categories = File.readlines("categories.txt")
# Index
2024-11-10 13:12:42 +00:00
get '/' do
2024-11-13 13:27:23 +00:00
puts image = get_random_image(get_images_by_category(categories))
2024-11-10 13:12:42 +00:00
erb :index, locals: { image: image }
end
# About page
2024-11-10 13:12:42 +00:00
get '/about' do
erb :about
2024-11-10 17:13:42 +00:00
end
2024-11-13 13:27:23 +00:00
# API information
get '/about-api' do
erb :api
end
# API
namespace "/api" do
before do
content_type 'application/json'
end
get '/random' do
image = get_random_image(get_images_by_category(categories))
image.to_json
end
get '/categories' do
content_type :json
categories_trimmed = []
categories.each do |category|
categories_trimmed << category.delete("\n")
end
categories_trimmed.to_json
end
end