github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/app/db.go (about)

     1  package app
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	// psql driver - we only use a psql db at the moment
     8  	_ "github.com/lib/pq"
     9  
    10  	"github.com/fragmenta/query"
    11  	"github.com/fragmenta/server/config"
    12  	"github.com/fragmenta/server/log"
    13  )
    14  
    15  // SetupDatabase sets up the db with query given our server config.
    16  func SetupDatabase() {
    17  	defer log.Time(time.Now(), log.V{"msg": "Finished opening database", "db": config.Get("db"), "user": config.Get("db_user")})
    18  
    19  	options := map[string]string{
    20  		"adapter":  config.Get("db_adapter"),
    21  		"user":     config.Get("db_user"),
    22  		"password": config.Get("db_pass"),
    23  		"db":       config.Get("db"),
    24  	}
    25  
    26  	// Optionally Support remote databases
    27  	if len(config.Get("db_host")) > 0 {
    28  		options["host"] = config.Get("db_host")
    29  	}
    30  	if len(config.Get("db_port")) > 0 {
    31  		options["port"] = config.Get("db_port")
    32  	}
    33  	if len(config.Get("db_params")) > 0 {
    34  		options["params"] = config.Get("db_params")
    35  	}
    36  
    37  	// Ask query to open the database
    38  	err := query.OpenDatabase(options)
    39  
    40  	if err != nil {
    41  		log.Fatal(log.V{"msg": "unable to read database", "db": config.Get("db"), "error": err})
    42  		os.Exit(1)
    43  	}
    44  
    45  }