From 9d8914bf26dfc41224ced4a7528a82dacdd28eb0 Mon Sep 17 00:00:00 2001 From: Shuhrat Tulyaganov Date: Mon, 12 Dec 2022 20:34:00 +0300 Subject: [PATCH] add database connection --- configs/restServer/config.yaml | 7 ++++++- go.mod | 1 + go.sum | 2 ++ internal/bd/bd.go | 34 +++++++++++++++++++++++++++++++ internal/bd/config.go | 9 ++++++++ internal/restserver/config.go | 9 +++++--- internal/restserver/restserver.go | 17 ++++++++++++++++ 7 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 internal/bd/bd.go create mode 100644 internal/bd/config.go diff --git a/configs/restServer/config.yaml b/configs/restServer/config.yaml index 3dce3a3..0323cf9 100644 --- a/configs/restServer/config.yaml +++ b/configs/restServer/config.yaml @@ -2,4 +2,9 @@ bind_addr: ":127.0.0.1" bind_port: ":8050" -log_level: "debug" \ No newline at end of file +log_level: "debug" + + +# db +DB: + baseurlbd: "host=localhost user=tester dbname=testdb sslmode=disable" \ No newline at end of file diff --git a/go.mod b/go.mod index bed5925..9744065 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require github.com/sirupsen/logrus v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/gorilla/mux v1.8.0 // indirect + github.com/lib/pq v1.10.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/testify v1.8.1 // indirect diff --git a/go.sum b/go.sum index 278949e..edde489 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= +github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= diff --git a/internal/bd/bd.go b/internal/bd/bd.go new file mode 100644 index 0000000..dcd178d --- /dev/null +++ b/internal/bd/bd.go @@ -0,0 +1,34 @@ +package bd + +import ( + "database/sql" + + _ "github.com/lib/pq" +) + +type Bd struct { + config *ConfigBD + db *sql.DB +} + +func New(config *ConfigBD) *Bd { + return &Bd{ + config: config, + } +} + +func (b *Bd) Open() error { + db, err := sql.Open("postgres", b.config.BaseUrlBd) + if err != nil { + return err + } + if err := db.Ping(); err != nil { + return err + } + b.db = db + return nil +} + +func (b *Bd) Close() { + b.db.Close() +} diff --git a/internal/bd/config.go b/internal/bd/config.go new file mode 100644 index 0000000..2eb8916 --- /dev/null +++ b/internal/bd/config.go @@ -0,0 +1,9 @@ +package bd + +type ConfigBD struct { + BaseUrlBd string `yaml:"baseurlbd"` +} + +func NewConfig() *ConfigBD { + return &ConfigBD{} +} diff --git a/internal/restserver/config.go b/internal/restserver/config.go index 6408a5d..cdc2a0e 100644 --- a/internal/restserver/config.go +++ b/internal/restserver/config.go @@ -3,13 +3,15 @@ package restserver import ( "io/ioutil" + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd" "gopkg.in/yaml.v3" ) type Config struct { - BindAddr string `yaml:"bind_addr"` - BindPort string `yaml:"bind_port"` - LogLevel string `yaml:"log_level"` + BindAddr string `yaml:"bind_addr"` + BindPort string `yaml:"bind_port"` + LogLevel string `yaml:"log_level"` + DB *bd.ConfigBD `yaml:"DB"` } func NewConfig() *Config { @@ -17,6 +19,7 @@ func NewConfig() *Config { BindAddr: ":127.0.0.1", BindPort: ":8080", LogLevel: "debug", + DB: bd.NewConfig(), } } diff --git a/internal/restserver/restserver.go b/internal/restserver/restserver.go index e6b703b..54decb8 100644 --- a/internal/restserver/restserver.go +++ b/internal/restserver/restserver.go @@ -4,6 +4,7 @@ import ( "io" "net/http" + "git.ukamnya.ru/stulyaganov/RestApi/internal/bd" "github.com/gorilla/mux" "github.com/sirupsen/logrus" ) @@ -12,6 +13,7 @@ type RestServer struct { config *Config logger *logrus.Logger router *mux.Router + db *bd.Bd } func New(config *Config) *RestServer { @@ -26,6 +28,10 @@ func (r *RestServer) Start() error { if err := r.configureLogger(); err != nil { return err } + if err := r.configurebd(); err != nil { + return err + } + r.configureRouter() r.logger.Info("Starting Server") @@ -46,6 +52,17 @@ func (r *RestServer) configureRouter() { r.router.HandleFunc("/hello", r.HandleHello()) } +func (r *RestServer) configurebd() error { + dataBase := bd.New(r.config.DB) + if err := dataBase.Open(); err != nil { + return err + } + + r.db = dataBase + + return nil +} + func (r *RestServer) HandleHello() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) {