#!/usr/bin/env ruby require 'unicorn-cuba-base' $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) Application.new('httpthumbnailer', port: 3100) do cli do description 'Image thumbnailing HTTP server' switch :formats, short: :F, description: 'print backend versions and supported formats' option :limit_disk, cast: Integer, description: 'image cache temporary file size limit in MiB', default: 1024 options :plugins, cast: Pathname, description: 'path to directory from which plugins will be loaded (files with .rb extension)', default: '/usr/share/httpthumbnailer/plugins' switch :no_optimization, description: 'disable load time size hinting and downsampling optimizations all together' switch :reload, description: 'reload input images without size hint that got upscaled instead of downsampling (broken JPEG lib)' switch :no_upscale_fix, description: 'do nothing if image got upscaled when using size hint (broken JPEG lib)' switch :no_downsample, description: 'disable downsampling of input image before processing' version (Pathname.new(__FILE__).dirname + '..' + 'VERSION').read end settings do |settings| if settings.formats require 'httpthumbnailer/plugin/thumbnailer' puts "Versions:" puts "\t#{Plugin::Thumbnailer::Service.rmagick_version}" puts "\t#{Plugin::Thumbnailer::Service.magick_version}" puts puts "Input formats:" puts "\t#{Plugin::Thumbnailer::Service.input_formats.join(' ')}" puts puts "Output formats:" puts "\t#{Plugin::Thumbnailer::Service.output_formats.join(' ')}" puts puts "Supported Interlace:" puts "\t#{Magick::InterlaceType.values.map(&:to_s).join(' ')}" exit 0 end Controller.settings[:optimization] = (not settings.no_optimization) Controller.settings[:reload] = settings.reload Controller.settings[:no_upscale_fix] = settings.no_upscale_fix Controller.settings[:no_downsample] = settings.no_downsample Controller.settings[:limit_memory] = settings.limit_memory * 1024**2 Controller.settings[:limit_map] = settings.limit_disk * 1024**2 Controller.settings[:limit_disk] = settings.limit_disk * 1024**2 end main do |settings| require 'httpthumbnailer/error_reporter' require 'httpthumbnailer/thumbnailer' settings.plugins.map do |dir| begin dir.realpath rescue Errno::ENOENT => error log.warn "plugin directory '#{dir}' is not accessible: #{error}" nil end end.compact.map do |dir| Pathname::glob(dir + '**/*.rb') end.each do |plugin_files| plugin_files.sort.each do |plugin_file| Plugin::Thumbnailer.setup_plugin_from_file(plugin_file) end end class HTTPThumbnailer < Controller extend Stats def_stats( :workers, :total_requests, :total_errors ) raindrops_stats = Raindrops::Middleware::Stats.new self.use Raindrops::Middleware, stats: raindrops_stats StatsReporter << HTTPThumbnailer.stats StatsReporter << raindrops_stats StatsReporter << Plugin::Thumbnailer::Service.stats StatsReporter << Plugin::ResponseHelpers.stats self.define do HTTPThumbnailer.stats.incr_total_requests on error? do HTTPThumbnailer.stats.incr_total_errors run ErrorReporter end on 'stats' do run StatsReporter end on 'health_check' do write_plain 200, 'HTTP Thumbnailer OK' end on root do write_plain 200, 'HTTP Thumbnailer' end on true do run Thumbnailer end end end HTTPThumbnailer end after_fork do |server, worker| HTTPThumbnailer.stats.incr_workers end end