Golang:使用Goji从Go Web应用程序同时提供HTTP和HTTPS的正确方法是什么?

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

Golang: Right way to serve both http & https from Go web app with Goji?

问题

这是一个处理同时处理HTTP和HTTPS流量的单个Go Web应用程序(使用Goji框架)的正确方法吗?

package main

import (
	"fmt"
	"net/http"
	"github.com/zenazn/goji/graceful"
	"github.com/zenazn/goji/web"
)

func main() {
	r := web.New()
	//https://127.0.0.1:8000/r
	r.Get("/r", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %s!", "r")
	})

	go graceful.ListenAndServeTLS(":8000", "cert.pem", "key.pem", r)

	r1 := web.New()
	//  http://127.0.0.1:8001/r1
	r1.Get("/r1", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %s!", "r1")
	})

	graceful.ListenAndServe(":8001", r1)
}

或者,有没有更好的方法让单个Go Web应用程序监听8000和8001端口?

英文:

Is this the right way to for a single Go web app (using Goji) to handle both http and https traffic?

package main

import (
	"fmt"
	"net/http"
	"github.com/zenazn/goji/graceful"
	"github.com/zenazn/goji/web"
)

func main() {
	r := web.New()
	//https://127.0.0.1:8000/r
	r.Get("/r", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %s!", "r")
	})

	go graceful.ListenAndServeTLS(":8000", "cert.pem", "key.pem", r)

	r1 := web.New()
	//  http://127.0.0.1:8001/r1
	r1.Get("/r1", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello, %s!", "r1")
	})

	graceful.ListenAndServe(":8001", r1)
}

Or what is the best method for having both ports 8000 and 8001 listened to by a single Go web app?

答案1

得分: 3

你不需要创建一个新的对象,只需将 r 传递给 graceful.ListenAndServe(":8001", r) 即可,除非你有其他依赖于 HTTPS 的操作。

英文:

You don't need to create a new object, you can simply pass r to graceful.ListenAndServe(":8001", r), unless of course you do a different action that depends on https.

huangapple
  • 本文由 发表于 2014年7月5日 22:26:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/24587386.html
匿名

发表评论

匿名网友

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

确定