首页 > 其他分享 >Go - Serving Through HTTPS

Go - Serving Through HTTPS

时间:2023-10-17 21:37:36浏览次数:31  
标签:Serving http certificate private pem Through key Go your

Problem: You want to serve your web application through HTTPS.


Solution: Use the http.ListenAndServeTLS function to serve your web application through HTTPS.

 

HTTPS is nothing more than layering HTTP on top of the Transport Security Layer (TLS). The net/http package provides the http.ListenAndServeTLS function to serve your web application through HTTPS.

package   main 

import   ( 
      "net/http" 
) 

func   main ()   { 
      http . HandleFunc ( "/" ,   index ) 
      http . ListenAndServeTLS ( ":8000" ,   "cert.pem" ,   "key.pem" ,   nil )
}
func index ( w http . ResponseWriter , r * http . Request ) { w . Write ([] byte ( "Hello World" )) }

Most of the code is the same, except you need a certificate file cert.pem and a private key file key.pem . The cert.pem is the SSL certificate, while key.pem is the private key for the server. In a production scenario, you will need to get the SSL certificate from a certificate authority (CA) like VeriSign or Thawte or Comodo SSL, or you can use Let’s Encrypt to get a free one. However, if you just need a certificate and private key to try things out, you can generate self-signed ones using OpenSSL. 

Run this from the command line:

$  openssl  req  - x509  - newkey  rsa:4096  - keyout  key.pem  - out  cert.pem  - days  365 - nodes

OpenSSL is an open source implementation of SSL and TLS. The library contains a command - line tool with the same name that can do a number of things, including creating private keys and SSL certificates.
The req command is used to manage certificate requests but can also be used to create self - signed certificates. Option - 509 tells the tool to create self - signed certificates (X.509 is an International Telecommunication Union standard defining the format of public key certificates). The option - newkey tells the tool to generate a new private key. The argument rsa:4096 tells the tool to create a key that is of size 4,096 bits. The - keyout and - out options tell the tool to create the private key and certificate files with the respective names given in the argument. The option - days specifies the number of days to certify the certificate. Here you use 365, which means the certificate is valid only for 1 year. Finally, use the - nodes option (which means, “no DES” rather than “nodes”) to say you don’t want to encrypt the private key. 

When you run the command, you should see something like this:

Generating  a  4096  bit  RSA  private  key
.........................................................................
...................................++++
.........................................................................
.........................................................................
...................................................++++
writing  new  private  key  to  'key.pem'
\ - - - - -
You  are  about  to  be  asked  to  enter  information  that  will  be  incorporated
into  your  certificate  request.
What  you  are  about  to  enter  is  what  is  called  a  Distinguished  Name  or  a  DN.
There  are  quite  a  few  fields  but  you  can  leave  some  blank
For  some  fields  there  will  be  a  default  value,
If  you  enter  '.',  the  field  will  be  left  blank.
\ - - - - -
Country  Name  (2  letter  code)  []:SG
State  or  Province  Name  (full  name)  []:
Locality  Name  (eg,  city)  []:
Organization  Name  (eg,  company)  []:Go  Cookbook
Organizational  Unit  Name  (eg,  section)  []:
Common  Name  (eg,  fully  qualified  host  name)  []:localhost
Email  Address  []:[email protected]

This is the interactive mode. You can press the return key to leave the entries empty except for the common name. The common name is the fully qualified domain name of the server you want to protect. In this case, it’s for testing, so you can just
enter localhost . 

Once you’re done, you should have two files — the cert.pem and key.pem files. Copy them into the same directory as the main.go file. Then run the code again to start the server.

While there would be uses for configuring TLS this way, most likely, if you are deploying your web application in a production environment, you will use a reverse proxy like Nginx or Apache to handle the TLS termination. This way, you can offload the TLS termination, which will improve the performance of your web application. This means your web application can still run with HTTP while it is fronted by a reverse proxy that runs through HTTPS.

 

