add database connection

main
Шухрат Туляганов 2022-12-12 20:34:00 +03:00
parent 2a5c0b9ffa
commit 9d8914bf26
7 changed files with 75 additions and 4 deletions

View File

@ -3,3 +3,8 @@ bind_addr: ":127.0.0.1"
bind_port: ":8050" bind_port: ":8050"
log_level: "debug" log_level: "debug"
# db
DB:
baseurlbd: "host=localhost user=tester dbname=testdb sslmode=disable"

1
go.mod
View File

@ -7,6 +7,7 @@ require github.com/sirupsen/logrus v1.9.0
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/mux v1.8.0 // 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/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect github.com/stretchr/testify v1.8.1 // indirect

2
go.sum
View File

@ -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/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 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=

34
internal/bd/bd.go 100644
View File

@ -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()
}

View File

@ -0,0 +1,9 @@
package bd
type ConfigBD struct {
BaseUrlBd string `yaml:"baseurlbd"`
}
func NewConfig() *ConfigBD {
return &ConfigBD{}
}

View File

@ -3,6 +3,7 @@ package restserver
import ( import (
"io/ioutil" "io/ioutil"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -10,6 +11,7 @@ type Config struct {
BindAddr string `yaml:"bind_addr"` BindAddr string `yaml:"bind_addr"`
BindPort string `yaml:"bind_port"` BindPort string `yaml:"bind_port"`
LogLevel string `yaml:"log_level"` LogLevel string `yaml:"log_level"`
DB *bd.ConfigBD `yaml:"DB"`
} }
func NewConfig() *Config { func NewConfig() *Config {
@ -17,6 +19,7 @@ func NewConfig() *Config {
BindAddr: ":127.0.0.1", BindAddr: ":127.0.0.1",
BindPort: ":8080", BindPort: ":8080",
LogLevel: "debug", LogLevel: "debug",
DB: bd.NewConfig(),
} }
} }

View File

@ -4,6 +4,7 @@ import (
"io" "io"
"net/http" "net/http"
"git.ukamnya.ru/stulyaganov/RestApi/internal/bd"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -12,6 +13,7 @@ type RestServer struct {
config *Config config *Config
logger *logrus.Logger logger *logrus.Logger
router *mux.Router router *mux.Router
db *bd.Bd
} }
func New(config *Config) *RestServer { func New(config *Config) *RestServer {
@ -26,6 +28,10 @@ func (r *RestServer) Start() error {
if err := r.configureLogger(); err != nil { if err := r.configureLogger(); err != nil {
return err return err
} }
if err := r.configurebd(); err != nil {
return err
}
r.configureRouter() r.configureRouter()
r.logger.Info("Starting Server") r.logger.Info("Starting Server")
@ -46,6 +52,17 @@ func (r *RestServer) configureRouter() {
r.router.HandleFunc("/hello", r.HandleHello()) 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 { func (r *RestServer) HandleHello() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {