add database connection
parent
2a5c0b9ffa
commit
9d8914bf26
|
@ -2,4 +2,9 @@ bind_addr: ":127.0.0.1"
|
|||
|
||||
bind_port: ":8050"
|
||||
|
||||
log_level: "debug"
|
||||
log_level: "debug"
|
||||
|
||||
|
||||
# db
|
||||
DB:
|
||||
baseurlbd: "host=localhost user=tester dbname=testdb sslmode=disable"
|
1
go.mod
1
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package bd
|
||||
|
||||
type ConfigBD struct {
|
||||
BaseUrlBd string `yaml:"baseurlbd"`
|
||||
}
|
||||
|
||||
func NewConfig() *ConfigBD {
|
||||
return &ConfigBD{}
|
||||
}
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue