mirror of
https://gitlab.com/commento/commento.git
synced 2025-06-29 22:56:37 -04:00
api: Add go files
I know this is a huge commit, but I can't be bothered to check this in part by part.
This commit is contained in:
39
api/database_connect.go
Normal file
39
api/database_connect.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/lib/pq"
|
||||
"os"
|
||||
)
|
||||
|
||||
func connectDB() error {
|
||||
con := os.Getenv("POSTGRES")
|
||||
logger.Infof("opening connection to postgres: %s", con)
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("postgres", con)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot open connection to postgres: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
statement := `
|
||||
CREATE TABLE IF NOT EXISTS migrations (
|
||||
filename TEXT NOT NULL UNIQUE
|
||||
);
|
||||
`
|
||||
_, err = db.Exec(statement)
|
||||
if err != nil {
|
||||
logger.Errorf("cannot create migrations table: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// At most 1000 database connections will be left open in the idle state. This
|
||||
// was found to be important when benchmarking with `wrk`: if this was unset,
|
||||
// too many open idle connections were present, resulting in dropped requests
|
||||
// due to the limit on the number of file handles. On benchmarking, around
|
||||
// 100 was found to be pretty optimal.
|
||||
db.SetMaxIdleConns(100)
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user