- Microservices Development Cookbook
- Paul Osman
- 167字
- 2021-07-16 17:48:27
How to do it...
- Open the service object we created in the previous recipe. We'll modify the service object to be able to call an external microservice responsible for managing attachments. For the sake of simplicity, we'll use an HTTP client that is provided in the Ruby standard library. The service object should be in the app/services/attachments_service.rb file:
class AttachmentsService
BASE_URI = "http://attachment-service.yourorg.example.com/"
def upload(message_id, user_id, file_name, data, media_type)
body = {
user_id: user_id,
file_name: file_name,
data: StringIO.new(Base64.decode64(params[:file]
[:data]), 'rb'),
message: message_id,
media_type: media_type
}.to_json
uri = URI("#{BASE_URI}attachment")
headers = { "Content-Type" => "application/json" }
Net::HTTP.post(uri, body, headers)
end
end
- Open the attachments_controller.rb file, located in pichat/app/controllers/, and look at the following create action. Because of the refactoring work done in the previous chapter, we require only a small change to make the controller work with our new service object:
class AttachmentsController < ApplicationController
# POST /messages/:message_id/attachments
def create
service = AttachmentService.new
response = service.upload(params[:message_id], current_user.id,
params[:file][:name], params[:file][:data],
params[:media_type])
json_response(response.body, response.code)
end
# ...
end
推薦閱讀
- 6G潛在關鍵技術(下冊)
- 物聯網安全:理論、實踐與創新
- 光網絡評估及案例分析
- Oracle SOA Suite 11g Performance Tuning Cookbook
- 物聯網時代
- 計算機網絡工程實用教程(第2版)
- Learning Swift(Second Edition)
- 網絡安全應急響應技術實戰
- C/C++串口通信:典型應用實例編程實踐
- Master Apache JMeter:From Load Testing to DevOps
- Android UI Design
- Learning Windows 8 Game Development
- 園區網絡架構與技術
- 精通SEO:100%網站流量提升密碼
- 新媒體交互藝術