Skip to content

Commit

Permalink
добавил конфиг и поменял фукнцию подключения к бд
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegsuus committed Jun 18, 2024
1 parent 199b330 commit 3745f5d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
15 changes: 4 additions & 11 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ package main

import (
"github.com/labstack/echo/v4"
"github.com/spf13/viper"
"log"
"news-api/internal/config"
"news-api/internal/database"
"news-api/internal/handlers"
)

func main() {
// Инициализация конфигурации
viper.SetConfigName("config")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %v", err)
}
cfg := config.InitConfig()

// Инициализация базы данных
database.InitDB()
database.InitDB(cfg)

// Создание нового Echo инстанса
e := echo.New()
Expand All @@ -27,7 +22,5 @@ func main() {
e.PUT("/news/:id", handlers.EditNewsHandler)

// Запуск сервера
if err := e.Start(":8080"); err != nil {
log.Fatalf("Error starting server: %v", err)
}
e.Start(":8090")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.2
require (
github.com/go-playground/validator/v10 v10.6.1
github.com/labstack/echo/v4 v4.6.3
github.com/lib/pq v1.8.0
github.com/spf13/viper v1.9.0
gopkg.in/reform.v1 v1.5.1
)
Expand Down
35 changes: 20 additions & 15 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,37 @@ import (
"log"
)

type Config struct {
Database struct {
Driver string
Host string
Port string
User string
Password int
DBName string
}
type ServerConfig struct {
Port int `yaml:"port"`
}

Server struct {
Port int
}
type DatabaseConfig struct {
Driver string `yaml:"driver"`
Host string `yaml:"host"`
Port string `yaml:"port"`
User string `yaml:"user"`
Password int `yaml:"password"`
DBName string `yaml:"dbname"`
}

var Cfg Config
type Config struct {
Server ServerConfig
Database DatabaseConfig
}

func initConfig() {
func InitConfig() *Config {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s\n", err)
}

if err := viper.Unmarshal(&Cfg); err != nil {
var cfg Config
if err := viper.Unmarshal(&cfg); err != nil {
log.Fatalf("Unable to decode into struct, %v\n", err)
}

return &cfg
}
4 changes: 3 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package database

import (
"database/sql"
_ "github.com/lib/pq"
"log"
"news-api/internal/config"

"github.com/spf13/viper"
"gopkg.in/reform.v1"
Expand All @@ -11,7 +13,7 @@ import (

var DB *reform.DB

func InitDB() {
func InitDB(v *config.Config) {
dsn := viper.GetString("database.dsn")
sqlDB, err := sql.Open("postgres", dsn)
if err != nil {
Expand Down

0 comments on commit 3745f5d

Please sign in to comment.