当前位置: 首页>>代码示例>>Golang>>正文


Golang Server.ListenAndServeTLS方法代码示例

本文整理汇总了Golang中net/http.Server.ListenAndServeTLS方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.ListenAndServeTLS方法的具体用法?Golang Server.ListenAndServeTLS怎么用?Golang Server.ListenAndServeTLS使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.Server的用法示例。


在下文中一共展示了Server.ListenAndServeTLS方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: runHostCmd

func runHostCmd(cmd *cobra.Command, args []string) {
	router := httprouter.New()
	serverHandler := &server.Handler{}
	serverHandler.Start(c, router)

	if ok, _ := cmd.Flags().GetBool("dangerous-auto-logon"); ok {
		logrus.Warnln("Do not use flag --dangerous-auto-logon in production.")
		err := c.Persist()
		pkg.Must(err, "Could not write configuration file: %s", err)
	}

	http.Handle("/", router)

	var srv = http.Server{
		Addr: c.GetAddress(),
		TLSConfig: &tls.Config{
			Certificates: []tls.Certificate{
				getOrCreateTLSCertificate(cmd),
			},
		},
	}

	var err error
	logrus.Infof("Starting server on %s", c.GetAddress())
	if ok, _ := cmd.Flags().GetBool("force-dangerous-http"); ok {
		logrus.Warnln("HTTPS disabled. Never do this in production.")
		err = srv.ListenAndServe()
	} else {
		err = srv.ListenAndServeTLS("", "")
	}
	pkg.Must(err, "Could not start server: %s %s.", err)
}
开发者ID:jemacom,项目名称:hydra,代码行数:32,代码来源:host.go

示例2: Serve

func (e *endpoint) Serve() error {
	// TODO: Currently only support single server per endpoint
	if len(e.OriginServers) > 1 {
		return errors.New("Currently only 1 origin server per endpoint is supported.")
	}

	// Create http2 server
	var srv http.Server

	// Set Port
	srv.Addr = e.Address

	// Mux setup
	router := http.NewServeMux()

	// Set mux
	srv.Handler = router

	// Set handlers
	setHandlers(router, e)

	err := http2.ConfigureServer(&srv, &http2.Server{})
	if err != nil {
		return err
	}

	log.Printf("Listening on %s", srv.Addr)
	srv.ListenAndServeTLS(e.Certs.CertFile, e.Certs.KeyFile)

	return nil
}
开发者ID:gophergala2016,项目名称:zoom_and_enhance,代码行数:31,代码来源:endpoint.go

示例3: main

func main() {
	var srv http.Server
	flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
	flag.Parse()
	srv.Addr = *httpsAddr
	srv.ConnState = idleTimeoutHook()

	registerHandlers()

	if *prod {
		*hostHTTP = "http2.golang.org"
		*hostHTTPS = "http2.golang.org"
		log.Fatal(serveProd())
	}

	url := "https://" + httpsHost() + "/"
	log.Printf("Listening on " + url)
	http2.ConfigureServer(&srv, &http2.Server{})

	if *httpAddr != "" {
		go func() {
			log.Printf("Listening on http://" + httpHost() + "/ (for unencrypted HTTP/1)")
			log.Fatal(http.ListenAndServe(*httpAddr, nil))
		}()
	}

	go func() {
		log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
	}()
	select {}
}
开发者ID:mobuter,项目名称:drmecanic2,代码行数:31,代码来源:h2demo.go

示例4: main

func main() {
	var httpServer http.Server
	var httpsServer http.Server
	http2.VerboseLogs = true
	http2.ConfigureServer(&httpsServer, nil)

	http.HandleFunc("/auth", authHandler)
	http.HandleFunc("/", handler)
	http.HandleFunc("/js", jsHandler)

	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		log.Println("start http listening :18888")
		httpServer.Addr = ":18888"
		log.Println(httpServer.ListenAndServe())
		wg.Done()
	}()
	go func() {
		log.Println("start https listening :18889")
		httpsServer.Addr = ":18889"
		log.Println(httpsServer.ListenAndServeTLS("server.crt", "server.key"))
		wg.Done()
	}()
	wg.Wait()
}
开发者ID:VViles,项目名称:curl_as_dsl,代码行数:26,代码来源:server.go

示例5: main

