drrkgbm6851 2016-08-31 23:01
浏览 425

在Go中构建URL(包括服务器方案)

I am creating a REST API in Go, and I want to build URLs to other resources in my replies.

Based on the http.Response I can get the Host and URL.

However, how would I go about getting the transport scheme used by the server? http or https?

I attemped to check if server.TLSConfig is nil and then assuming it is using http since it says this in the documentation for http.Server:

TLSConfig    *tls.Config   // optional TLS config, used by ListenAndServeTLS

But it turns out this exists even when I do not run the server with ListenAndServeTLS.


Or is this way of building my URLs the wrong way of doing things? Is there some other normal way of doing this?

  • 写回答

1条回答 默认 最新

  • drlhsfqoa350437979 2016-09-01 00:29
    关注

    My preferred solution when running http and https is just to run a simple listener on :80 that redirects all traffic to https. Then any real traffic can be assumed to be https.

    Alternately I believe you can access a request's URL at req.URL.Scheme to see the protocol.

    Or do you mean for the entire application? If you accept configuration to switch between http and https, then can't you look at that and see which they chose? I guess I'm missing some context maybe.

    It is also common practice for apps to take a baseURL via flag or config to generate external urls with.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测