songtianlun
5feaee4922
- Create SitemapsController to serve sitemaps - Configure AWS S3 storage for sitemaps - Update routes to include sitemap paths - Add SitemapsHelper module - Configure SitemapGenerator with AWS adapter - Update storage configurations for AWS This feature adds sitemap functionality to the application, enabling search engines to discover and index its content more efficiently. It includes configuration for AWS S3 storage to host the sitemaps and updates the application's routes to serve them.
34 lines
974 B
Ruby
34 lines
974 B
Ruby
class SitemapsController < ApplicationController
|
|
def show
|
|
path = params[:path]
|
|
bucket_name =
|
|
Rails.env.production? ?
|
|
ENV.fetch("AWS_BUCKET", Rails.application.credentials.dig(:aws, :bucket)) :
|
|
ENV.fetch("AWS_DEV_BUCKET", Rails.application.credentials.dig(:aws_dev, :bucket))
|
|
Rails.logger.info "Sitemap: #{path}"
|
|
|
|
begin
|
|
s3_client = Aws::S3::Client.new
|
|
response = s3_client.get_object(
|
|
bucket: bucket_name,
|
|
key: "sitemaps/#{path}"
|
|
)
|
|
|
|
expires_in 12.hours, public: true
|
|
content_type = response.content_type || "application/xml"
|
|
|
|
send_data(
|
|
response.body.read,
|
|
filename: path,
|
|
type: content_type,
|
|
disposition: "inline"
|
|
)
|
|
rescue Aws::S3::Errors::NoSuchKey
|
|
render status: :not_found
|
|
rescue Aws::S3::Errors::ServiceError => e
|
|
Rails.logger.error "S3 Error: #{e.message}"
|
|
render status: :internal_server_error
|
|
end
|
|
end
|
|
end
|