/*
main registers this RP's HTTP request handlers; creates the HTTPS client for issuing OP ID Token requests and starts its HTTP server.
*/
func main() {
	var (
		certPool *x509.CertPool
		server   http.Server
		err      error
	)

	//This aeadCipher is used to encrypt/decrypt the Authn Request Cookie that is used to pass the Authn Request State value
	//from the Authn Request to the Authn Response.
	aeadCipher, err = aead.NewAEADCipher()
	if err != nil {
		return
	}

	//Initialize an HTTPS capable client and replace the default aws HTTP client that doesn't support HTTPS
	certPool = x509.NewCertPool()
	certPool.AppendCertsFromPEM([]byte(certbndl.PemCerts))
	opClient = &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{RootCAs: certPool},
		},
	}

	//Start the service
	server = http.Server{Addr: ":443", ReadTimeout: 10 * time.Minute, WriteTimeout: 10 * time.Minute, ErrorLog: logger.Logger()}
	http.HandleFunc("/login", handleLogin)
	http.HandleFunc("/authn-token", handleAuthnToken)
	logger.Println("Starting oidc on " + exthost + ":443")
	err = server.ListenAndServeTLS("resilient-networks.crt", "resilient-networks.key")
	if err != nil {
		logger.Fatal(err)
	}

}
开发者ID:develrns,项目名称:resilient,代码行数:37,代码来源:oidc.go

示例6: main

func main() {
	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: nil,
	}
	server.ListenAndServeTLS("cert.pem", "key.pem")
}
开发者ID:jf,项目名称:gwp,代码行数:7,代码来源:server.go

示例7: serveHTTPS

func serveHTTPS(ctx context.Context, httpServer *http.Server) error {
	log.Printf("Starting TLS server on %s", *httpsAddr)
	httpsServer := new(http.Server)
	*httpsServer = *httpServer
	httpsServer.Addr = *httpsAddr
	cacheFile := "letsencrypt.cache"
	if !inProd {
		if *tlsCertFile != "" && *tlsKeyFile != "" {
			return httpsServer.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
		}
		// Otherwise use Let's Encrypt, i.e. same use case as in prod
	} else {
		cacheFile = prodLECache
		if err := os.MkdirAll(filepath.Dir(cacheFile), 0755); err != nil {
			return err
		}
	}
	var m letsencrypt.Manager
	if err := m.CacheFile(cacheFile); err != nil {
		return err
	}
	httpsServer.TLSConfig = &tls.Config{
		GetCertificate: m.GetCertificate,
	}
	log.Printf("Listening for HTTPS on %v", *httpsAddr)
	ln, err := net.Listen("tcp", *httpsAddr)
	if err != nil {
		return err
	}
	return httpsServer.Serve(tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, httpsServer.TLSConfig))
}
开发者ID:pombredanne,项目名称:camlistore,代码行数:31,代码来源:camweb.go

示例8: main

func main() {
	var srv http.Server
	flag.BoolVar(&http2.VerboseLogs, "verbose", false, "Verbose HTTP/2 debugging.")
	flag.Parse()
	srv.Addr = *addr

	registerHandlers()

	if *prod {
		*httpAddr = "http2.golang.org"
		log.Fatal(serveProd())
	}

	url := "https://" + *addr + "/"
	log.Printf("Listening on " + url)
	http2.ConfigureServer(&srv, &http2.Server{})

	if *httpAddr != "" {
		go func() { log.Fatal(http.ListenAndServe(*httpAddr, nil)) }()
	}

	go func() {
		log.Fatal(srv.ListenAndServeTLS("server.crt", "server.key"))
	}()
	if *openFirefox && runtime.GOOS == "darwin" {
		time.Sleep(250 * time.Millisecond)
		exec.Command("open", "-b", "org.mozilla.nightly", "https://localhost:4430/").Run()
	}
	select {}
}
开发者ID:johndmulhausen,项目名称:kubernetes,代码行数:30,代码来源:h2demo.go

示例9: main

func main() {
	if _, err := fs.Mount(); err != nil {
		log.Printf("fs-mounting %v\n", err)
		os.Exit(-1)
	}

	var server http.Server
	server.Addr = envGetOrAlternatives(EnvCachyServerAddress, ":8080")

	log.Printf("cachy server running on %q\n", server.Addr)

	http.HandleFunc("/", Cache)
	http.HandleFunc("/cache", Cache)

	certFilepath := envGetOrAlternatives(EnvCachyCertFile, "cachy.cert")
	keyFilepath := envGetOrAlternatives(EnvCachyKeyFile, "cachy.key")

	if envGetOrAlternatives("http1") != "" {
		if err := server.ListenAndServe(); err != nil {
			log.Printf("serving http: %v\n", err)
		}
	} else {
		if err := server.ListenAndServeTLS(certFilepath, keyFilepath); err != nil {
			log.Printf("servingTLS: %v\n", err)
		}
	}
}
开发者ID:odeke-em,项目名称:cachy,代码行数:27,代码来源:main.go

示例10: runServer

func runServer(mux *http.ServeMux, c *serverConfig) {
	h := httpxtra.Handler{
		Handler:  mux,
		XHeaders: c.XHeaders,
	}
	if c.Log {
		h.Logger = httpLogger
	}
	s := http.Server{
		Addr:         c.Addr,
		Handler:      h,
		ReadTimeout:  15 * time.Second,
		WriteTimeout: 15 * time.Second,
	}
	if len(c.KeyFile) > 0 && len(c.CertFile) > 0 {
		log.Printf("Starting HTTPS server on tcp/%s "+
			"log=%t xheaders=%t cert=%s key=%s",
			c.Addr,
			c.Log,
			c.XHeaders,
			c.CertFile,
			c.KeyFile,
		)
		log.Fatal(s.ListenAndServeTLS(c.CertFile, c.KeyFile))
	} else {
		log.Printf("Starting HTTP server on tcp/%s "+
			"log=%t xheaders=%t",
			c.Addr,
			c.Log,
			c.XHeaders,
		)
		log.Fatal(httpxtra.ListenAndServe(s))
	}
}
开发者ID:mendosus,项目名称:freegeoip,代码行数:34,代码来源:freegeoip.go

