Initial commit: monorepo Match Live TV.
Rails API, app Flutter, infrastruttura Docker/MediaMTX, sito marketing e documentazione di deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
53
backend/app/controllers/webhooks/mediamtx_controller.rb
Normal file
53
backend/app/controllers/webhooks/mediamtx_controller.rb
Normal file
@@ -0,0 +1,53 @@
|
||||
module Webhooks
|
||||
class MediamtxController < ApplicationController
|
||||
skip_before_action :authenticate_request!
|
||||
|
||||
before_action :verify_signature!
|
||||
|
||||
def connect
|
||||
handle_event("connect")
|
||||
end
|
||||
|
||||
def disconnect
|
||||
handle_event("disconnect")
|
||||
end
|
||||
|
||||
def ready
|
||||
handle_event("ready")
|
||||
end
|
||||
|
||||
def validate_publish
|
||||
session_id = params[:session_id] || extract_session_id(params[:path])
|
||||
token = params[:token]
|
||||
session = StreamSession.find_by(id: session_id)
|
||||
|
||||
valid = session && session.publish_token == token && !session.ended?
|
||||
render json: { valid: valid }, status: valid ? :ok : :forbidden
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_event(event)
|
||||
session_id = params[:session_id].presence || extract_session_id(params[:path])
|
||||
Webhooks::MediamtxHandler.new(
|
||||
event: event,
|
||||
session_id: session_id,
|
||||
metadata: params.permit!.to_h.except("controller", "action")
|
||||
).call
|
||||
head :ok
|
||||
end
|
||||
|
||||
def extract_session_id(path)
|
||||
path.to_s[%r{match_([0-9a-f-]+)}, 1]
|
||||
end
|
||||
|
||||
def verify_signature!
|
||||
signature = request.headers["X-MediaMTX-Signature"]
|
||||
payload = request.raw_post.presence || request.query_string
|
||||
return if Webhooks::Signature.valid?(payload, signature)
|
||||
return if Rails.env.development? && signature.blank?
|
||||
|
||||
head :unauthorized
|
||||
end
|
||||
end
|
||||
end
|
||||
32
backend/app/controllers/webhooks/stripe_controller.rb
Normal file
32
backend/app/controllers/webhooks/stripe_controller.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
module Webhooks
|
||||
class StripeController < ActionController::API
|
||||
def create
|
||||
payload = request.body.read
|
||||
sig_header = request.env["HTTP_STRIPE_SIGNATURE"]
|
||||
|
||||
unless MatchLiveTv.stripe_webhook_secret.present?
|
||||
Rails.logger.info("[Stripe webhook] ignored — STRIPE_WEBHOOK_SECRET not set")
|
||||
return head :ok
|
||||
end
|
||||
|
||||
event = ::Stripe::Webhook.construct_event(
|
||||
payload, sig_header, MatchLiveTv.stripe_webhook_secret
|
||||
)
|
||||
|
||||
Billing::Stripe::WebhookHandler.call(event)
|
||||
|
||||
head :ok
|
||||
rescue JSON::ParserError, ::Stripe::SignatureVerificationError => e
|
||||
Rails.logger.warn("[Stripe webhook] #{e.message}")
|
||||
head :bad_request
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def stripe_object_from_hash(hash)
|
||||
OpenStruct.new(hash.transform_keys(&:to_s).transform_values do |v|
|
||||
v.is_a?(Hash) ? stripe_object_from_hash(v) : v
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user