Exemple #1
0
func run() error {
	var (
		debug      = flag.Bool("debug", false, "")
		staticPath = flag.String("static", "./static", "serve static files located in this directory")
		host       = flag.String("host", ":8080", "address:port for HTTP listener")
		pgaddr     = flag.String("postgres", "postgres://*****:*****@localhost/abalone?sslmode=disable", "postgres connection info")
		dir        = flag.String("dir", "/var/abalone", "directory where players are kept")
	)
	flag.Parse()

	configDir := *dir
	configPlayersDir := path.Join(configDir, "players")

	if err := os.MkdirAll(configDir, os.ModePerm); err != nil {
		return err
	}
	if err := os.MkdirAll(configPlayersDir, os.ModePerm); err != nil {
		return err
	}

	const driver = "postgres" // only supported database
	log.Printf("using database %s with config %s", driver, *pgaddr)

	ds, err := db.Open(driver, *pgaddr, configPlayersDir)
	if err != nil {
		return err
	}
	ds.DB.LogMode(*debug)

	r := ConfigureRouter(ds, *staticPath)

	log.Printf("listening at %s", *host)
	log.Fatal(http.ListenAndServe(*host, r))
	return nil
}
func NewTestServer(t *testing.T) *httptest.Server {
	if *pgaddr == "" {
		t.SkipNow()
	}
	ds, err := db.Open(dialect, *pgaddr, os.TempDir())
	if err != nil {
		t.Fatal(err)
	}

	r := router.NewAPIRouter()
	MountHandlers(r, ds)
	return httptest.NewServer(r)
}