github.com/blend/go-sdk@v1.20220411.3/webutil/http_server_option.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package webutil 9 10 import ( 11 "context" 12 "crypto/tls" 13 "log" 14 "net" 15 "net/http" 16 "time" 17 ) 18 19 // HTTPServerOption is a mutator for an http server. 20 type HTTPServerOption func(*http.Server) error 21 22 // OptHTTPServerHandler mutates a http server. 23 func OptHTTPServerHandler(handler http.Handler) HTTPServerOption { 24 return func(s *http.Server) error { 25 s.Handler = handler 26 return nil 27 } 28 } 29 30 // OptHTTPServerBaseContext sets the base context for requests to a given server. 31 func OptHTTPServerBaseContext(baseContextProvider func(net.Listener) context.Context) HTTPServerOption { 32 return func(s *http.Server) error { 33 s.BaseContext = baseContextProvider 34 return nil 35 } 36 } 37 38 // OptHTTPServerTLSConfig mutates a http server. 39 func OptHTTPServerTLSConfig(cfg *tls.Config) HTTPServerOption { 40 return func(s *http.Server) error { 41 s.TLSConfig = cfg 42 return nil 43 } 44 } 45 46 // OptHTTPServerAddr mutates a http server. 47 func OptHTTPServerAddr(addr string) HTTPServerOption { 48 return func(s *http.Server) error { 49 s.Addr = addr 50 return nil 51 } 52 } 53 54 // OptHTTPServerMaxHeaderBytes mutates a http server. 55 func OptHTTPServerMaxHeaderBytes(value int) HTTPServerOption { 56 return func(s *http.Server) error { 57 s.MaxHeaderBytes = value 58 return nil 59 } 60 } 61 62 // OptHTTPServerReadTimeout mutates a http server. 63 func OptHTTPServerReadTimeout(value time.Duration) HTTPServerOption { 64 return func(s *http.Server) error { 65 s.ReadTimeout = value 66 return nil 67 } 68 } 69 70 // OptHTTPServerReadHeaderTimeout mutates a http server. 71 func OptHTTPServerReadHeaderTimeout(value time.Duration) HTTPServerOption { 72 return func(s *http.Server) error { 73 s.ReadHeaderTimeout = value 74 return nil 75 } 76 } 77 78 // OptHTTPServerWriteTimeout mutates a http server. 79 func OptHTTPServerWriteTimeout(value time.Duration) HTTPServerOption { 80 return func(s *http.Server) error { 81 s.WriteTimeout = value 82 return nil 83 } 84 } 85 86 // OptHTTPServerIdleTimeout mutates a http server. 87 func OptHTTPServerIdleTimeout(value time.Duration) HTTPServerOption { 88 return func(s *http.Server) error { 89 s.IdleTimeout = value 90 return nil 91 } 92 } 93 94 // OptHTTPServerErrorLog sets the error log. 95 func OptHTTPServerErrorLog(log *log.Logger) HTTPServerOption { 96 return func(s *http.Server) error { 97 s.ErrorLog = log 98 return nil 99 } 100 }