How do I pass the response writer and http request to an executable in Go?

huangapple go评论91阅读模式
英文:

How do I pass the response writer and http request to an executable in Go?

问题

我想在Go语言中运行一个简单的Web服务器,实现一些基本的授权和路由功能,用于多个应用程序。

是否可以将Web服务器作为独立的可执行文件运行,并将响应写入器(response writer)和HTTP请求传递给其他可执行文件?

这个想法是希望应用程序的二进制文件可以独立于Web服务器进行编译和部署。

英文:

I want to run a simple webserver in Go doing some basic authorisation and routing to multiple apps.

Is it possible to have the webserver running as a standalone executable and pass the response writer and http request to other executables?

The idea is that the app binaries can hopefully be compiled and deployed independently of the webserver.

答案1

得分: 3

正在翻译以下内容:

运行应用程序的内存区域是隔离的:一个进程不能直接读取或写入另一个应用程序的内存(维基百科:进程隔离)。

因此,仅仅传递响应写入器和HTTP请求并不那么容易。即使你实现了它(例如将它们序列化为二进制或文本数据,以某种方式发送/传递它们,并在另一端重建它们),在后台提供HTTP请求不仅仅涉及与ResponseWriterRequest对象交互:它还涉及从底层TCP连接读取和写入...所以你还必须“传递”TCP连接或在真正的HTTP客户端和你转发到的应用程序之间创建一个桥接。

另一个选择是在进行身份验证和路由逻辑之后向客户端发送重定向(HTTP 3xx状态码)。使用这种解决方案,你可以在应用程序中实现身份验证和某些路由逻辑,但你将失去进一步的路由可能性,因为进一步的请求将直接发送到指定的主机。

实质上,你尝试创建的是一个代理服务器的功能,市面上有很多实现。鉴于一个良好的代理服务器的复杂性,重现一个代理服务器应该是不可行的。

我建议要么利用现有的代理服务器,要么“重构”你的架构以避免这种分割。

英文:

Memory areas of running applications are isolated: a process cannot just read or write another application's memory (Wikipedia: Process isolation).

So just passing the response writer and the http request is not so easy. And even if you would implement it (e.g. serializing them into binary or text data, sending/passing them over somehow, and reconstructing them on the other side) serving an HTTP request in the background is more than just interacting with the ResponseWriter and Request objects: it involves reading from and writing to the underlying TCP connection... so you would also have to "pass" the TCP connection or create a bridge between the real HTTP client and the application you forward to.

Another option would be to send a redirect back to the client (HTTP 3xx status codes) after doing the authentication and routing logic. With this solution you could have authentication and certain routing logic implemented in your app, but you would lose further routing possibilities because further request would go directly to the designated host.

Essentially what you try to create is the functionality of a proxy server which have plenty of implementations out there. Given the complexity of a good proxy server, it should not be feasible to reproduce one.

I suggest to either utilize an existing proxy server or "refactor" your architecture to avoid this kind of segmentation.

huangapple
  • 本文由 发表于 2015年3月31日 18:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/29366722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定