示例11: serveHTTPS

func serveHTTPS(ctx context.Context, httpServer *http.Server) error {
	log.Printf("Starting TLS server on %s", *httpsAddr)
	httpsServer := new(http.Server)
	*httpsServer = *httpServer
	httpsServer.Addr = *httpsAddr
	if !inProd {
		if *tlsCertFile == "" {
			return errors.New("unspecified --tlscert flag")
		}
		if *tlsKeyFile == "" {
			return errors.New("unspecified --tlskey flag")
		}
		return httpsServer.ListenAndServeTLS(*tlsCertFile, *tlsKeyFile)
	}
	cert, err := tlsCertFromGCS(ctx)
	if err != nil {
		return fmt.Errorf("error loading TLS certs from GCS: %v", err)
	}
	httpsServer.TLSConfig = &tls.Config{
		Certificates: []tls.Certificate{*cert},
	}
	log.Printf("Listening for HTTPS on %v", *httpsAddr)
	ln, err := net.Listen("tcp", *httpsAddr)
	if err != nil {
		return err
	}
	return httpsServer.Serve(tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, httpsServer.TLSConfig))
}
开发者ID:stevearm,项目名称:camlistore,代码行数:28,代码来源:camweb.go

示例12: main

func main() {
	port := 8080

	var srv http.Server

	router := mux.NewRouter()

	router.HandleFunc("/hello", hello)
	router.HandleFunc("/welcome", welcome)
	router.PathPrefix("/").Handler(http.FileServer(http.Dir(".")))

	srv.Addr = Sprintf(":%d", port)

	/* set the http handler */
	srv.Handler = &gpp.Handler{EnableProxy: true, Handler: router}

	/* initial http2 support */
	http2.ConfigureServer(&srv, nil)

	log.Print("Listen on: ", Sprintf("https://0.0.0.0:%d", port))
	srv.ListenAndServeTLS("server.crt", "server.key")
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:fangdingjun,项目名称:gpp,代码行数:25,代码来源:ex_http2.go

示例13: main

func main() {
	destURL, err := url.Parse(destURL)
	if err != nil {
		log.Fatalf("failed to parse destination URL: %v", err)
	}

	certPool := x509.NewCertPool()
	{
		fh, err2 := os.Open(clientFile)
		if err2 != nil {
			log.Fatalf("failed to parse root certificate file: %v", err2)
		}
		defer fh.Close()

		buf := new(bytes.Buffer)
		rh := bufio.NewReader(fh)
		io.Copy(buf, rh)
		ok := certPool.AppendCertsFromPEM(buf.Bytes())
		if !ok {
			log.Fatalf("failed to append PEM file.")
		}
	}

	proxyHandler := httputil.NewSingleHostReverseProxy(destURL)
	proxyServer := http.Server{
		Addr:    listenAddr,
		Handler: proxyHandler,
		TLSConfig: &tls.Config{
			ClientAuth: tls.RequireAndVerifyClientCert,
			ClientCAs:  certPool,
		},
	}

	log.Fatal(proxyServer.ListenAndServeTLS(certFile, keyFile))
}
开发者ID:nyarla,项目名称:sslr,代码行数:35,代码来源:main.go

示例14: BackgroundRunHttps

// BackgroundRunHttps runs an https server in a goroutine.
// Returns a send-only channel to watch for errors.
func BackgroundRunHttps(server *http.Server, conf *BaseConfiguration) chan error {
	https := make(chan error)
	go func() {
		server.Addr = fmt.Sprintf("%s:%d", conf.BindAddress, int(conf.BindHttpsPort))
		https <- server.ListenAndServeTLS(conf.CertFile, conf.KeyFile)
	}()
	return https
}
开发者ID:pombredanne,项目名称:go-serv,代码行数:10,代码来源:server.go

示例15: ListenHTTPS

func ListenHTTPS() {
	s := http.Server{
		Addr:    cfg.HTTPS.Addr,
		Handler: httpxtra.Handler{Logger: _logger},
	}
	log.Println("Starting HTTPS server on", cfg.HTTPS.Addr)
	log.Fatal(s.ListenAndServeTLS(cfg.HTTPS.CertFile, cfg.HTTPS.KeyFile))
}
开发者ID:NailClippar,项目名称:go-web,代码行数:8,代码来源:http.go


注:本文中的net/http.Server.ListenAndServeTLS方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。