在GoLang服务器上的开发分支,多个listenAndServes

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

Development Branch on a GoLang Server, multiple listenAndServes

问题

我正在为我的服务器开发分支工作。想法是在两个不同的文件夹中使用相同的git分支的两个克隆,这两个文件夹在某种程度上是相同的。第一个文件夹将是实时环境,第二个文件夹将是开发环境。

目前,我先启动实时环境的go项目,然后再启动开发环境的go项目。然后,整个网站会在"www.k.com/"和"www.k.com/developer/"上进行镜像。

问题是,这样做不起作用。当我启动第二个go应用程序时,一切都正常运行,但是最后一行的"http.ListenAndServe"没有起作用。我也没有看到任何由Listen和Serve抛出的错误。这使得我的服务器正常工作,但是开发者页面会抛出404错误,这意味着第二个脚本没有起作用。无论我先运行哪个,都会发生这种情况,也就是说,如果我先运行开发环境,开发者部分就会工作,而正常网站则不会。

prefix := "/"
if utilities.Dev() {
    prefix = "/developer"
}

router := mux.NewRouter()
subrouter := router.PathPrefix(prefix).Subrouter()

subrouter.HandleFunc("/", controllers.HomeHandler).Methods("GET")
subrouter.HandleFunc("/", controllers.HomeSessionHandler).Methods("POST")
subrouter.HandleFunc("/team", controllers.TeamHandler).Methods("GET")
subrouter.HandleFunc("/contact", controllers.ContactHandler).Methods("GET")

http.Handle("/", router)
http.ListenAndServe(":80", nil)

那么,如何在两个不同的进程中使用ListenAndServe来合并两个项目的路由?肯定有办法,如果没有,我该如何创建这样的开发环境呢?

英文:

I'm working on a development branch for my server. The idea is to use two clones of the same git branch in two different folders, which more or less are identical. The first folder will be live, the second will be my developer.

At the moment, I'm launching the live-branch go project and later launching the developer-branch go project. Then, the entire site gets mirrored on "www.k.com/" and "www.k.com/developer/".

The problem is, this doesn't work. When I launch my second go application, everything runs fine but the last line "http.ListenAndServe" doesn't catch. There's no error that I know of that gets thrown by Listen and Serve either. This leaves my server functional but the developer/ pages throw 404's, which means the second scipt isn't doing anything. This happens to whichever I run second, meaning if I run developer then live the developer section works and the normal site doesn't.

prefix := "/"
if(utilities.Dev()){
	prefix = "/developer"
}

router := mux.NewRouter()
subrouter := router.PathPrefix(prefix).Subrouter()

subrouter.HandleFunc("/",controllers.HomeHandler).Methods("GET")
subrouter.HandleFunc("/",controllers.HomeSessionHandler).Methods("POST")
subrouter.HandleFunc("/team", controllers.TeamHandler).Methods("GET")
subrouter.HandleFunc("/contact", controllers.ContactHandler).Methods("GET")

http.Handle("/", router)
http.ListenAndServe(":80", nil)

So how can I use ListenAndServe in two different processes to combine routes from two projects? Surely there must be a way, and if not, how else would I go about creating a development environment like this?

答案1

得分: 1

你的问题是你试图从两个不同的应用程序上监听端口80(我假设你在同一台机器上运行这些应用程序?)。老实说,我不确定为什么你没有从http.ListenAndServe得到错误 - 你肯定应该得到一个“bind: address already in use”的错误。

关于如何解决这个问题,简短的答案是你不能。至少不是如果你想从两个独立的子进程中运行它。

较长的答案是,如果你愿意设置一个HTTP代理来拦截网络流量,然后将其路由到正确的应用程序(也就是说,你的两个应用程序都监听除80以外的端口,而你的代理将监听端口80并根据URL将流量路由到正确的应用程序),那么你是可以实现的。

另一种选择是将你的功能实现为包。一个包是生产包,一个是开发包,你的主包会要求每个包注册处理程序。然后主包可以自己运行http.ListenAndServe,你仍然可以分别开发生产和开发分支。

英文:

Your problem is that you're trying to listen on port 80 from two different applications (I assume you're running these on the same machine?). To be honest, I'm not sure why you're not getting an error from http.ListenAndServe - you should definitely be getting a "bind: address already in use" error.

In terms of how to get this to work, the short answer is that you can't. At least not if you want to run this from two separate subprocesses.

The longer answer is that you can if you're willing to set up an HTTP proxy to intercept web traffic and then route it to the proper application (that is, you'd have both applications listening on ports other than 80, and your proxy would listen on port 80 and route to the proper application depending on the URL).

Another alternative would be to have your functionality implemented as packages. One would be the production package and one would be the development package, and your main package would ask each of these packages to register handlers. Then the main package could run http.ListenAndServe itself, and you'd still get to develop your production and development branches separately.

huangapple
  • 本文由 发表于 2014年12月16日 12:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/27497542.html
匿名

发表评论

匿名网友

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

确定