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

标签 https go goji

对于单个 Go 网络应用程序(使用 Goji)来说,这是处理 http 和 https 流量的正确方法吗?

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 网络应用同时监听端口 8000 和 8001 的最佳方法是什么?

最佳答案

你不需要创建一个新对象,你可以简单地将r传递给graceful.ListenAndServe(":8001", r),当然除非你执行取决于 https 的不同操作。

关于https - 戈朗 : Right way to serve both http & https from Go web app with Goji?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587386/

相关文章:

go - CryptoJS.AES 和 Golang

Goji 子路由器返回 404

google-app-engine - 定义了一个带有绑定(bind)参数但得到 404 的 Goji 路由

java - 使用 ant/ivy 从 nexus 检索依赖项之间的 http 身份验证?

ssl - 使用自托管 GitLab 的自签名证书 SSL 错误

apache - 页面加载时将 https 设置为默认值

algorithm - 如何在golang中实现可变字节编码算法

http - 通过 HTTPS 包含 Twitter Widgets.js

oracle - 如何解析层级数据

goji/httpauth - 不能与标准 Golang 'net/http' 库一起使用