func readConfiguration() (*app.Configuration, *config, error) { fl := flag.CommandLine var appCfg app.Configuration var srvCfg config fl.StringVar(&srvCfg.HTTPAddr, "http_addr", "0.0.0.0:8080", "Address to bind HTTP server") fl.StringVar(&appCfg.DBDSN, "db_dsn", "sslmode=disable dbname=saypi", "postgres data source name") fl.IntVar(&appCfg.DBMaxIdle, "db_max_idle", 2, "maximum number of idle DB connections") fl.IntVar(&appCfg.DBMaxOpen, "db_max_open", 100, "maximum number of open DB connections") fl.IntVar(&appCfg.IPPerMinute, "per_ip_rpm", 12, "maximum number of requests per IP per minute") fl.IntVar(&appCfg.IPRateBurst, "per_ip_burst", 5, "maximum instantaneous burst of requests per IP") userSecretStr := flag.String("user_secret", "", "hex encoded secret for generating secure user tokens") if err := fl.Parse(os.Args[1:]); err != nil { return nil, nil, err } userSecret, err := hex.DecodeString(*userSecretStr) if err != nil { return nil, nil, err } appCfg.UserSecret = userSecret return &appCfg, &srvCfg, nil }
// NewTestClient initializes a TestClient instance with an embedded // copy of the app. This will modify your passed Configuration to // incorporate testing default values. For non-stub configurations, // this will initialize a new database and store the DSN in the // Configuration. func NewTestClient(cfg *app.Configuration) (*TestClient, error) { var cli TestClient base := url.URL{} cli.baseURL = &base if cfg == nil { cfg = &app.Configuration{} } if len(cfg.UserSecret) == 0 { cfg.UserSecret = apptest.TestSecret } if cfg.IPPerMinute == 0 { cfg.IPPerMinute = 100000 } if cfg.IPRateBurst == 0 { cfg.IPRateBurst = 100000 } if cfg.DBDSN == "" { tdb, db, err := dbutil.NewTestDB() if err != nil { return nil, err } // We don't need the db handle if err := db.Close(); err != nil { return nil, err } cli.closers = append(cli.closers, tdb) cfg.DBDSN = dbutil.DefaultDataSource + " dbname=" + tdb.Name() } a, err := app.New(cfg) if err != nil { cli.Close() return nil, err } cli.closers = append(cli.closers, a) cli.do = func(req *http.Request) (*http.Response, error) { rr := httptest.NewRecorder() a.ServeHTTP(rr, req) resp := http.Response{ Status: fmt.Sprintf("%d %s", rr.Code, http.StatusText(rr.Code)), StatusCode: rr.Code, Body: ioutil.NopCloser(rr.Body), Header: rr.HeaderMap, ContentLength: int64(rr.Body.Len()), Request: req, } return &resp, nil } return &cli, nil }