예제 #1
0
파일: saypi.go 프로젝트: antifuchs/saypi
func main() {
	appCfg, srvCfg, err := readConfiguration()
	if err != nil {
		log.Fatalf("Error parsing configuration. event=config_error error=%q", err)
	}

	a, err := app.New(appCfg)
	if err != nil {
		log.Fatalf("Error initializing app event=init_error error=%q", err)
	}
	defer a.Close()

	listener, err := net.Listen("tcp", srvCfg.HTTPAddr)
	if err != nil {
		log.Fatalf("Error attempting to listen on port, event=listen_error address=%q error=%q", err, srvCfg.HTTPAddr)
	}

	graceful.Timeout(httpGrace)
	graceful.HandleSignals()
	graceful.PreHook(func() {
		log.Print("Shutting down. event=app_stop")
	})
	log.Printf("Starting. event=app_start address=%q", listener.Addr())
	bind.Ready()
	err = graceful.Serve(listener, a)
	if err != nil {
		log.Fatalf("Shutting down after a fatal error. event=fatal_error error=%q", err)
	}
}
예제 #2
0
// 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
}