在Go + FastCGI中,使用多个处理程序有意义吗?

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

In Go + FastCGI, does it make any sense to use multiple handlers?

问题

Gopher新手在这里。请友善一点 在Go + FastCGI中,使用多个处理程序有意义吗?

我有一个设置,我在一个共享服务器上有一个帐户,该服务器运行Apache + FastCGI,我对其没有控制权。不过,它与Go无缝地进行交互。我更习惯使用net/http来使用Go,但是弄清楚如何使用net/http/fcgi似乎很简单。这是我的最小测试应用程序:

package main

import (
	"fmt"
	"net/http"
	"net/http/fcgi"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This was generated by Go running as a FastCGI app")
}

func main() {
	/*
	 *	
	 *	Everything that is done here should be setup code etc. which is retained between calls
	 *	
	 */
	http.HandleFunc("/", handler)
	// This is what actually concurrently handles requests
	if err := fcgi.Serve(nil, nil); err != nil {
		panic(err)
	}
}

现在这个工作得很好:将其编译为go-fcgi-test.fcgi并将其放置在适当的目录下后,可以通过URL http://my.shared.web.server/go-fcgi-test.fcgi 运行Go代码。为了简单起见,我省略了大部分实际处理过程,但是这可以很好地提取表单参数、环境变量(在Go 1.9下!)等等,所以我知道基本设置必须没问题。

让我们尝试一个稍微复杂一点的例子:

package main

import (
	"fmt"
	"net/http"
	"net/http/fcgi"
)

func handler1(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This comes from handler1")
}

func handler2(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This comes from handler2")
}

func main() {
	http.HandleFunc("/first", handler1)
	http.HandleFunc("/", handler2)
	if err := fcgi.Serve(nil, nil); err != nil {
		panic(err)
	}
}

在这种情况下,我期望http://my.shared.web.server/go-fcgi-test.fcgi输出This comes from handler2,实际上也确实是这样。

但是为什么http://my.shared.web.server/go-fcgi-test.fcgi/first实际上也调用了handler2,也就是说完全忽略了handler1?请注意,handler2确实获取到了URL的/first部分,Apache并没有将其去除,因为我可以读取r.URL.Path[1:]并确认这是发送给Go应用程序的完整路径。

我在网上找到的所有使用类似FastCGI框架的示例都只显示了一个处理程序。这是FastCGI包本身的限制吗?这是FastCGI协议的限制(但是为什么整个路径被正确发送了)?这是Apache配置中的某些限制(请记住,我无法更改Apache配置)?还是我做错了什么?

(为了完整起见,我应该补充说,是的,我已经尝试了上述的几种变体,重命名Go应用程序,使用子文件夹,使用多个处理程序而不仅仅是一个等等)

我的真实场景实际上是一个小应用程序,它可以作为一个独立的Web服务器使用net/http,或者作为FastCGI应用程序运行,如果独立模式不受支持或甚至被禁止(这是一些共享环境提供商的情况)。由于实际处理对于任一情况都是完全相同的,唯一的区别是调用fcgi.Serve()而不是http.ListenAndServe()。但是如果能够在FastCGI下使用net/http包的路由功能并具有不同的处理程序,那将是很好的。

提前感谢您的任何见解。即使答案是'是的,这正是Go下FastCGI实现的工作方式——只有一个处理程序!',那也会很有用——这意味着我只需要解决自己的代码并以不同的方式进行操作(基本上,根据通过Form接口传递的参数创建自己的路由器/调度程序——没什么大不了的,可以做到!)

英文:

Gopher newbie here. Please be kind 在Go + FastCGI中,使用多个处理程序有意义吗?

I have a setup where I do have an account on a shared server which runs Apache + FastCGI over which I have no control. It smoothly interfaces with Go, though. I'm more used to using Go with net/http, but figuring out how to use it with net/http/fcgi seemed simple enough. Here is my minimal test application:

package main

import (
	"fmt"
	"net/http"
	"net/http/fcgi"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This was generated by Go running as a FastCGI app")
}

