@@ -3,6 +3,7 @@ package bridge
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "net"
6
7
"net/http"
7
8
"time"
8
9
@@ -14,17 +15,17 @@ import (
14
15
)
15
16
16
17
const (
17
- OKPath = "/ok"
18
- OKMessage = "bridge is ready"
19
- ProxyPath = "/htproxy"
18
+ OKPath = "/ok"
19
+ OKMessage = "bridge is ready"
20
+ ProxyPath = "/htproxy"
21
+ GetCheckConnectionServerAddrPath = "/get_check_connection_server_addr"
20
22
)
21
23
22
24
// Env stores configuration settings extract from enviromental variables
23
25
// The practice getting from environmental variables comes from https://12factor.net.
24
26
type Env struct {
25
27
// Port is port to listen HTTP server. Default is 8080.
26
- // This value should be other than 4321. because "127.0.0.1:4321" is used in connection checking.
27
- Port string `envconfig:"PORT" default:"8080" description:"bridge を HTTP としてサーブするために利用します。4321 以外を指定してください。"`
28
+ Port string `envconfig:"PORT" default:"8080" description:"bridge を HTTP としてサーブするために利用します。"`
28
29
29
30
// LogLevel is INFO or DEBUG. Default is "INFO".
30
31
LogLevel string `envconfig:"LOG_LEVEL" default:"INFO"`
@@ -44,23 +45,23 @@ type Env struct {
44
45
45
46
// HTTPHandlerConfig is a config to setup bridge http handler.
46
47
type HTTPHandlerConfig struct {
47
- Logger logr.Logger
48
- PublicKeyGetter auth.PublicKeyGetter
49
- RegisterUserObject auth.TenantIDGetter
50
- TenantID string
51
- Middlewares []bridgehttp.Middleware
48
+ Logger logr.Logger
49
+ PublicKeyGetter auth.PublicKeyGetter
50
+ RegisterUserObject auth.TenantIDGetter
51
+ TenantID string
52
+ Middlewares []bridgehttp.Middleware
53
+ CheckConnectionServerAddr string
52
54
}
53
55
54
56
// NewHTTPHandler is a handler for handling any requests.
55
57
func NewHTTPHandler (c * HTTPHandlerConfig ) http.Handler {
56
58
mux := http .NewServeMux ()
57
- mux .HandleFunc (OKPath , func (w http.ResponseWriter , r * http.Request ) {
58
- if r .Method != http .MethodGet {
59
- w .WriteHeader (http .StatusMethodNotAllowed )
60
- return
61
- }
59
+ mux .HandleFunc (fmt .Sprintf ("GET %s" , OKPath ), func (w http.ResponseWriter , r * http.Request ) {
62
60
w .Write ([]byte (OKMessage ))
63
61
})
62
+ mux .HandleFunc (fmt .Sprintf ("GET %s" , GetCheckConnectionServerAddrPath ), func (w http.ResponseWriter , r * http.Request ) {
63
+ w .Write ([]byte (c .CheckConnectionServerAddr ))
64
+ })
64
65
middlewares := append (c .Middlewares ,
65
66
ctxtime .Middleware (),
66
67
auth .Middleware (& auth.MiddlewareConfig {
@@ -77,20 +78,12 @@ func NewHTTPHandler(c *HTTPHandlerConfig) http.Handler {
77
78
return mux
78
79
}
79
80
80
- // ConnectionCheckPort is used in connection check from API.
81
- const ConnectionCheckPort = "4321"
82
-
83
81
func NewHTTPServer (envPort string , handler http.Handler ) (* http.Server , func (), error ) {
84
- if envPort == ConnectionCheckPort {
85
- return nil , nil , fmt .Errorf ("PORT env should be other than 4321" )
86
- }
87
82
srv := & http.Server {
88
83
Addr : ":" + envPort ,
89
84
Handler : handler ,
90
85
}
91
86
92
- ServeCheckConnectionServer ()
93
-
94
87
return srv , func () {
95
88
ctx , cancel := context .WithTimeout (
96
89
context .Background (),
@@ -108,14 +101,19 @@ func NewHTTPServer(envPort string, handler http.Handler) (*http.Server, func(),
108
101
// because used only the connection check from API.
109
102
//
110
103
// Serve with goroutine.
111
- func ServeCheckConnectionServer () {
104
+ func ServeCheckConnectionServer () (addr string , err error ) {
105
+ ln , err := net .Listen ("tcp" , ":0" )
106
+ if err != nil {
107
+ return "" , fmt .Errorf ("failed to listen a port for connection check: %w" , err )
108
+ }
109
+
112
110
go func () {
113
- err := http .ListenAndServe (
114
- ":" + ConnectionCheckPort ,
115
- http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
116
- w .Write ([]byte (OKMessage ))
117
- }),
118
- )
111
+ srv := & http.Server {Addr : addr , Handler : http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
112
+ w .Write ([]byte (OKMessage ))
113
+ })}
114
+ err := srv .Serve (ln )
119
115
panic (fmt .Errorf ("failed to serve a server to check connection from API: %w" , err ))
120
116
}()
117
+
118
+ return ln .Addr ().String (), nil
121
119
}
0 commit comments