-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HTTP::Client and HTTP::Server on Unix Socket #2735
Comments
Preferably i'd like to tackle this issue but seems like https://github.com/crystal-lang/crystal/blob/master/src/socket/unix_server.cr needs to be improved. I've already managed to make bind https://github.com/crystal-lang/crystal/blob/master/src/http/server/server.cr#L135-L137 to create Any tips and leads are welcome 😄 |
My only comment is that I wouldn't accept a string for this, in HTTP::Server constructor, as it's confusing with a host String parameter. Maybe use a required named argument, or a UnixServer instance. |
@asterite how about? HTTP::Server.new(socket: "/tmp/server.sock") {|context|} |
How about dropping both HTTP::Server.new
HTTP::Server.new("localhost")
HTTP::Server.new("localhost:443")
HTTP::Server.new("[::1]")
HTTP::Server.new("[::1]:443")
HTTP::Server.new("127.0.0.1:443")
HTTP::Server.new("0.0.0.0:443")
HTTP::Server.new("tcp:localhost:443")
HTTP::Server.new("tcp:[::]")
HTTP::Server.new("unix:server.sock") # relative
HTTP::Server.new("unix:/run/server.sock") # absolute |
@jhass it sounds like a good idea but i guess it's not common to create a server that way (or is it?) 🤔 |
In puma you add listeners to a single server: server = Puma::Server.new
server.add_tcp_listener(host, port)
server.add_ssl_listener(host, port, ctx)
server.add_unix_listener(path) |
Having multiple listeners, both unix-socket and tcp, ssl seems like something one very well might want to do for a service (if not using nanomsg, ZMQ, etc.)! |
If there's a final decision for the API, i'd like to tackle this //cc @asterite |
I'm in favour of the |
Is there any active work on this issue? |
No, there isn't. I think it got stuck at the design phase with so many variants and no clear winner way forward. I'm with @jhass on this. Sounds like adding more listeners would be a topic for a separate issue, wouldn't it? |
I think it'd be great to accept a single argument with an URI (either as |
To get this moved on, I would propose the following API (with simplified bodies): # existing overloads for TCP server with port/port+host:
def self.new(host : String, port : Int32)
TCPServer.new(host, port)
end
def self.new(port : Int32)
new("127.0.0.1", port)
end
# new socket overload:
def self.new(*, socket : String)
UNIXServer.new(socket)
end
# URI overloads:
def self.new(uri : URI)
if port = uri.port
if host = uri.host
new(host, port)
else
new(port)
end
elsif path = uri.path
new(socket: path)
else
raise "URI requires either port or path"
end
end
def self.new(uri : String)
new(URI.parse(uri))
end However, with 4 variants for each ( A different approach would be to deprecate the current API and only assign handlers in the initializer (i.e. only 4 total overloads) and move the binding target to the |
Note: this is almost covered by #9543 |
Coming back to this issue, I think we can close it. Both server and client are supported. The API can still be improved on the client but still, is working. require "http/server"
server = HTTP::Server.new do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world!"
end
server.bind_unix "/tmp/my-socket.sock"
puts "Listening"
server.listen require "http/client"
client = HTTP::Client.new(UNIXSocket.new("/tmp/my-socket.sock"))
response_body = client.get("/test").body
pp! response_body
client.close Testing the server via curl can be done via
|
Currently HTTP::Server only listens on TCP.
It'd be really great to listen on a Unix socket like Unicorn, Thin, Puma e.g
An API like this
The text was updated successfully, but these errors were encountered: