Skip to content
Open

HW 2 #33

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
2 changes: 2 additions & 0 deletions .dockerdev/.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alias be="bundle exec"
PS1='🐳\[\033[01;31m\] ❯\[\033[00m\] '
26 changes: 26 additions & 0 deletions .dockerdev/.psqlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Don't display the "helpful" message on startup.
\set QUIET 1

-- Allow specifying the path to history file via `PSQL_HISTFILE` env variable
-- (and fallback to the default $HOME/.psql_history otherwise)
\set HISTFILE `[ -z $PSQL_HISTFILE ] && echo $HOME/.psql_history || echo $PSQL_HISTFILE`

-- Show how long each query takes to execute
\timing

-- Use best available output format
\x auto

-- Verbose error reports
\set VERBOSITY verbose

-- If a command is run more than once in a row,
-- only store it once in the history
\set HISTCONTROL ignoredups
\set COMP_KEYWORD_CASE upper

-- By default, NULL displays as an empty space. Is it actually an empty
-- string, or is it null? This makes that distinction visible
\pset null '[NULL]'

\unset QUIET
3 changes: 3 additions & 0 deletions .dockerdev/Aptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vim
file
zip
64 changes: 64 additions & 0 deletions .dockerdev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
ARG RUBY_VERSION=3.3.0
ARG DISTRO_NAME=bookworm

FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME

# Common dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

