The Crystal Programming Language Forum

The example of HTTP::Server does not start

Hi! Please tell me how to run an example of HTTP Server in the reference book.

require "http/server"
server = HTTP::Server.new do |context|
   context.response.content_type = "text/plain"
   context.response.print "Hello world! The time is #{Time.local}"
end
address = server.bind_tcp 8080
puts "Listening on http://#{address}"
server.listen

When I trying “build” or “run” program I get error.

Error: execution of command failed with code: 1:…

When I launching through “play”, I get a site playground, but I think I get a white page with text “Hello, World”.
What am I doing wrong? How launching this code?

Hey! Welcome to Crystal.

Assuming you have the latest crystal installed and all that, you just take that code sample and put it in to a file named server.cr, and then from your terminal, you can run crystal server.cr. That will output the “Listening on Listening on http://127.0.0.1:8080”.

That’s it!

To create a single binary file for this, you would run crystal build --release server.cr. Then to run that file, you just run ./server. Give that a shot, and let us know how it goes.

1 Like


I tried to run through crystal server.cr and crystal build --release server.cr but everyone get this error.

@Mostik for future posts, it’s always good to post the entire error message. The error above suggests the app is compiling against a wrong version of SSL. This can’t be deduced from the original error you posted: “Error: execution of command failed with code: 1:…”.

Ok, so a few questions here to get on the same page:

What OS are you on? It looks like you maybe installed with snap? You could maybe take a look at an alternate install method: https://crystal-lang.org/install/

Then if you’re able to get it to work with an alternate way, let the community know just in case there’s an issue with how the snap does installed. (I’ve never used it, so I don’t know how that works).

Hi @Mostik , probably you are missing some of the required libraries for TLS/SSL or you are using an outdated version of it

Can you show use output of the following commands?

$ pkg-config --libs libssl
$ pkg-config --libs libcrypto

For this small example you would be fine probably turning off the integration for SSL, but is not the first suggestion I would do. Yet, to do that use the following command:

$ crystal run server.cr -Dwithout_openssl
$ pkg-config --libs libssl - not found
$ pkg-config --libs libcrypto - found

Then I ran $ crystal run server.cr -Dwithout_openssl the program started. Yes it worked!
Then I tried to ran $ crystal run server.cr and it worked too! Although I didn’t install anything.
Then I ran $ pkg-config --libs libssl again it found. This is strange.
Everything works now!
Thank you so much!

1 Like

That’s the whole output of pkg-config? It should have been somthing like

$ pkg-config --libs libssl
-L/path/to/openssl@1.1/1.1.1g/lib -lssl

The second run you did work becuase the program was already compiled in the first place and the object files were reused.

If you remove the temporal files with $ rm -rf $(crystal env CRYSTAL_CACHE_DIR) you will be in a clean state again.

I would recommend install openssl or libressl.

$ pkg-config --libs libssl
Before it looked like this (Sorry I just forgot to take a screenshot )

Package libssl was not found in the pkg-config search path.
Perhaps you should add the directory containing `libssl '
to the PKG_CONFIG_PATH environment variable
Package 'libssl', required by 'virtual: world', not found

After that it looked like this

-lssl

I deleted the temporary files, but everything works.
I created a new project and everything works there too.

I already had openssl installed.

I also reinstalled Сrystal using pacman, but it still didn’t work, but it may have somehow affected it when combined with something else.