Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 17 additions & 61 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,69 +1,25 @@
# syntax=docker/dockerfile:1
# check=error=true
FROM ruby:3.4-alpine

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t cl_test_assignment .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name cl_test_assignment cl_test_assignment
RUN apk add \
build-base \
yaml-dev \
postgresql-dev \
tzdata \
nodejs \
libxml2-dev \
libxslt-dev

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
WORKDIR /app

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.4.1
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/



RUN bundle install --jobs=$(nproc) --retry=3

# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER 1000:1000
COPY . .

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]

# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80
CMD ["./bin/thrust", "./bin/rails", "server"]
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]
69 changes: 69 additions & 0 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# syntax=docker/dockerfile:1
# check=error=true

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t cl_test_assignment .
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name cl_test_assignment cl_test_assignment

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.4.1
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/




# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
USER 1000:1000

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start server via Thruster by default, this can be overwritten at runtime
EXPOSE 80
CMD ["./bin/thrust", "./bin/rails", "server"]
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ gem "thruster", require: false
# CSV processing for seed data
gem "csv"

gem 'rswag'
group :development, :test do
gem 'pry'
gem 'redis'
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
gem "brakeman", require: false
gem "rubocop-rails-omakase", require: false
Expand Down
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ GEM
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.3)
base64 (0.3.0)
bcrypt_pbkdf (1.1.1)
Expand All @@ -82,6 +84,7 @@ GEM
brakeman (7.1.0)
racc
builder (3.3.0)
coderay (1.1.3)
concurrent-ruby (1.3.5)
connection_pool (2.5.3)
crass (1.0.6)
Expand Down Expand Up @@ -116,6 +119,9 @@ GEM
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.13.2)
json-schema (5.2.2)
addressable (~> 2.8)
bigdecimal (~> 3.1)
kamal (2.7.0)
activesupport (>= 7.0)
base64 (~> 0.2)
Expand All @@ -139,6 +145,7 @@ GEM
net-pop
net-smtp
marcel (1.0.4)
method_source (1.1.0)
mini_mime (1.1.5)
minitest (5.25.5)
msgpack (1.8.0)
Expand Down Expand Up @@ -186,9 +193,13 @@ GEM
prettyprint
prettyprint (0.2.0)
prism (1.4.0)
pry (0.15.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.2.6)
date
stringio
public_suffix (6.0.2)
puma (6.6.1)
nio4r (~> 2.0)
raabro (1.4.0)
Expand Down Expand Up @@ -235,6 +246,10 @@ GEM
rdoc (6.14.2)
erb
psych (>= 4.0.0)
redis (5.4.1)
redis-client (>= 0.22.0)
redis-client (0.25.3)
connection_pool
regexp_parser (2.11.2)
reline (0.6.2)
io-console (~> 0.5)
Expand All @@ -255,6 +270,21 @@ GEM
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.5)
rswag (2.16.0)
rswag-api (= 2.16.0)
rswag-specs (= 2.16.0)
rswag-ui (= 2.16.0)
rswag-api (2.16.0)
activesupport (>= 5.2, < 8.1)
railties (>= 5.2, < 8.1)
rswag-specs (2.16.0)
activesupport (>= 5.2, < 8.1)
json-schema (>= 2.2, < 6.0)
railties (>= 5.2, < 8.1)
rspec-core (>= 2.14)
rswag-ui (2.16.0)
actionpack (>= 5.2, < 8.1)
railties (>= 5.2, < 8.1)
rubocop (1.80.0)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
Expand Down Expand Up @@ -347,9 +377,12 @@ DEPENDENCIES
factory_bot_rails
kamal
pg (>= 1.1)
pry
puma (>= 5.0)
rails (~> 8.0.2, >= 8.0.2.1)
redis
rspec-rails
rswag
rubocop-rails-omakase
solid_cable
solid_cache
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/routes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# наболело, автолоадер всю душу вынул, при спринге такого не было =)
class RoutesController < ApplicationController
before_action :check_params
def index
permitted_routes = PermittedRoute.by_carrier_and_route(search_params['carrier'], search_params['origin_iata'], search_params['destination_iata']).first
render json: {status: 200, message: 'route not found', routes: [] } and return if permitted_routes.nil?
data = FindRoutsService.new.call(permitted_routes, search_params)
result = format_routes(data)
# RoutesJsonGenerator.new.format_routes(data)
render json: {status: 200, routes: result}
end

private

def format_routes(routes)
routes.map do |segments|
{
origin_iata: segments.first.origin_iata,
destination_iata: segments.last.destination_iata,
departure_time: segments.first.std.iso8601,
arrival_time: segments.last.sta.iso8601,
segments: segments.map do |segment|
{
carrier: segment.airline,
segment_number: segment.segment_number,
origin_iata: segment.origin_iata,
destination_iata: segment.destination_iata,
std: segment.std.iso8601,
sta: segment.sta.iso8601
}
end
}
end
end

def check_params
required = [:carrier, :origin_iata, :destination_iata, :departure_from, :departure_to]
render json: {status: 420, message: 'invalid request'} and return unless required.all? {|k| params.has_key? k}
end

def search_params
params.permit(:carrier, :origin_iata, :destination_iata, :departure_from, :departure_to)
end
end
22 changes: 22 additions & 0 deletions app/generators/routes_json_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class RoutesJsonGenerator
def format_routes(routes)
routes.map do |segments|
{
origin_iata: segments.first.origin_iata,
destination_iata: segments.last.destination_iata,
departure_time: segments.first.std.iso8601,
arrival_time: segments.last.sta.iso8601,
segments: segments.map do |segment|
{
carrier: segment.airline,
segment_number: segment.segment_number,
origin_iata: segment.origin_iata,
destination_iata: segment.destination_iata,
std: segment.std.iso8601,
sta: segment.sta.iso8601
}
end
}
end
end
end
4 changes: 4 additions & 0 deletions app/models/permitted_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@

class PermittedRoute < ApplicationRecord
validates :carrier, :origin_iata, :destination_iata, presence: true

scope :by_carrier_and_route, ->(carrier, origin, destination) do
where(carrier: carrier, origin_iata: origin, destination_iata: destination)
end
end
4 changes: 4 additions & 0 deletions app/models/segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
class Segment < ApplicationRecord
validates :airline, :segment_number, presence: true
validates :origin_iata, :destination_iata, presence: true, length: { is: 3 }

scope :by_carrier, ->(carrier) { where(airline: carrier) }
scope :by_route, ->(origin, destination) { where(origin_iata: origin, destination_iata: destination) }
scope :departing_between, ->(from, to) { where(std: from..to) }
end
5 changes: 5 additions & 0 deletions app/services/data_to_hash_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# не просто так созданный класс, думал превращать параметры в хеш,
# чтобы потом положить в кеш, но я не придумал как его правильно инвалидировать,
# потому что как я понял, сегметы могут меняться, добавляться или удаляться
class DataToHashService
end
Loading