func main() {
	/*
	 *	
	 *	Everything that is done here should be setup code etc. which is retained between calls
	 *	
	 */
	http.HandleFunc("/", handler)
	// This is what actually concurrently handles requests
	if err := fcgi.Serve(nil, nil); err != nil {
			panic(err)
	}
}

Now this works beautifully and flawlessly: after compiling this to, say, go-fcgi-test.fcgi and placing it under the appropriate directory, the Go code is run from an URL like http://my.shared.web.server/go-fcgi-test.fcgi. For the sake of simplicity, I've left most of the actual processing out — but this works perfectly with extracting form parameters, ENV variables (under Go 1.9!) and so forth, so I know that the basic setup must be ok.

Let's try a slightly more complicated example:

package main

import (
	"fmt"
	"net/http"
	"net/http/fcgi"
)

func handler1(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This comes from handler1")
}

func handler2(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-type", "text/plain; charset=utf-8")
	fmt.Fprintln(w, "This comes from handler2")
}

func main() {
	http.HandleFunc("/first", handler1)
	http.HandleFunc("/", handler2)
	if err := fcgi.Serve(nil, nil); err != nil {
			panic(err)
	}
}

Now, in this scenario, I would expect http://my.shared.web.server/go-fcgi-test.fcgi to output This comes from handler2, and, indeed, that's exactly what happens.

But why does http://my.shared.web.server/go-fcgi-test.fcgi/first actually invoke handler2 as well, i.e. handler1 is completely ignored? Note that handler2 does get the /first bit of the URL — Apache is not stripping it out — because I can read r.URL.Path[1:] and confirm that this was the whole path sent to the Go application.

All examples I've found on the Web using a similar skeleton for FastCGI show only one handler. Is this a limitation of the FastCGI package itself? A limitation of the FastCGI protocol (but then why is the whole path correctly sent?)? Something done at the Apache configuration which imposes this limitation (remember, I cannot touch the Apache configuration)? Or am I doing something terribly wrong?

(For the sake of completeness, I should add that yes, I have tried out several variations of the above, renaming the Go app, using subfolders, using several handlers and not just one, etc. and so forth)

My real world scenario is actually a small application that is supposed to run either as a stand-alone web server using net/http or as a FastCGI application in the case that the stand-alone model is either not supported or even forbidden (which is the case of some providers of shared environments). Since the actual handling is exactly the same for either case, the only difference is calling fcgi.Serve() as opposed to http.ListenAndServe(). But it would be nice to be able to use the routing ability of the net/http package with different handlers under FastCGI as well.

Thanks in advance for any insight. And even if the answer is 'yes, that's exactly how the FastCGI implementation works under Go — one handler only!' that would still be useful — meaning that I just need to work around my own code and do things differently (basically, creating my own router/dispatcher based on parameters passed through the Form interface — no big deal, it's doable!)

答案1

得分: 2

我意识到这是一个旧帖子,但我刚开始尝试使用Go和fcgi,并遇到了同样的问题。

简短的答案是,使用多个处理程序是有意义的。你示例中的问题在于没有考虑到go-fcgi-test.fcgi是URL的一部分。

当Go的ServeMux处理URL时,它使用的是请求的完整URL,而不仅仅是由你的fcgi进程处理的部分。在http://my.shared.web.server/go-fcgi-test.fcgi/first的情况下,程序会寻找与/go-fcgi-test.fcgi/first最接近的匹配项,即/

英文:

I realize this is an old post, but I'm just starting to play with Go and fcgi myself and came across this same issue.

The short answer is yes, it does make make sense to use multiple handlers. The flaw in your example is that you aren't accounting for the go-fcgi-test.fcgi being part of your URL.

When Go's ServeMux processes the URL, it is using the full URL of the request, not just the part being handled by your fcgi process. In the case of http://my.shared.web.server/go-fcgi-test.fcgi/first, the program is looking for the closest match to /go-fcgi-test.fcgi/first, which is /.

