duancongduo4109 2014-07-05 14:26
浏览 352
已采纳

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

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

  • dongliangkeng1056 2014-07-05 14:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)