本文整理汇总了Golang中github.com/bmizerany/pq.ParseURL函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseURL函数的具体用法?Golang ParseURL怎么用?Golang ParseURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseURL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getdb
func getdb() *sql.DB {
var settings, herokupg, localpg string
herokupg = os.Getenv("DATABASE_URL")
if herokupg != "" {
settings, _ = pq.ParseURL(herokupg)
} else {
c, _ := goconf.ReadConfigFile("db.config")
localpg, _ = c.GetString("dev", "postgresurl")
settings, _ = pq.ParseURL(localpg)
}
db, _ := sql.Open("postgres", settings)
return db
}
示例2: main
func main() {
flag.Parse()
cs, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
log.Println("unable to parse database url")
os.Exit(1)
}
pg, err = sql.Open("postgres", cs)
if err != nil {
log.Println("unable to connect to database")
os.Exit(1)
}
log.Println("bind tcp", os.Getenv("PORT"))
server, err := net.Listen("tcp", "0.0.0.0:"+os.Getenv("PORT"))
if err != nil {
log.Println("unable to bind tcp")
os.Exit(1)
}
conns := clientConns(server)
for {
go readData(<-conns)
}
}
示例3: connectToDB
func connectToDB() {
var err error
dataSource, err := pq.ParseURL(os.Getenv("PGURL"))
handleError(err, "URL PARSE")
DB, err = sql.Open("postgres", dataSource)
handleError(err, "DB CONNECT")
}
示例4: feel
func feel(databaseUrl string) {
dataSource, err := pq.ParseURL(databaseUrl)
if err != nil {
fmt.Printf("Unable to parse database url (%s)", databaseUrl)
panic(err)
}
u, err := url.Parse(databaseUrl)
if err != nil {
panic(err)
}
databaseName := u.Path[1:]
db, err := sql.Open("postgres", dataSource)
if err != nil {
fmt.Println("Unable to connect to database")
}
for {
row := db.QueryRow("SELECT count(*) from pg_stat_activity")
var count uint16
err = row.Scan(&count)
if err != nil {
panic(err)
}
row = db.QueryRow("SELECT pg_database_size($1)", databaseName)
var bytes uint64
err = row.Scan(&bytes)
if err != nil {
panic(err)
}
o := observation{connections: count, databaseUrl: databaseUrl, bytes: bytes, serviceAvailable: true, updatedAt: time.Now()}
o.persist()
}
}
示例5: initwebdb
func initwebdb() {
connstr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal("pq.ParseURL", err)
}
db, err = sql.Open("postgres", connstr+" sslmode=disable")
if err != nil {
log.Fatal("sql.Open", err)
}
mustExec(`SET bytea_output = 'hex'`) // work around https://github.com/bmizerany/pq/issues/76
mustExec(`create table if not exists release (
plat text not null,
cmd text not null,
ver text not null,
sha256 bytea not null,
primary key (plat, cmd, ver)
)`)
mustExec(`create table if not exists cur (
plat text not null,
cmd text not null,
curver text not null,
foreign key (plat, cmd, curver) references release (plat, cmd, ver),
primary key (plat, cmd)
)`)
mustExec(`create table if not exists mod (
t timestamptz not null
)`)
mustExec(`insert into mod (t)
select now()
where not exists (select 1 from mod)
`)
}
示例6: db_open
func db_open(url string) (*sql.DB, error) {
conn_str, err := pq.ParseURL(url)
if err != nil {
return nil, err
}
return sql.Open("postgres", conn_str)
}
示例7: dbConnect
func dbConnect() (db *sql.DB, err error) {
dbUrl := os.Getenv("DATABASE_URL")
source, err := pq.ParseURL(dbUrl)
if err != nil {
db, err = sql.Open("postgres", source)
}
return
}
示例8: storeBatches
func storeBatches(batches <-chan []LogMessage) {
conn, err := pg.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
db, err := sql.Open("postgres", conn)
if err != nil {
panic(err)
}
_, err = db.Query("SET application_name TO \"log-stuff:storeBatches\";")
if err != nil {
panic(err)
}
insertStatement, err := db.Prepare("INSERT INTO logs values ($1, $2, $3, $4, $5, $6, $7, $8)")
if err != nil {
panic(err)
}
rows_inserted := 0
for batch := range batches {
tx, err := db.Begin()
if err != nil {
panic(err)
}
txStmt := tx.Stmt(insertStatement)
for _, item := range batch {
_, err := txStmt.Exec(item.facility, item.severity, item.length, item.timestamp, item.drain, item.source, item.process, item.log)
if err != nil {
panic(err)
}
rows_inserted += 1
}
err = tx.Commit()
if err != nil {
panic(err)
}
rows_inserted = 0
}
}
示例9: openTestDB
func openTestDB(t *testing.T) *sql.DB {
cs, err := pq.ParseURL("postgres://localhost:5432/gls_test")
if err != nil {
t.Fatal(err)
}
cs += " sslmode=disable"
db, err := OpenDB(cs)
if err != nil {
t.Fatal(err)
}
return db
}
示例10: init
func init() {
url := os.Getenv("DATABASE_URL")
if len(url) == 0 {
postgresEnabled = false
fmt.Println("Postgres has been disabled.")
return
}
str, err := pq.ParseURL(url)
if err != nil {
fmt.Printf("error=\"unable to parse DATABASE_URL\"\n")
os.Exit(1)
}
pg, err = sql.Open("postgres", str)
if err != nil {
fmt.Printf("error=%s\n", err)
os.Exit(1)
}
rurl := os.Getenv("DATABASE_READ_URL")
if len(rurl) > 0 {
rstr, err := pq.ParseURL(rurl)
if err != nil {
fmt.Printf("error=\"unable to parse DATABASE_READ_URL\"\n")
os.Exit(1)
}
pgRead, err = sql.Open("postgres", rstr)
if err != nil {
fmt.Printf("error=%s\n", err)
os.Exit(1)
}
return
}
fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
pgRead, err = sql.Open("postgres", str)
if err != nil {
fmt.Printf("error=%s\n", err)
os.Exit(1)
}
}
示例11: init
func init() {
url := os.Getenv("DATABASE_URL")
if len(url) == 0 {
fmt.Printf("at=error error=\"must set DATABASE_URL\"\n")
os.Exit(1)
}
str, err := pq.ParseURL(url)
if err != nil {
fmt.Printf("at=error error=\"unable to parse DATABASE_URL\"\n")
os.Exit(1)
}
PG, err = sql.Open("postgres", str)
if err != nil {
fmt.Printf("at=error error=%s\n", err)
os.Exit(1)
}
rurl := os.Getenv("DATABASE_READ_URL")
if len(rurl) > 0 {
rstr, err := pq.ParseURL(rurl)
if err != nil {
fmt.Printf("at=error error=\"unable to parse DATABASE_READ_URL\"\n")
os.Exit(1)
}
PGR, err = sql.Open("postgres", rstr)
if err != nil {
fmt.Printf("at=error error=%s\n", err)
os.Exit(1)
}
return
}
fmt.Printf("Missing DATABASE_READ_URL. Using DATABASE_URL to service reads.\n")
PGR, err = sql.Open("postgres", str)
if err != nil {
fmt.Printf("at=error error=%s\n", err)
os.Exit(1)
}
}
示例12: dbConnect
func dbConnect() *sql.DB {
connStr, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
db, err := sql.Open("postgres", connStr)
if err != nil {
fmt.Printf("Error connecting: %s", err)
}
return db
}
示例13: GetConn
func GetConn() *sql.DB {
// Get the connection string from the DATABASE_URL env var
connString, err := pq.ParseURL(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
db, err := sql.Open("postgres", connString+" sslmode=disable")
if err != nil {
panic(err)
}
return db
}
示例14: parseUrl
func parseUrl(name string) string {
var tmp string
var err error
tmp = os.Getenv(name)
if len(tmp) == 0 {
fmt.Printf("at=error error=\"must set %s\"\n", name)
os.Exit(1)
}
tmp, err = pq.ParseURL(tmp)
if err != nil {
fmt.Printf("at=error error=\"unable to parse %s\"\n", name)
os.Exit(1)
}
return tmp
}
示例15: getDatabaseUrl
// Get the Postgres Database Url.
func getDatabaseUrl() string {
var database_url = os.Getenv("DATABASE_URL")
// Set a default database url if there is nothing in the environemnt
if database_url == "" {
// Postgres.app uses this variable to set-up postgres.
user := os.Getenv("USER")
// Inject the username into the connection string
database_url = "postgres://" + user + "@localhost/up?sslmode=disable"
// Let the user know they are using a default
fmt.Println("INFO: No DATABASE_URL env var detected, defaulting to " + database_url)
}
conn_str, err := pq.ParseURL(database_url)
if err != nil {
panic("Unable to Parse DB URL connection string: " + database_url)
}
return conn_str
}