I am noob, how do I keep the HTTP server running if I want to start it from the command line?

I have a very simple HTTP server that I want to run

server.jl
using Pkg

Pkg.add("HTTP")

using HTTP

# if false
ENV["PORT"] = "8080"
# end

print(ENV["PORT"])

const PORT = parse(Int, ENV["PORT"])

using HTTP

# HTTP.listen! and HTTP.serve! are the non-blocking versions of HTTP.listen/HTTP.serve
server = HTTP.serve!(PORT) do request::HTTP.Request
   @show request
   @show request.method
   @show HTTP.header(request, "Content-Type")
   @show request.body
   try
       return HTTP.Response("Hello")
   catch e
       return HTTP.Response(400, "Error: $e")
   end
end

#close(server)

So I try to trigger it using julia server.jl from bash and it seems to close instantly.

I am very new to this web things, what do I need to do to keep this server running so I can test it by sending request to it?

Either use the blocking call or put in a long sleep or something else which keeps your script from exiting.

1 Like

you can julia -i script.jl so it drops you into interactive session after loading the script

I want to eventually run this from docker. Yeah so no need to drop into an interactive session

Put wait(Condition()) at the end of the script and it will stay open.

You can just wait(#= for the =# server).

1 Like

Yeah, you can call wait(server) or just don’t use the HTTP.serve! bang version and use the blocking version like HTTP.serve(...)

1 Like