Servers and our long battle with Shotgun

Emma Priester
3 min readMar 15, 2021

This week we’ll be working towards getting our first web app online — using Heroku and Sinatra. During the precourse, I already looked into this and got a simple Ceaser cypher online here: https://glacial-taiga-66850.herokuapp.com. Whilst doing this I did not fully understand what was happening, so this week, I will try and figure it out step by step.

I started by trying to understand Servers and Clients, and in this post, I will try and explain this the way that I understand it now.

I started by setting up a simple host, as given to me by makers.

require ‘socket’

server = TCPServer.new(2345)

socket = server.accept

socket.puts “What do you say?”

they_said = socket.gets.chomp

socket.puts “You said: #{they_said}. Goodbye!”

socket.close

After putting this in atom, I ran it in one terminal using ‘ruby servers_1.rb’ (servers_1.rb is the name of the file I created. In a separate terminal, I ran ‘telnet localhost 2345’. A thing I ran into first, is that you have to run ‘ruby servers_1.rb’ first, otherwise telnet will not be able to connect to a remote host. Don’t close it, it will close itself!

You can also find your localhost in your web browser, running it on the command line. You can run this by simply calling ruby filename.rb, or by calling shotgun filename.rb. Shotgun is a gem that allows you to update your file in your browser when you make changes in your file. Shotgun, however, proved to be more of a challenge than we thought it would.

The main problem, that took us almost half an hour to figure out, is that shotgun does not work with the latest version of ruby (ruby 3.0.0). So we ran rvm ruby 2.7.2 to get us down to an earlier version. The next issue with shotgun is, that it is not super secure, so you have to add security to your file. That can simply be

set :session_secret, ‘any sort of password’

We still ran into some errors, because we ran ‘shotgun filename.rb’, and our localhost got confused. This was solved by running ‘shotgun filename.rb -p 4567’. By running -p we told shotgun what path to follow, and 4567 would be our localhost. It all worked!

Now we could start playing around with stuff on the server.

get “/” do
“Hello World”
end

get ‘/secret’ do
“foodbar”
end

get ‘/taran’ do
“Hi Taran”
end

get ‘/emma’ do
“Hi Emma”
end

get ‘/bloop’ do
“bloopdywoop”
end

Do remember to require Sinatra in this file, and have both Sinatra and shotgun saved in the gemfile! After saving this, and running shotgun, we went to our browser, and ran http://localhost:4567/, which led us to a page that said ‘Hello World’, just as our function told it to! We tested if all the above worked, and added some more, which we could immediately check out because of Shotgun. After this we played around some more, creating an erb file and adding some html and css to our file. More about that tomorrow!

--

--