Allow dev-* scripts listening on port 0, and then log resolved port.

This commit is contained in:
Brad Fitzpatrick 2011-05-09 12:22:31 -07:00
parent ec0c73f08d
commit c619cdcdf1
3 changed files with 20 additions and 9 deletions

View File

@ -15,7 +15,8 @@ GetOptions(
"tls" => \$opt_tls, "tls" => \$opt_tls,
) or usage(); ) or usage();
my $port = shift || "3179"; my $port = shift;
$port = "3179" unless defined($port);
usage() unless $port =~ /^\d+$/; usage() unless $port =~ /^\d+$/;
system("./build.pl", "server/go/camlistored") and die "Failed to build.\n"; system("./build.pl", "server/go/camlistored") and die "Failed to build.\n";
@ -38,6 +39,6 @@ $ENV{CAMLI_TLS_CRT_FILE} = $opt_tls ? "$Bin/config/dev-tls.crt" : "";
$ENV{CAMLI_TLS_KEY_FILE} = $opt_tls ? "$Bin/config/dev-tls.key" : ""; $ENV{CAMLI_TLS_KEY_FILE} = $opt_tls ? "$Bin/config/dev-tls.key" : "";
exec("$FindBin::Bin/server/go/camlistored/camlistored", exec("$FindBin::Bin/server/go/camlistored/camlistored",
"-configfile=$Bin/config/dev-blobserver-config.json", "-configfile=$Bin/config/dev-blobserver-config.json",
"-listen=:$port", "-listen=127.0.0.1:$port",
@ARGV); @ARGV);

View File

@ -12,7 +12,8 @@ my $opt_wipe;
GetOptions("wipe" => \$opt_wipe) GetOptions("wipe" => \$opt_wipe)
or usage(); or usage();
my $port = shift || "3202"; my $port = shift;
$port = "3202" unless defined($port);
usage() unless $port =~ /^\d+$/; usage() unless $port =~ /^\d+$/;
system("./build.pl", "server/go/camlistored") and die "Failed to build camlistored"; system("./build.pl", "server/go/camlistored") and die "Failed to build camlistored";
@ -51,5 +52,5 @@ $ENV{CAMLI_KEY_RING} = "$Bin/lib/go/camli/jsonsign/testdata/test-keyring.gpg";
$ENV{CAMLI_SECRET_RING} = "$Bin/lib/go/camli/jsonsign/testdata/test-secring.gpg"; $ENV{CAMLI_SECRET_RING} = "$Bin/lib/go/camli/jsonsign/testdata/test-secring.gpg";
exec("$FindBin::Bin/server/go/camlistored/camlistored", exec("$FindBin::Bin/server/go/camlistored/camlistored",
"-configfile=$Bin/config/dev-server-config.json", "-configfile=$Bin/config/dev-server-config.json",
"-listen=:$port", "-listen=127.0.0.1:$port",
@ARGV); @ARGV);

View File

@ -61,10 +61,13 @@ func (s *Server) BaseURL() string {
if s.enableTLS { if s.enableTLS {
scheme = "https" scheme = "https"
} }
if strings.HasPrefix(*Listen, ":") { if s.listener != nil {
return scheme + "://127.0.0.1" + *Listen return scheme + "://" + s.listener.Addr().String()
} }
return scheme + "://" + strings.Replace(*Listen, "0.0.0.0:", "127.0.0.1:", 1) if strings.HasPrefix(*Listen, ":") {
return scheme + "://localhost" + *Listen
}
return scheme + "://" + strings.Replace(*Listen, "0.0.0.0:", "localhost:", 1)
} }
// Register conditional handler-picker functions which get run before // Register conditional handler-picker functions which get run before
@ -94,8 +97,10 @@ func (s *Server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
func (s *Server) Serve() { func (s *Server) Serve() {
if os.Getenv("TESTING_PORT_WRITE_FD") == "" { // Don't make noise during unit tests doLog := os.Getenv("TESTING_PORT_WRITE_FD") == "" // Don't make noise during unit tests
log.Printf("Starting to listen on %s\n", s.BaseURL()) base := s.BaseURL()
if doLog {
log.Printf("Starting to listen on %s\n", base)
} }
var err os.Error var err os.Error
@ -104,6 +109,10 @@ func (s *Server) Serve() {
log.Fatalf("Failed to listen on %s: %v", *Listen, err) log.Fatalf("Failed to listen on %s: %v", *Listen, err)
} }
if doLog && strings.HasSuffix(base, ":0") {
log.Printf("Now listening on %s\n", s.BaseURL())
}
if s.enableTLS { if s.enableTLS {
config := &tls.Config{ config := &tls.Config{
Rand: rand.Reader, Rand: rand.Reader,