add support for toml configuration file
This commit is contained in:
parent
2e90d32ce2
commit
fb43d8da3a
2 changed files with 22 additions and 10 deletions
20
data.go
20
data.go
|
|
@ -6,14 +6,13 @@ import (
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
//TODO: move into separate config file?
|
type databaseConfig struct {
|
||||||
const (
|
Host string
|
||||||
DBHost = "127.0.0.1"
|
Port int
|
||||||
DBPort = 5432
|
User string
|
||||||
DBUser = "username"
|
Pass string
|
||||||
DBPass = "password"
|
Name string
|
||||||
DBName = "database"
|
}
|
||||||
)
|
|
||||||
|
|
||||||
type userData struct {
|
type userData struct {
|
||||||
nameserver string
|
nameserver string
|
||||||
|
|
@ -23,9 +22,10 @@ type userData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareDatabase() (*sql.DB, error) {
|
func prepareDatabase() (*sql.DB, error) {
|
||||||
|
dbconf := config.Database
|
||||||
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s " +
|
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s " +
|
||||||
"sslmode=disable", DBHost, DBPort, DBUser, DBPass,
|
"sslmode=disable", dbconf.Host, dbconf.Port,
|
||||||
DBName)
|
dbconf.User, dbconf.Pass, dbconf.Name)
|
||||||
db, err := sql.Open("postgres", psqlInfo)
|
db, err := sql.Open("postgres", psqlInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
12
main.go
12
main.go
|
|
@ -2,9 +2,21 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type tomlConfig struct {
|
||||||
|
Database databaseConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
var config tomlConfig
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
_, err := toml.DecodeFile("config.toml", &config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
db, err := prepareDatabase()
|
db, err := prepareDatabase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue