How do you move a sinatra app to github pages? - ruby

I created a sinatra app and I'm trying to put it on github pages. I can run my app on localhost, but I don't know how to push it to the actual web.
I have tried googling and searching stackoverflow and I keep seeing people use heroku. Is this required? Is there anyway to push directly from sinatra to the web? I can get a simple index.html file to run on github pages, but I don't know how to get my sinatra app to run. Any help would be great.

As Sirajus says, Github pages only serve static HTML pages. Sinatra is a framework for producing code that produces web pages. It's really there for sites that require some server side processing (i.e. dynamic), but can serve static pages too (a wise decision).
It doesn't serve the pages itself, it runs on a Rack compliant application server (like Thin, Puma, Unicorn or Webrick) and when asked, it builds the page, gives it to the server and the server gives it to you.
Entirely static sites like Github pages or those served via Amazon's S3 are just a collection of HTML files sitting behind HTTP file servers (those of the Apache and Nginx variety). They're static because they're not generated on the fly… dynamically. They don't change, there's no extra processing needed to serve them. They're just sat there wait to be served. You could use things like Sinatra and Thin to do this, but the added complexity adds maintenance work, security issues, costs extra money, time… and crucially, slows the serving speed down. If your site is essentially all static, by which I mean there's no need for the server to process anything, then you may be better off using something that generates static pages from the off. There are many libraries to help with this, Middleman (based on Padrino and hence Sinatra) and Jekyll come to mind, as does Nanoc.
You can emulate what these libraries do by saving the output of each route and uploading that, if it's appropriate. There are libraries that help with this, like Sinatra Static and its fork, Sinatra Export. You could use cURL to save the pages with a command like curl http://localhost:9292/ > index.html for every single path if you wanted. I did something like this for my own blog the other day.
If it's not appropriate to do this or you can't be bothered, set up some server hosting. A lot of these services are free to start with so it won't hurt to give them all a try, you'll learn a lot from doing each.

To run your sinatra app you need to host it with a server. Github pages only run html page not a ruby web application.
To host your sinatra app you can use heroku/ digital ocean.

Related

What service to use for deploying my flask + dash application

I am building a small application with dash and flask. Where my user can upload his csv/excel file and have a look at the graphs being generated.
I assume the size of each excel file could be around hardly 50MB max / week.
I have 'ZERO' knowledge on servers and deployment etc. Can anyone guide or enlighten me on this area. Also this application is just for an internal purpose so we are not allowed to go easy on the budget.
My random google searches gave me options like,
1. AWS
2. Heroku
Which would be a right option and why ? Considering price and ease of use.
Thanks !
I will share some of mine web dev knowledge, so.. in my company we use flask for all server dev, using many of his libs(like marshmallow, sqlalchemy, etc) and making improvements to them, flask offers you a big flexibility and fast development, but your request thread is poor, so i highly recommend to use a load balancer, the most famous load balancer for flask is Gunicorn, is easy to set and use. For Http server we use Nginx, its like Apache, but make to work with Websockets more easy, and to use with Gunicorn just make a proxy. For the Host, we use AWS, and work very fine for big and little applications, but your application is small and your budget too, so i recommend use the pythonanywhere server, its easy to use and optimized for python webservers. And for frontend we use Vue.js framework, makes our page more beautiful and fast to dev.

What's the best practices for making a Web application to support HTTP/2

I'm trying out HTTP/2 and would like to know if there's any best practices for making a web application take advantages of the new protocol in both:
Server side and
Client side
And... where (and when?) should I start? What are the least effort changes to take effort of HTTP/2
Update:
Assumption : Users are already using browser that support HTTP/2.
Update 2:
Some good resources on this topic:
https://http2.github.io/faq/
https://www.nginx.com/blog/7-tips-for-faster-http2-performance/
https://www.smashingmagazine.com/2016/02/getting-ready-for-http2/
You don't need any changes to your apps to deploy HTTP/2: most things will work out of the box.
That said, here are a few tips that apply mostly if you are starting from scratch:
Start developing your application using HTTPS. You don't want to have problems with your URLs when you deploy your application because fetching some resources over http:// (e.g. Google fonts or any other CDN resources) does not work anymore. It takes five minutes with some tools (just google "Five minutes to HTTPS development URLs").
Consider if you want to host some resources that you would normally link to a public CDN in your own server. The main performance reason with HTTP/2 for linking to some assets in a CDN, if you are not hosting your entire site on that CDN, is that those resources may be already in the user's cache. If they are not, opening a new connection takes in average half a second.
Don't set up bundling. Set up instead HTTP/2 Push. And then go and grab your favourite module manager. Give a try also to web components and PolymerJS. They are simply awesome when you are not concerned about bundling and round-trips.
If you are using a server that uses machine learning for configuring HTTP/2 Push automatically (e.g, ShimmerCat), then a third tip is to keep the structure of your web pages predictable.

Is there a standard way to serve pre-gzipped assets in Rails 3.2 on Heroku Cedar?

