示例#1
0
func main() {
	var err error

	db, err = database.InitPG()
	if err != nil {
		log.Fatalf("ERROR: problem with DB config: %s", err)
	}
	defer db.Close()

	db.Check()

	log.Println("starting message listener.")
	listen()
}
示例#2
0
文件: server.go 项目: GeoNet/haz
// main connects to the database, sets up request routing, and starts the http server.
func main() {
	var err error
	db, err = database.InitPG()
	if err != nil {
		log.Println("Problem with DB config.")
		log.Fatal(err)
	}
	defer db.Close()

	if err = db.Ping(); err != nil {
		log.Println("ERROR: problem pinging DB - is it up and contactable? 500s will be served")
	}

	log.Println("starting server")

	http.Handle("/", handler())
	log.Fatal(http.ListenAndServe(":"+os.Getenv("WEB_SERVER_PORT"), nil))
}
示例#3
0
文件: server.go 项目: GeoNet/haz
// main connects to the database, sets up request routing, and starts the http server.
func main() {
	var err error
	db, err = database.InitPG()
	if err != nil {
		log.Println("Problem with DB config.")
		log.Fatal(err)
	}
	defer db.Close()

	if err = db.Ping(); err != nil {
		log.Println("ERROR: problem pinging DB - is it up and contactable? 500s will be served")
	}

	// create an http client to share.
	timeout := time.Duration(5 * time.Second)
	client = &http.Client{
		Timeout: timeout,
	}

	var sess *session.Session
	if sess, err = session.NewSession(); err != nil {
		log.Printf("ERROR creating AWS session 500s will be served %s", err)
	}

	// Credentials can come from environment var (e.g., for dev)
	// or from the role provider when running in EC2.
	// expire the role creds early to force updates.
	creds := credentials.NewChainCredentials(
		[]credentials.Provider{
			&credentials.EnvProvider{},
			&ec2rolecreds.EC2RoleProvider{
				Client:       ec2metadata.New(sess),
				ExpiryWindow: time.Second * 30,
			},
		})

	s3Client = s3.New(sess, &aws.Config{Credentials: creds})

	log.Println("starting server")
	http.Handle("/", handler())
	log.Fatal(http.ListenAndServe(":"+os.Getenv("WEB_SERVER_PORT"), nil))
}
示例#4
0
func main() {
	var err error

	db, err = database.InitPG()
	if err != nil {
		log.Fatalf("ERROR: problem with DB config: %s", err)
	}
	defer db.Close()

	db.Check()

	files, err := ioutil.ReadDir(spoolDir)
	if err != nil {
		log.Fatal(err.Error())
	}

	sc3ml := make(chan os.FileInfo)

	go func() {
		defer close(sc3ml)

		for _, fi := range files {
			if !strings.HasSuffix(fi.Name(), ".xml") {
				continue
			}
			sc3ml <- fi
		}
	}()

	var wg sync.WaitGroup
	wg.Add(10)
	for i := 0; i < 10; i++ {
		go func() {
			procSC3ML(sc3ml)

			wg.Done()
		}()
	}
	wg.Wait()
}
示例#5
0
文件: server.go 项目: junghao/haz
// main connects to the database, sets up request routing, and starts the http server.
func main() {
	var err error
	db, err = database.InitPG()
	if err != nil {
		log.Println("Problem with DB config.")
		log.Fatal(err)
	}
	defer db.Close()

	if err = db.Ping(); err != nil {
		log.Println("ERROR: problem pinging DB - is it up and contactable? 500s will be served")
	}

	// create an http client to share.
	timeout := time.Duration(5 * time.Second)
	client = &http.Client{
		Timeout: timeout,
	}

	log.Println("starting server")
	http.Handle("/", handler())
	log.Fatal(http.ListenAndServe(":"+os.Getenv("WEB_SERVER_PORT"), nil))
}
示例#6
0
// setup starts a db connection and test server then inits an http client.
func setup() {
	// load some test data.  Needs a write user.
	var err error
	database.DBUser = "******"
	tdb, err := database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	tdb.Check()

	_, err = tdb.Exec("delete from haz.quake")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from haz.quakeapi")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from haz.quakehistory")
	if err != nil {
		log.Fatal(err)
	}

	q := msg.ReadSC3ML07("etc/test/files/2013p407387.xml")
	if err != nil {
		log.Fatal(err)
	}

	// stop the quake being deleted from haz.quakehistory and haz.quakeapi
	q.Time = time.Now().UTC()

	err = tdb.SaveQuake(q)
	if err != nil {
		log.Fatal(err)
	}

	q = msg.ReadSC3ML07("etc/test/files/2015p012816.xml")
	if err != nil {
		log.Fatal(err)
	}

	// stop the quake being deleted from haz.quakehistory and haz.quakeapi
	q.Time = time.Now().UTC()

	err = tdb.SaveQuake(q)
	if err != nil {
		log.Fatal(err)
	}

	tdb.Close()

	database.DBUser = "******"
	tdb, err = database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	tdb.Check()

	_, err = tdb.Exec("delete from impact.intensity_measured")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from impact.intensity_reported")
	if err != nil {
		log.Fatal(err)
	}

	for _, m := range measuredTest {
		_, err = tdb.Exec("select impact.add_intensity_measured($1, $2, $3, $4, $5)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI)
		if err != nil {
			log.Fatal(err)
		}
	}

	for _, m := range reportedTest {
		_, err = tdb.Exec("select impact.add_intensity_reported($1, $2, $3, $4, $5, $6)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI, m.Comment)
		if err != nil {
			log.Fatal(err)
		}
	}

	tdb.Close()

	// switch back to the correct user for the tests.
	// hazard_r can read haz and impact.
	database.DBUser = "******"
	db, err = database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	err = db.Ping()

	if err != nil {
		log.Fatal(err)
	}

	ts = httptest.NewServer(handler())

	client = &http.Client{}
}
示例#7
0
文件: server_test.go 项目: GeoNet/haz
// setup starts a db connection and test server then inits an http client.
func setup() {
	// load some test data.  Needs a write user.
	var err error
	database.DBUser = "******"
	tdb, err := database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	tdb.Check()

	_, err = tdb.Exec("delete from haz.quake")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from haz.quakeapi")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from haz.quakehistory")
	if err != nil {
		log.Fatal(err)
	}

	q := msg.ReadSC3ML07("etc/test/files/2013p407387.xml")
	if err != nil {
		log.Fatal(err)
	}

	// stop the quake being deleted from haz.quakehistory and haz.quakeapi
	q.Time = time.Now().UTC()

	err = tdb.SaveQuake(q)
	if err != nil {
		log.Fatal(err)
	}

	q = msg.ReadSC3ML07("etc/test/files/2015p012816.xml")
	if err != nil {
		log.Fatal(err)
	}

	// stop the quake being deleted from haz.quakehistory and haz.quakeapi
	q.Time = time.Now().UTC()

	err = tdb.SaveQuake(q)
	if err != nil {
		log.Fatal(err)
	}

	tdb.Close()

	database.DBUser = "******"
	tdb, err = database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	tdb.Check()

	_, err = tdb.Exec("delete from impact.intensity_measured")
	if err != nil {
		log.Fatal(err)
	}

	_, err = tdb.Exec("delete from impact.intensity_reported")
	if err != nil {
		log.Fatal(err)
	}

	for _, m := range measuredTest {
		_, err = tdb.Exec("select impact.add_intensity_measured($1, $2, $3, $4, $5)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI)
		if err != nil {
			log.Fatal(err)
		}
	}

	for _, m := range reportedTest {
		_, err = tdb.Exec("select impact.add_intensity_reported($1, $2, $3, $4, $5, $6)", m.Source, m.Longitude, m.Latitude, m.Time, m.MMI, m.Comment)
		if err != nil {
			log.Fatal(err)
		}
	}

	tdb.Close()

	// switch back to the correct user for the tests.
	// hazard_r can read haz and impact.
	database.DBUser = "******"
	db, err = database.InitPG()
	if err != nil {
		log.Fatal(err)
	}

	err = db.Ping()

	if err != nil {
		log.Fatal(err)
	}

	ts = httptest.NewServer(handler())

	client = &http.Client{}

	var sess *session.Session
	if sess, err = session.NewSession(); err != nil {
		log.Printf("ERROR creating AWS session 500s will be served %s", err)
	}

	// Credentials can come from environment var (e.g., for dev)
	// for testing.
	creds := credentials.NewChainCredentials(
		[]credentials.Provider{
			&credentials.EnvProvider{},
		})

	s3Client = s3.New(sess, &aws.Config{Credentials: creds})
}
示例#8
0
文件: server_test.go 项目: GeoNet/haz
// setup starts a db connection and test server then inits an http client.
func setup(t *testing.T) {

	var err error
	database.DBUser = "******"
	tdb, err := database.InitPG()
	if err != nil {
		t.Fatal(err)
	}

	tdb.Check()

	_, err = tdb.Exec("delete from haz.quake")
	if err != nil {
		t.Fatal(err)
	}

	q := msg.ReadSC3ML07("etc/test/files/1542894.xml")
	if err != nil {
		t.Fatal(err)
	}

	err = tdb.SaveQuake(q)
	if err != nil {
		t.Fatal(err)
	}

	q = msg.ReadSC3ML07("etc/test/files/2190619.xml")
	if err != nil {
		t.Fatal(err)
	}

	err = tdb.SaveQuake(q)
	if err != nil {
		t.Fatal(err)
	}

	q = msg.ReadSC3ML07("etc/test/files/3366146.xml")
	if err != nil {
		t.Fatal(err)
	}

	err = tdb.SaveQuake(q)
	if err != nil {
		t.Fatal(err)
	}

	tdb.Close()

	// switch back to the correct user for the tests.
	// hazard_r can read haz and impact.
	database.DBUser = "******"
	db, err = database.InitPG()
	if err != nil {
		t.Fatal(err)
	}

	err = db.Ping()

	if err != nil {
		t.Fatal(err)
	}

	ts = httptest.NewServer(handler())

}