ARG PG_MAJOR
ARG DISTRO_NAME
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /usr/share/keyrings/postgres-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/postgres-archive-keyring.gpg] https://apt.postgresql.org/pub/repos/apt/" \
$DISTRO_NAME-pgdg main $PG_MAJOR | tee /etc/apt/sources.list.d/postgres.list > /dev/null
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Application dependencies
COPY Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/Aptfile | xargs) \
&& apt-get clean \
&& rm -rf /var/cache/apt/archives/* \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log

# Configure bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3

# Store Bundler settings in the project's root
ENV BUNDLE_APP_CONFIG=.bundle

# Upgrade RubyGems and install the latest Bundler version
RUN gem update --system && \
gem install bundler

# Create a directory for the app code
WORKDIR /rails

# Use Bash as the default command
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
102 changes: 102 additions & 0 deletions .dockerdev/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
x-app:
&app
build:
context: .
args:
RUBY_VERSION: '3.3.0'
PG_MAJOR: '17'
image: library/dev:1.0.0
environment:
&env
RAILS_ENV: ${RAILS_ENV:-development}
env_file:
- ./.env
tmpfs:
- /tmp
- /app/tmp/pids

x-backend:
&backend
<<: *app
stdin_open: true
tty: true
volumes:
- ..:/rails:cached
- bundle:/usr/local/bundle
- rails_cache:/rails/tmp/cache
- history:/usr/local/hist
- ./.psqlrc:/root/.psqlrc:ro
- ./.bashrc:/root/.bashrc:ro
- "$DOCKER_COMPOSE_ARCHIVE_FOLDER:/rails/db/data"
environment:
&backend_environment
<<: *env
DATABASE_URL: postgres://postgres:postgres@postgres:5432
MALLOC_ARENA_MAX: 2
WEB_CONCURRENCY: ${WEB_CONCURRENCY:-1}
BOOTSNAP_CACHE_DIR: /usr/local/bundle/_bootsnap
XDG_DATA_HOME: /rails/tmp/cache
HISTFILE: /usr/local/hist/.bash_history
PSQL_HISTFILE: /usr/local/hist/.psql_history
IRB_HISTFILE: /usr/local/hist/.irb_history
EDITOR: vi
RAILS_LOG_TO_STDOUT: 'yes'
PAGER: 'more'
depends_on:
postgres:
condition: service_healthy
mongodb:
condition: service_healthy


services:
runner:
<<: *backend
command: /bin/bash

rails:
<<: *backend
command: bundle exec rails

web:
<<: *backend
command: >
sh -c "rm -f tmp/pids/server.pid &&
bundle exec rails s -p 3000 -b '0.0.0.0'"
ports:
- '3000:3000'

postgres:
image: postgres:17
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres:/var/lib/postgresql/data
- history:/usr/local/hist
environment:
PSQL_HISTFILE: /usr/local/hist/.psql_history
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s

mongodb:
image: mongo:latest
ports:
- '27017:27017'
volumes:
- mongodb:/data/db
healthcheck:
test: ["CMD","mongosh", "--eval", "db.adminCommand('ping')"]
interval: 5s
timeout: 5s
retries: 3
start_period: 5s

volumes:
bundle:
history:
rails_cache:
postgres:
mongodb:
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ gem 'puma', '>= 6.5.0'

# Базы данных
gem 'pg'
gem 'mongoid', '~> 9.0'
gem 'bson_ext'
gem 'kaminari-mongoid'

# Многопоточное выполнение
gem 'parallel'
Expand All @@ -29,6 +32,9 @@ gem 'activeadmin'
gem 'activeadmin_addons'
gem 'devise'

gem 'alba'
gem 'kaminari'

group :development, :test do
gem 'bundler-audit'
gem 'capybara'
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ GEM
uri (>= 0.13.1)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
alba (3.5.0)
ostruct (~> 0.6)
arbre (1.7.0)
activesupport (>= 3.0.0)
ruby2_keywords (>= 0.0.2)
Expand All @@ -104,6 +106,8 @@ GEM
benchmark (0.4.0)
bigdecimal (3.1.8)
bindex (0.8.1)
bson (5.0.2)
bson_ext (1.5.1)
builder (3.3.0)
bundler-audit (0.9.2)
bundler (>= 1.2.0, < 3)
Expand Down Expand Up @@ -210,6 +214,9 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
kaminari-mongoid (1.0.2)
kaminari-core (~> 1.0)
mongoid
language_server-protocol (3.17.0.3)
logger (1.6.0)
loofah (2.23.1)
Expand All @@ -226,6 +233,12 @@ GEM
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.1)
mongo (2.21.0)
bson (>= 4.14.1, < 6.0.0)
mongoid (9.0.6)
activemodel (>= 5.1, < 8.1, != 7.0.0)
concurrent-ruby (>= 1.0.5, < 2.0)
mongo (>= 2.18.0, < 3.0.0)
net-imap (0.5.6)
date
net-protocol
Expand Down Expand Up @@ -448,6 +461,8 @@ DEPENDENCIES
activeadmin
activeadmin_addons
activerecord-import
alba
bson_ext
bundler-audit
capybara
config
Expand All @@ -460,6 +475,9 @@ DEPENDENCIES
factory_bot_rails
fasterer
ffaker
kaminari
kaminari-mongoid
mongoid (~> 9.0)
parallel
pg
pry-byebug
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class BooksController < ApplicationController
def index
books = Mongo::Book.page(page).per(Settings.app.items_per_page)
render json: BookSerializer.new(books).serialize
end

private

def page
[ params[:page].to_i, 1 ].max
end
end
28 changes: 28 additions & 0 deletions app/models/mongo/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Mongo
class Book
include Mongoid::Document
include Mongoid::Timestamps

field :title, type: String
field :series, type: String
field :serno, type: String
field :libid, type: Integer
field :size, type: Integer
field :filename, type: Integer
field :del, type: Boolean
field :ext, type: String
field :published_at, type: Date
field :insno, type: String

has_and_belongs_to_many :authors
has_and_belongs_to_many :genres
has_and_belongs_to_many :keywords

belongs_to :folder, optional: true, class_name: 'Mongo::Folder'
belongs_to :language, optional: true, class_name: 'Mongo::Language'

PUBLIC_FIELDS = %w[del ext filename folder_id insno language_id
libid series serno size title published_at
updated_at created_at].freeze
end
end
9 changes: 9 additions & 0 deletions app/models/mongo/folder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Mongo
class Folder
include Mongoid::Document
include Mongoid::Timestamps

validates :name, presence: true
validates :name, uniqueness: true
end
end
6 changes: 6 additions & 0 deletions app/models/mongo/genre_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Mongo
class GenreGroup
include Mongoid::Document
include Mongoid::Timestamps
end
end
9 changes: 9 additions & 0 deletions app/models/mongo/language.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Mongo
class Language
include Mongoid::Document
include Mongoid::Timestamps

validates :slug, presence: true
validates :slug, uniqueness: true
end
end
12 changes: 12 additions & 0 deletions app/serializers/book_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class BookSerializer
include ::Alba::Resource

attributes :id, :title, :created_at, :updated_at, :series, :serno, :libid, :size, :filename,
:del, :ext, :published_at, :insno

attribute :folder do |book|
book.folder.try(:name) if book.folder
end
end
Loading