gddo-server: Group all flags into main package.
This CL moves the flags defined in database package into main and changes
them to exported variables.
Change-Id: I6c4c3287f20b506170375bbd30880d172fd39ea3
Reviewed-on: https://go-review.googlesource.com/24175
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/database/database.go b/database/database.go
index 100bf62..914ec97 100644
--- a/database/database.go
+++ b/database/database.go
@@ -33,7 +33,6 @@
"bytes"
"encoding/gob"
"errors"
- "flag"
"fmt"
"log"
"math"
@@ -71,15 +70,16 @@
func (p byPath) Less(i, j int) bool { return p[i].Path < p[j].Path }
func (p byPath) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
+// Configuration variables (and default values of flags.
var (
- redisServer = flag.String("db-server", "redis://127.0.0.1:6379", "URI of Redis server.")
- redisIdleTimeout = flag.Duration("db-idle-timeout", 250*time.Second, "Close Redis connections after remaining idle for this duration.")
- redisLog = flag.Bool("db-log", false, "Log database commands")
- GAESearch = flag.Bool("gae_search", false, "Use GAE Search API in the search function.")
+ RedisServer = "redis://127.0.0.1:6379" // URL of Redis server
+ RedisIdleTimeout = 250 * time.Second // Close Redis connections after remaining idle for this duration.
+ RedisLog = false // Log database commands
+ GAESearch = false // Use GAE Search API in the search function.
)
func dialDb() (c redis.Conn, err error) {
- u, err := url.Parse(*redisServer)
+ u, err := url.Parse(RedisServer)
if err != nil {
return nil, err
}
@@ -95,7 +95,7 @@
return
}
- if *redisLog {
+ if RedisLog {
l := log.New(os.Stderr, "", log.LstdFlags)
c = redis.NewLoggingConn(c, l, "")
}
@@ -115,7 +115,7 @@
pool := &redis.Pool{
Dial: dialDb,
MaxIdle: 10,
- IdleTimeout: *redisIdleTimeout,
+ IdleTimeout: RedisIdleTimeout,
}
c := pool.Get()
@@ -264,7 +264,7 @@
return err
}
- if *GAESearch {
+ if GAESearch {
id, n, err := pkgIDAndImportCount(c, pdoc.ImportPath)
if err != nil {
return err
diff --git a/gddo-server/main.go b/gddo-server/main.go
index a68edb3..d04f274 100644
--- a/gddo-server/main.go
+++ b/gddo-server/main.go
@@ -577,7 +577,7 @@
pkgs []database.Package
err error
)
- if *database.GAESearch {
+ if database.GAESearch {
ctx := appengine.NewContext(req)
pkgs, err = database.Search(ctx, q)
} else {
@@ -869,6 +869,13 @@
sourcegraphURL = flag.String("sourcegraph_url", "https://sourcegraph.com", "Link to global uses on Sourcegraph based at this URL (no need for trailing slash).")
)
+func init() {
+ flag.StringVar(&database.RedisServer, "db-server", database.RedisServer, "URI of Redis server.")
+ flag.DurationVar(&database.RedisIdleTimeout, "db-idle-timeout", database.RedisIdleTimeout, "Close Redis connections after remaining idle for this duration.")
+ flag.BoolVar(&database.RedisLog, "db-log", database.RedisLog, "Log database commands")
+ flag.BoolVar(&database.GAESearch, "gae_search", database.GAESearch, "Use GAE Search API in the search function.")
+}
+
func main() {
flag.Parse()
log.Printf("Starting server, os.Args=%s", strings.Join(os.Args, " "))
@@ -1019,7 +1026,7 @@
gceLogger = newGCELogger(logc)
}
- if *database.GAESearch {
+ if database.GAESearch {
http.Handle("/", root)
appengine.Main()
} else {