标签:Serving,http,certificate,private,pem,Through,key,Go,your
From: https://www.cnblogs.com/zhangzhihui/p/17770740.html

相关文章

  • 从内存使用角度的比较:Go vs Rust
    Go和Rust是最近几年非常火的语言,经常有人问到底该怎么选择,特别是谁更适合搭建网络后台服务,哪一个性能更好,稳定性更高。网络上Go和Rust的比较文章很多,大体上是做一个测试或写几段测试代码,根据运行的时长来比较哪个性能更好,但这种测试可能会陷入误区:1)比来比去,比的是网络IO,因为这种......
  • Go - Creating a JSON Web Service API
    Problem: YouwanttocreateasimplewebserviceAPIthatreturnsJSON.Solution: Usethenet/httppackagetocreateawebserviceAPIandtheencoding/jsonpackagetoencodedatatobesentbackasJSON. You’llcreateawebserviceAPIthatreturnsa......
  • RunnerGo UI自动化使用体验
    首先需要进入官网,RunnerGo支持开源,可以自行下载安装,也可以点击右上角体验企业版按钮快速体验点击体验企业版进入工作台后可以点击页面上方的UI自动化进入到测试页面创建元素我们可以在元素管理中创建我们测试时需要的元素这里我们以一个打开百度搜索的场景,添加了百度输入框和百度......
  • Go - Serving Static Files
    Problem: Youwanttoservestaticfilessuchasimages,CSS,andJavaScriptfiles.Solution: Usethehttp.FileServerfunctiontoservestaticfiles. funcmain(){dir:=http.Dir("./static")fs:=http.FileS......
  • RunnerGo UI自动化使用体验
    RunnerGo怎么做UI自动化首先需要进入官网,RunnerGo支持开源,可以自行下载安装,也可以点击右上角体验企业版按钮快速体验 点击体验企业版进入工作台后可以点击页面上方的UI自动化进入到测试页面 创建元素我们可以在元素管理中创建我们测试时需要的元素 这里我们以一个......
  • 用go封装和实现扫码登录
    用go封装和实现扫码登录本篇为用go设计开发一个自己的轻量级登录库/框架吧-秋玻-博客园(cnblogs.com)的扫码登录业务篇,会讲讲扫码登录的实现,给库/框架增加新的功能,最后说明使用方法Github:https://github.com/weloe/token-go扫码登录流程首先我们需要知道扫码登录流程打......
  • Django必会三板斧
    HttpResponse=========返回字符串类型的数据render      ==========返回html页面并且支持传值redirect  =========重定向 使用方法:fromdjango.shortcutsimportrender,HttpResponse,redirectdefindex(request):""":paramrequest:请......
  • 解决TypeError: read_excel() got an unexpected keyword argument ‘parse_cols or
    解决TypeError:read_excel()gotanunexpectedkeywordargument‘parse_cols'或‘sheetname‘在使用pandas包进行Excel文件处理时,有时候会遇到TypeError:read_excel()gotanunexpectedkeywordargument‘parse_cols'或TypeError:read_excel()gotanunexpectedkeyword......
  • 使用docker搭建drogon windows10,linux,mac下开发环境
    2023年10月13日14:52:26本机环境Windows10专业版22H2操作内核19045.2965如果直接在windows,linux,mac上直接搭建环境确实有一点难度,之前drogon官方并未提供官方镜像,现在有了docker镜像确实方便了,其实我是最近才有简述安装dockerdesktop,windows的虚拟化有2个方案hyper-v和w......
  • 【转】,接上面3篇.Implement Sql Database Driver in 100 Lines of Go
    原文: https://vyskocil.org/blog/implement-sql-database-driver-in-100-lines-of-go/ -------------------- ImplementSqlDatabaseDriverin100LinesofGo2019.02.18Go database/sql definesinterfacesforSQLdatabases.Actualdrivermustbeimplemented......