本文整理汇总了Golang中net/http.Server.Addr方法的典型用法代码示例。如果您正苦于以下问题:Golang Server.Addr方法的具体用法?Golang Server.Addr怎么用?Golang Server.Addr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Server
的用法示例。
在下文中一共展示了Server.Addr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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()
}
示例2: 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))
}
示例3: 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)
}
}
示例4: 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))
}
示例5: 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
}
示例6: 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 {}
}
示例7: ListenAndServe
// ListenAndServe is same as http.ListenAndServe except it returns
// the address that will be listened on (which is useful when using
// arbitrary ports).
// See https://github.com/golang/go/blob/master/src/net/http/server.go
func ListenAndServe(svr *http.Server) (*RestServiceInfo, error) {
log.Infof("Entering ListenAndServe(%p)", svr)
if svr.Addr == "" {
svr.Addr = ":0"
}
ln, err := net.Listen("tcp", svr.Addr)
if err != nil {
return nil, err
}
realAddr := ln.Addr().String()
channel := make(chan ServiceMessage)
l := svr.ErrorLog
if l == nil {
l = clog.New(os.Stderr, "[negroni] ", 0)
}
go func() {
channel <- Starting
l.Printf("ListenAndServe(%p): listening on %s (asked for %s) with configuration %v, handler %v\n", svr, realAddr, svr.Addr, svr, svr.Handler)
err := svr.Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})
if err != nil {
log.Criticalf("RestService: Fatal error %v", err)
os.Exit(255)
}
}()
return &RestServiceInfo{Address: realAddr, Channel: channel}, nil
}
示例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 {}
}
示例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)
}
}
}
示例10: serveSmart
func (c *conn) serveSmart() {
defer func() {
if err := recover(); err != nil {
const size = 4096
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
log.Printf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
}
if !c.hijacked() {
c.close()
}
}()
if tlsConn, ok := c.rwc.(*tls.Conn); ok {
if d := c.server.ReadTimeout; d != 0 {
c.rwc.SetReadDeadline(time.Now().Add(d))
}
if d := c.server.WriteTimeout; d != 0 {
c.rwc.SetWriteDeadline(time.Now().Add(d))
}
if err := tlsConn.Handshake(); err != nil {
return
}
c.tlsState = new(tls.ConnectionState)
*c.tlsState = tlsConn.ConnectionState()
if proto := c.tlsState.NegotiatedProtocol; validNPN(proto) {
if fn := c.server.TLSNextProto[proto]; fn != nil {
h := initNPNRequest{tlsConn, serverHandler{c.server}}
fn(c.server, tlsConn, h)
}
return
}
}
initial_bytes := make([]byte, start_bytes)
// add smart detection
n, err := c.rwc.Read(initial_bytes)
if err != nil || n != start_bytes {
return
}
if checkspdy(initial_bytes) {
s := c.server
z := new(http.Server)
z.Addr = s.Addr
z.Handler = s.inner_h
z.ReadTimeout = s.ReadTimeout
z.WriteTimeout = s.WriteTimeout
z.MaxHeaderBytes = s.MaxHeaderBytes
server_session := spdy.NewServerSession(NewSmartConn(c.rwc, initial_bytes), z)
server_session.Serve()
return
}
c2, err := c.server.newConn(NewSmartConn(c.rwc, initial_bytes))
if err != nil {
return
}
c2.serve()
}
示例11: 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
}
示例12: main
func main() {
var srv http.Server
srv.Addr = ":8000"
http2.ConfigureServer(&srv, nil)
http.HandleFunc("/", HelloWorld)
http.HandleFunc("/favicon.ico", Error404)
log.Fatal(srv.ListenAndServeTLS("localhost.crt", "localhost.key"))
}
示例13: main
func main() {
var srv http.Server
srv.Addr = ":12345"
authenticator := auth.NewDigestAuthenticator("063.jp", secret)
http.HandleFunc("/", authenticator.Wrap(func(res http.ResponseWriter, req *auth.AuthenticatedRequest) {
http.FileServer(http.Dir("/var/www/html/SS/")).ServeHTTP(res, &req.Request)
}))
http2.ConfigureServer(&srv, &http2.Server{})
srv.ListenAndServeTLS("../key/server.crt", "../key/server.key")
}
示例14: main
func main() {
var httpServer http.Server
http2.VerboseLogs = true
httpServer.Addr = ":8080"
http2.ConfigureServer(&httpServer, nil)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to test HTTP2 page %q\n", html.EscapeString(r.URL.Path))
ShowRequestInfoHandler(w, r)
})
log.Fatal(httpServer.ListenAndServeTLS("localhost.cert", "localhost.key"))
}
示例15: main
func main() {
var srv http.Server
//http2.VerboseLogs = true
srv.Addr = ":8443"
http2.ConfigureServer(&srv, nil)
http.HandleFunc("/", get)
err := srv.ListenAndServeTLS("cert.pem", "key.pem")
if err != nil {
log.Fatal(err)
}
}