predator_pics/server.rb

69 lines
1.7 KiB
Ruby

=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
require 'bundler/setup'
require 'json'
require 'faraday'
require 'sinatra'
require 'sinatra/namespace'
require 'thin'
require_relative 'get_images_by_category.rb'
require_relative 'get_random_image.rb'
set :environment, :production
set :server, %w[thin mongrel webrick]
categories = File.readlines("categories.txt")
# Index
get '/' do
puts image = get_random_image(get_images_by_category(categories))
erb :index, locals: { image: image }
end
# About page
get '/about' do
erb :about
end
# 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