I have a Rails 3.2 app that I'm deploying on the Heroku Cedar stack. This means that the app itself is responsible for serving its static assets. I'd like these assets to be gzipped, so I've inserted Rack::Deflater in my middleware stack in production.rb:
middleware.insert_after('Rack::Cache', Rack::Deflater)
...and curl tells me that this works as advertised.
However, since Heroku is going to all the effort of running rake assets:precompile, producing a bunch of pre-gzipped assets, I'd quite like to use those (rather than letting Rack::Deflater do all the work again). I've seen recipes for serving these up with nginx (no use on Heroku), and with CDNs (not wanting to use a CDN just yet), but I haven't seen anything that can just run standalone. I've hacked together a rack middleware to do this, but I was wondering if this is the best way to go about it?
Since deflater is after rack cache, then deflator will only have to do the work once, and after that the compressed assets will be served from rack cache (assuming the cache is big enough so that they don't get bumped out occasionally).
That said, your middleware looks pretty cool, and you should make make it a gem and blog about it, maybe it will be what people start to use :-)

Running a Ruby script via the web

I have a small Ruby script that I want to run by visiting a page in a browser. This might seem like a dumb question but what's the easiest way to accomplish this? I haven't found very good documentation on how to get Ruby working with Apache so I'm open to any suggestions at this point.
I suggest Sinatra. As shown on that page, it's very lightweight, and Apache is not even necessary. As you get to needing more performance you might use Nginx or Apache as a reverse proxy (serving your static files quickly) and something like Thin to make your application run faster.
But for now, just start using Sinatra. As shown on the home page, you can get started in just a few lines.

Why is my sinatra website so slow?

After asking this question, I started using Sinatra as a way to serve web pages.
This evening, a friend of mine and I started to test the speed of the server.
The file to log in looks like:
require 'rubygems'
require 'sinatra'
require 'haml'
enable :sessions #for cookies!
get '/' do
haml :index
end
And the index.haml looks like:
%title
First Page
%header
%h2 First Page
He's sitting on a recent laptop, as am I, with an Apple 802.11n router between the two of us. We're both running Windows 7. I've also tried these same files on a laptop running Ubuntu 9.10 x64 with Sinatra and all relevant files installed from apt-get.
Sinatra is taking 7 seconds to serve up a single page request, no matter the server OS, Windows or Linux. I see that here the author managed to get over 400 requests/second processed. What gives? (or should this be on SuperUser or the like?)
I'll set aside any opinions on when you should optimize your web application.
Set up different configurations in your Sinatra app for development and production because some of these suggestions, you won't always want to use. In fact, you should probably go ahead and setup and environment similar to how you would deploy in production. You would not deploy by simply running ruby app.rb. You'd want to put apache or nginx in front of your Mongrel. Mongrel will serve up your static files, but that's really only advisable for development mode. In deployment, a web server is going to do a lot better job for that. In short, your deployed environment will be faster than your standalone development environment.
At this point, I wouldn't worry about Mongrel vs. Thin. If Thin is twice as fast - it isn't - then your 7 seconds becomes 3.5. Will that be good enough?
Some things to try ...
I know I just told you to set up a deployment environment, but maybe it's not the server side. Have you tried running YSlow or PageSpeed on your pages? I/O is going to take up more of those 7 seconds (Disclaimer: I'm assuming that there's nothing wrong with your network set up) than the server. YSlow - Firebug actually - will tell you how long each part of your page takes to get to the browser.
One of the things that YSlow told me to do was to put a far forward Expires header on my static assets, which I knew but I was leaving optimization until the end. That's when I realized that there were at least 3 different places that I could specify that header. I'm convincing myself that doing it in nginx is the right place to put it.
If you're happy with those results, then you can look at the server. Off the top of my head, so not exhaustive
Turn on gzip responses.
Combine your stylesheets so there's only one per page request. There may be some Rack Middleware for this, if you don't do it manually.
Cache. I'm trying Rack::Cache.
Use sprites to decrease the number of image downloads you use.
Minify your Javascript. Again, maybe via Rack Middleware.
Rack Middleware is neat, but it uses CPU. So, manually minifying your Javascript adds a new step to your workflow, but on the server, it's faster than Middleware. It's a tradeoff.
Sorry if this was rambly.
I had this problem when running Sinatra with shotgun but not when running my app directly (i.e., ruby -rubygems app.rb). This is because shotgun forks and reloads the application for each request.
I found a thread in Sinatra's mailing list which discussed this issue and people there advised using rerun instead of shotgun. I'm happy to say it solved this issue for me.
Try using Thin as the server. I noticed an increase in performance compared with WEBrick and Mongrel.
gem install thin
When you run your app using ruby TestServer.rb you'll see the following:
Sinatra/0.10.1 has taken the stage on 4567 for development with backup from Thin
I'm running Sinatra inside VMWare Fusion with Vagrant. My app was running slowly (about ten seconds to service a request). Then I found this gem:
Webrick is very slow to respond. How to speed it up?
It seems that WEBrick was (by default) configured to reverse dns lookup on every request, and that was slowing it down.

Resources