英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论