class HealthController < ApplicationController
skip_before_action :verify_authenticity_token
def liveness
render json: { status: 'ok' }, status: :ok
end
json.array! @posts do |post|
json.cache! ['v1', post], expires_in: 1.hour do
json.id post.id
json.title post.title
json.excerpt post.excerpt
json.published_at post.published_at
class CreatePostService
def initialize(author:, params:)
@author = author
@params = params
end
class AddIndexesToPosts < ActiveRecord::Migration[6.1]
def change
add_index :posts, :author_id
add_index :posts, :published_at
add_index :posts, [:author_id, :published_at]
add_index :posts, :created_at, order: { created_at: :desc }
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
:concurrency: 10
:queues:
- [critical, 4]
- [default, 2]
- [low, 1]
Rails.application.configure do
config.after_initialize do
Bullet.enable = true
Bullet.alert = false
Bullet.bullet_logger = true
Bullet.console = true
module CursorPagination
extend ActiveSupport::Concern
private
def paginate_with_cursor(scope, per_page: 20)
module ApiErrorHandler
extend ActiveSupport::Concern
included do
rescue_from StandardError, with: :handle_standard_error
rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
class Rack::Attack
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(url: ENV['REDIS_URL'])
safelist('allow-localhost') do |req|
req.ip == '127.0.0.1' || req.ip == '::1'
end
class JwtService
ACCESS_TOKEN_LIFETIME = 15.minutes
REFRESH_TOKEN_LIFETIME = 7.days
SECRET = Rails.application.credentials.jwt_secret
def self.encode_access_token(user_id)
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :create, :update]
resources :posts do
resources :comments, only: [:index, :create]