This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience for serving protocols
- Loading branch information
Joshua T Corbin
committed
May 19, 2016
1 parent
565bcb8
commit 6e4ec07
Showing
2 changed files
with
60 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package protocol | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"code.uber.internal/personal/joshua/gwr" | ||
) | ||
|
||
// TODO: write a http+rest server that heuristically detects the protocol | ||
|
||
// ListenAndServeResp starts a redis protocol gwr server. | ||
func ListenAndServeResp(hostPort string) error { | ||
return NewRedisServer(&gwr.DefaultDataSources).ListenAndServe(hostPort) | ||
} | ||
|
||
// ListenAndServeHTTP starts an http protocol gwr server. | ||
func ListenAndServeHTTP(hostPort string) error { | ||
return http.ListenAndServe(hostPort, NewHTTPRest(&gwr.DefaultDataSources, "")) | ||
} | ||
|
||
// ProtoListenAndServe maps protocol names to listenAndServe func(hostPort | ||
// string) error. | ||
var ProtoListenAndServe = map[string]func(string) error{ | ||
"resp": ListenAndServeResp, | ||
"http": ListenAndServeHTTP, | ||
} | ||
|
||
// TODO: provide a default protoHostPorts based on environment variables | ||
|
||
// ListenAndServe starts one or more servers given a map of protocol name to | ||
// hostPort string. Any errors are passed to log.Fatal. | ||
func ListenAndServe(protoHostPorts map[string]string) { | ||
for proto := range protoHostPorts { | ||
if ProtoListenAndServe[proto] == nil { | ||
log.Fatalf("invalid protocol %v", proto) | ||
} | ||
} | ||
|
||
if len(protoHostPorts) == 1 { | ||
for proto, hostPort := range protoHostPorts { | ||
listenAndServe := ProtoListenAndServe[proto] | ||
log.Fatal(listenAndServe(hostPort)) | ||
return | ||
} | ||
} | ||
|
||
for proto, hostPort := range protoHostPorts { | ||
go func(proto, hostPort string) { | ||
listenAndServe := ProtoListenAndServe[proto] | ||
log.Fatal(listenAndServe(hostPort)) | ||
}(proto, hostPort) | ||
} | ||
select {} | ||
} |