Code Monkey home page Code Monkey logo

sinatra-shotgun-server's Introduction

Using the Shotgun Development Server

Overview

This lesson will introduce you to Shotgun and how to use it with Sinatra apps. We'll also cover troubleshooting common problems that you might encounter when running Shotgun.

Note: shotgun only works on Linux and Mac OS X. If you're not using a Linux VM on Windows, you might encounter difficulties in this section of the course. Seek coaching advise if you need help.

Objectives

  1. Explain how using rackup to start a Sinatra application will only read the code once at boot
  2. Describe how Shotgun allows for automatic code reloading
  3. Install Shotgun and require it in an application's Gemfile
  4. Start and stop a Rack or Sinatra application with Shotgun
  5. Troubleshoot common Shotgun problems, including "command not found," "bundler error," and "port in use."

Setup

Make sure you run bundle install to install the gems in our Gemfile. If your operating system is OSX El Capitan and you have an issue installing EventMachine, run the following command: gem install eventmachine -- --with-cppflags=-I/usr/local/opt/openssl/include.

Why Shotgun

Normally when we develop simple Rack applications, like Sinatra applications, we start our application server with rackup and load config.ru.

For the purpose of this lesson, fork and clone this repo. There are, however, no tests to pass. In terminal, go ahead and enter rackup app.rb. This will start up a Rack server. You should see something like this:

rackup

Pay attention to the line that says Listening on localhost:9292. localhost:9292 is the url you want to enter in the browser. If you are in the Learn IDE, you will see a different address โ€” here's an explanatory Help Center article. Go ahead and copy and paste that into the browser. You should see Welcome to your app!!!! on the screen.

When starting an application with rackup, our application code is read once on boot and never again. Once we start the application locally, if we make changes to our code, our running application server will not read those changes until it is stopped and restarted.

Now add some more text to the string in the controller action:

  get '/' do
    "Welcome to your app!!!! I BUILT THIS!"
  end

Refresh the page in the browser. You should still see Welcome to your app!!!!. That's because Rack isn't aware that we made changes. You can shut down your server by going back to terminal and hitting ctrl + c.

Start your server back up by entering rackup app.rb and now try visiting localhost:9292 in the browser. It should work and you should see the text Welcome to your app!!!! I BUILT THIS! in your browser window.

This tedious save, stop, and restart cycle makes developing Rack or Sinatra applications near impossible. To avoid this, instead of starting our application with rackup, we will use shotgun.

Shotgun is a small Ruby gem that makes it easier to develop and test Rack-based Ruby web applications locally by starting Rack with automatic code reloading. Remember: a gem is just a library of code that developers wrote and made free and available to the public. This gem lets us start Rack to have a development server running to test our app.

When you start an application with shotgun, all of your application code will be reloaded upon every request. That means if you change anything in your code and save it, when you hit 'Refresh' in your browser, your application will respond with the latest version of your code.

Installing Shotgun

You can install Shotgun via gem install shotgun. You should also require it in an application's Gemfile so that you can install it and load it via Bundler. Shotgun is already in your gemfile, so go ahead and enter bundle install in terminal if you haven't already.

Starting and Stopping Shotgun

Within a Rack or Sinatra application directory, you can start the application via Shotgun by simply executing shotgun in your terminal. You should see something like:

$ shotgun
== Shotgun/Thin on http://127.0.0.1:9393/
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 127.0.0.1:9393, CTRL+C to stop

That means your application is loaded and being served from port 9393, the default Shotgun port. The application will respond to requests at http://127.0.0.1:9393 or, more commonly, localhost:9393 and will reload your code on every request until the process is terminated by typing CTRL+C. Go ahead and visit localhost:9393 in the browser.

A port is just an endpoint on the server that is open for communication. It's typical for a server to regulate the open ports to make sure it can monitor requests appropriately. You'll notice that with Rack you used port 9292, but in Shotgun it's 9393.

A working server responding to a request and then exiting looks like:

Shotgun Working

You can pass shotgun most of the CLI arguments and flags that rackup accepts.

Now, with your Shotgun server still running, change the text in the string in the controller action:

get '/' do
  "Started my server using Shotgun!"
end

Save your changes to app.rb and flip back to localhost:9393 in the browser. Hit 'Refresh' in your browser to make a new request. This time, you should see the text Started my server using Shotgun! as opposed to seeing the previous text, just like with Rack.

Troubleshooting Shotgun

command not found

When you run shotgun from your terminal you might get a Shell error that reads something like:

$ shotgun
-bash: shotgun: command not found

That means the Shotgun gem isn't properly installed or available in your PATH. Try fixing it with:

$ gem install shotgun

If you still get that error, from within your application directory, try running:

$ bundle install

followed by

$ bundle exec shotgun

bundler error

You might get an error about bundler that will tell you to run bundle install. It'll look like this:

$ shotgun
bundler: command not found: shotgun
Install missing gem executables with `bundle install`

Just run bundle install and then shotgun again. If you still get the error, try running via:

$ bundle exec shotgun

port in use

You also might get an error about a port being in use. It'll look like this:

$ shotgun
== Shotgun/Thin on http://127.0.0.1:9393/
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 127.0.0.1:9393, CTRL+C to stop
/Users/avi/.rvm/gems/ruby-2.2.2/gems/eventmachine-1.0.8/lib/eventmachine.rb:534:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)

This means you have another Shotgun server running somewhere. Do you have another terminal or shell open running a web application or Shotgun? You need to find that process or tab that is running a web application using Shotgun and shut it down with CTRL+C.

You can also tell Shotgun to use a different port with the -p flag.