答案2

得分: 0

*更新2023-02-24:*我在下面的假设中完全错误了。@Kodiak是正确的,我只是没有完全理解。几年过去了,多亏了下面的@Hudon的评论(也许还有更多的经验...),我撤回了我以前的答案,并接受@Kodiak的答案作为正确答案(它确实是正确的)。

我只是让以下内容保留作为历史记录。请注意,它并不是“完全错误的”,但它是基于我对传递给ServeMux的实际URL的无知。现在我知道了:)


<strike>在阅读了@Kodiak提供的答案之后,我重新阅读了ServeMux的文档,并看到了以下段落:

> 请注意,由于以斜杠结尾的模式命名了一个根子树,因此模式“/”匹配所有未被其他已注册模式匹配的路径,而不仅仅是Path ==“/”的URL。
>
> 如果已经注册了一个子树,并且收到了命名该子树根目录的请求而没有尾随斜杠,ServeMux会将该请求重定向到子树根目录(添加尾随斜杠)。可以使用不带尾随斜杠的路径进行单独注册来覆盖此行为。例如,注册“/images/”会导致ServeMux将对“/images”的请求重定向到“/images/”,除非已单独注册“/images”。

(斜体是我自己加的)

我的假设ServeMux基本上像nginx和/或Apache的rewrite模块的基于规则的模式匹配功能一样,即根据它们注册的顺序处理规则,因此我期望/first会首先匹配(双关语),只有在找不到匹配项时,才会匹配/

但是文档并没有这样说。相反,在我的给定场景中,ServeMux因为我忘记添加尾随斜杠,将始终回退到&quot;/&quot;的处理程序,这不是因为某种错误或匹配算法的变态,而是因为这是Go开发人员编写的预期行为!换句话说,如果你有一个&quot;/&quot;的处理程序,它就像一个捕捉所有非斜杠结尾子树的处理程序。

我只是没有正确阅读文档!(或者也许在2017年,那段话不够清楚)</strike>

英文:

Update 2023-02-24: I was completely wrong in the assumptions below. @Kodiak was right, I just didn't quite get it. A few years later, and, thanks to @Hudon who commented below (and perhaps a bit more experience...), I'm rejecting this old answer of mine, and accepting @Kodiak's as the correct one (which it is).

I'm just letting the following remain for historical purposes. It's not 'completely wrong', mind you; but it is based on my ignorance of what the actual URL was being passed to ServeMux. Now I know 在Go + FastCGI中,使用多个处理程序有意义吗?


<strike>After reading the answer provided by @Kodiak, I re-read the documentation for ServeMux and came across this paragraph:

> Note that since a pattern ending in a slash names a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".
>
> If a subtree has been registered and a request is received naming the subtree root without its trailing slash, ServeMux redirects that request to the subtree root (adding the trailing slash). This behavior can be overridden with a separate registration for the path without the trailing slash. For example, registering "/images/" causes ServeMux to redirect a request for "/images" to "/images/", unless "/images" has been registered separately.

(italics mine)

My assumption was that ServeMux pretty much behaved as the rule-based pattern matching features of nginx and/or Apache's rewrite module, i. e. rules are processed according to the order they're registered, and therefore I was expecting that /first would be matched first (pun intended), and only if no match was found, / would be matched next.

But this is not what the documentation says. Instead, the order of registration makes no real difference; ServeMux, in my given scenario, because I forgot adding a trailing slash, will always 'fall back' to the handler for &quot;/&quot;, not because of some bug or perversion of the matching algorithm, but because that is the intended behaviour as coded by the Go developers! In other words, if you have a handler for &quot;/&quot;, it acts as a catch-all for every non-slash-terminated subtree.

I just failed to read the documentation properly! (or maybe back in 2017 that paragraph wasn't clear enough)</strike>

huangapple
  • 本文由 发表于 2017年9月12日 17:07:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/46172287.html
匿名

发表评论

匿名网友

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

确定