douhao6557 2014-12-16 04:30
浏览 395

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

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条回答 默认 最新

  • doumaojin4008 2014-12-16 06:33
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100