$ shotgun -p 4200
== Shotgun/Thin on http://127.0.0.1:4200/
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on 127.0.0.1:4200, CTRL+C to stop

You'll notice the server started on port 4200, which is hopefully unoccupied. You can supply any port number. But it's best to terminate your servers rather than creating hundreds of new ones on different ports.

sinatra-shotgun-server's People

Contributors

annjohn avatar aviflombaum avatar danielseehausen avatar dependabot[bot] avatar dfenjves avatar franknowinski avatar gj avatar ihollander avatar jmburges avatar karuna24s avatar kaylee42 avatar maxwellbenton avatar mendelb avatar peterbell avatar plai217 avatar pletcher avatar rrcobb avatar sgharms avatar shmuwol avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinatra-shotgun-server's Issues

Forked the Lesson, But Sidebar Menu Does Not Reflect

I have forked this lesson, both manually and through the "Open" button. However, the "Fork this lesson" part of the side menu does not turn green. I was able to submit a pull request, as seen in the picture below.

Because the "Fork this lesson" part of the menu is not marked successful, I am not able to complete this part of the lesson.

screen shot 2016-07-05 at 12 28 18 pm

eventmachine-1.0.8 fails to install

Error and fix here eventmachine/eventmachine#643
brew link openssl --force fixes it
It is an issue only on El Capitan OSX as far as I can tell.
Also they say using eventmachine-1.0.9 fixes the problem. I am not sure if bundle install would automatically use that version if there was no Gemfile.lock

Shotgun not compatible with IDES on Windows system

It would be nice if the lessons using shotgun advised that it is not compatible with Windows and some other gem/application should be used. I spent a lot of time trying to figure out why it wasn't working and eventually found a small section about it on StackOverflow. I prefer using my desktop which is a larger system but it is Windows. Please notify students of issues with gems that are not supported in certain systems.

typo

The word "just" implies they are the same, but the point of the passage is that Rack does not feature continuous loading and Shotgun does

This time, you should see the text Started my server using Shotgun! as opposed to seeing the previous text, just like with Rack.

rackup app.rb/config.ru

Unsure if others have this issue, but I was unable to start a server via these commands. I double-checked whether or not it was still working for other labs, and I was able to easily set up a server in other, previous labs. Went back and tried in this lab, and again, no server.

Shotgun worked, however. Unsure why this happened.

Quick explantion on what a port is

in the bottom you say about using different port. Just a quick explanation of what a port is, and maybe a link to an article you think is good

Knowledge gaps to address for students in the Learn Qualifying Track

screen shot 2015-10-01 at 12 01 57 pm

^^ Students won't know what a "gem" is. Is it worth a single line explanation?

screen shot 2015-10-01 at 12 04 12 pm

^^ "start our application server with rackup" is somewhat confusing. Would it be possible to visually (via code snippets or other things) or through an example to explain the paragraph after that. Explain better what or implications of reading code once on boot and never again is.

screen shot 2015-10-01 at 12 04 12 pm

^^ Screenshot would be helpful

screen shot 2015-10-01 at 12 04 12 pm

In general showing a Sinatra app directory structure would be helpful. Also really going into detail about how to execute this would be good. Could this be turned into a codealong lab?

Overall, I'm still pretty unclear at the end of this reading what shotgun actually is.

@matbalez

Eventmachine -1.0.8

This repo is not able to install 'Eventmachine 1.0.8' which is required by it.

suggest adding additional sentence to README.md

Hi LV team, consider adding the below bold sentence to the README.md to clarify that you need to refresh the browser, even when using shotgun: "Save your changes to app.rb and visit localhost:9393 in the browser. Hit 'Refresh' in your browser to make a new request. This time, you should see the text Started my server using shotgun! as opposed to seeing the previous text, just like with rackup."

Better explanation as to why Shotgun is awesome

Pretty much show them that with a normal rackup you have to re rackup after every change. Walk them through that, actually have the student do it. Then install Shotgun and show them how much nicer it is!

Can't actually run shotgun

Since this is a README and I'm not forking a repo to get code, I don't have a directory structure in which I can successfully run shotgun. I tried running it and it complained I did not have a config.ru file. It's not clear to me if I'm supposed to create such a file just to test that shotgun works?

Re: setup

Installing EventMachine via:

"Make sure you run bundle install to install the gems in our Gemfile. If your operating system is OSX El Capitan, and you have an issue installing EventMachine, first check to make sure Open SSL is installed by entering brew install openssl in terminal. Once it's installed, enter brew link openssl --force"

no longer works.

Solution:
$ gem install eventmachine -- --with-cppflags=-I/usr/local/opt/openssl/include

Source: http://stackoverflow.com/questions/30818391/gem-eventmachine-fatal-error-openssl-ssl-h-file-not-found

Kill rogue shotgun server

This may be helpful to others with the same issue.

I was unable to run shotgun because I had a connection somewhere else I was not aware of. I found this command that easily solved the issue.

The basic process is:
-you list open files
-you see the running shotfun server (with ID?)
-you kill that open file using the ID
-you are now able to run shotgun again for something else

$ lsof -i TCP:9393   # shotgun starts on port 9393
=> ruby    53033 <a target="_blank" title="Stefan" href=" http://www.sdraht.com">Stefan</a>    5u  IPv4 0x15b4cb18      0t0  TCP localhost:9393 (LISTEN)
$ kill 53033

source:
http://nimblespring.com/snippet-kill-a-rogue-server-process-in-terminal

typo

Should include the word "to" after "pass"-

"You can pass shotgun most of the CLI arguments and flags that rackup accepts."

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.