简体   繁体   中英

Rails app (production) interfacing with another rails app on same machine

I created a small application that generates reports (mainly PDFs) from an sqlite3 database exported from GNUCash. I separated the app with a possibility of sharing it with other VFW posts that are currently using GNUCash or stuck with a paper general ledger. I call it a quasi API in that most reports can be generated in both HTML and PDF or it can just send data. Before I separated it, I had tried a multiple database approach, adding the sqlite3 database to my database.yaml along with my main postgresql database. It worked but that made the possibility of sharing difficult and I was a little skeptical about the approach.

All is fine with PDFs (prawn generated). I use RestClient to get the reports from the pdf server running on the same machine on a different port. For example, to generate a General Ledger this controller get action is called:

def ledger_pdf
  resp = RestClient.get "http://localhost:8600/ledgers/#{params[:id]}/ledger_pdf"
  send_data resp.body, filename: "Ledger#{params[:id]}",
    type: "application/pdf",
    disposition: "inline"
end

On the pdf server, this action responds

def ledger_pdf
  pdf = Pdf::Ledger.new(view_context,params)
  send_data pdf.render, filename: "Ledger#{params[:id]}",
    type: "application/pdf",
    disposition: "inline"
end

Getting the HTML version became a little more challenging! On the pdf server I have a one page menu that lists all the reports available, some by date and others with a collection route. This was for the 'sharing' version where someone only needs the reports and does not have another rails application that does other stuff. The server could be stand alone running on some box.

Rather then getting the data and creating a page on the main application from the data, I got the menu from the pdf server in an iframe tag. I though this was working great when I discovered that, in a view:

<iframe src="http://localhost:8600/ledgers/" width="100%" height="100%" id="rails_iframe">error!</iframe>

was calling localhost:8600 on my machine (which was running a development version and fooled me into thinking it was working!) rather than on the production server. After some researching I discovered that you can't do that. I guess the RestClient call to localhost:8600 is hidden from the browser, but the iframe call is not.

I guess I can get the HTML versions using the same RestClient approach and return text/html and use nokogiri to get the body/html and yield it somehow, if I knew how to do that.

The html.slim is really simple and I could just duplicate it on my main server after getting the data, but then I'd have two code bases to maintain.

Another approach would be to make it a real API and make it public so that the iframe approach would work, something I was trying to avoid (authentication and all that).

I guess my question is, does someone have another approach or can refine or give me pointers on my approaches.

You just need to add another action to your 'pdf' server which will render menu:

def menu
  render json: {first: 'first/report', second: 'second/report'}
end

And then just fetch that menu from your main server (which is facing users):

def menu
  resp = RestClient.get ...
  # parse resp.body to get menu structure
  # convert JSON response from pdf server to HTML with your ERB template
  @menu_items = ...
end

Then just have route on your main server to pass information from collection server as well, something like:

get "reports/:collection/:date/:report", to: 'reports#fetch'

# then in your controller

def fetch
  resp = RestClient.get "...#{params[:collection]}/#{params[:date]}/#{params[:report]}
  ...
end

Personally, I wouldn't use Rails server for pdf generation. I would've just used cron script which would run Ruby script for PDF generation, and then I would just upload them to S3 or some other cloud storage. My Rails server would've just return the link for that S3 file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM