-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
173 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"log" | ||
migration "news-api/internal/migrations" | ||
|
||
"news-api/internal/app" | ||
"news-api/internal/config" | ||
"news-api/internal/database" | ||
"news-api/internal/handlers" | ||
) | ||
|
||
func main() { | ||
// Инициализация конфигурации | ||
cfg := config.InitConfig() | ||
|
||
// Инициализация базы данных | ||
database.InitDB(cfg) | ||
|
||
// Создание нового Echo инстанса | ||
e := echo.New() | ||
cfg := config.GetConfig() | ||
db := database.DataBase{} | ||
db.GetStorage(cfg) | ||
migration.Migrations(cfg, db.DB) | ||
App := app.App{Config: cfg, DB: &db} | ||
|
||
// Регистрация маршрутов | ||
e.GET("/news", handlers.ListNewsHandler) | ||
e.PUT("/news/:id", handlers.EditNewsHandler) | ||
srv := &app.Server{} | ||
App.ServerInterface = srv | ||
|
||
// Запуск сервера | ||
e.Start(":8090") | ||
if err := App.Start(); err != nil { | ||
log.Fatalf("Failed to start server: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
Server: | ||
port: 8082 | ||
|
||
Database: | ||
driver: "postgres" | ||
host: "localhost" | ||
port: 5432 | ||
user: "olegsemashko" | ||
password: 0000 | ||
dbname: "NewsDB" | ||
|
||
Server: | ||
port: 8090 | ||
dbname: "NewsDB" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
_ "fmt" | ||
"github.com/labstack/echo/v4/middleware" | ||
_ "github.com/labstack/echo/v4/middleware" | ||
"log" | ||
|
||
"news-api/internal/config" | ||
|
||
"github.com/labstack/echo/v4" | ||
_ "news-api/internal/models" | ||
) | ||
|
||
type App struct { | ||
Config *config.Config | ||
DB Storage | ||
ServerInterface ServerInterface | ||
Echo *echo.Echo | ||
} | ||
|
||
type Storage interface { | ||
Stop() error | ||
} | ||
|
||
func (a *App) Start() error { | ||
a.Echo = echo.New() | ||
a.ServerInterface.GetServer(a) | ||
a.Echo.Use(middleware.Logger()) | ||
a.Echo.Use(middleware.Recover()) | ||
|
||
addr := fmt.Sprintf(":%d", a.Config.Server.Port) | ||
log.Printf("Starting server on %s", addr) | ||
return a.Echo.Start(addr) | ||
} | ||
|
||
// Stop закрывает если есть ошибки | ||
func (a *App) Stop() { | ||
if err := a.DB.Stop(); err != nil { | ||
log.Fatalf("Failed to close database: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package app | ||
|
||
type ServerInterface interface { | ||
GetServer(*App) | ||
} | ||
|
||
type Server struct { | ||
} | ||
|
||
// GetServer метод для запуска роутера и обработчика запросов | ||
func (s *Server) GetServer(app *App) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package database | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"database/sql" | ||
_ "github.com/lib/pq" | ||
"news-api/internal/config" | ||
) | ||
|
||
type DataBase struct { | ||
DB *sql.DB | ||
} | ||
|
||
// GetStorage функция для подключения к Базе Данных | ||
func (db *DataBase) GetStorage(cfg *config.Config) { | ||
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%d dbname=%s sslmode=disable", | ||
cfg.Database.Host, cfg.Database.Port, cfg.Database.User, cfg.Database.Password, cfg.Database.DBName) | ||
|
||
var err error | ||
db.DB, err = sql.Open("postgres", dsn) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to database: %v", err) | ||
} | ||
|
||
if err := db.DB.Ping(); err != nil { | ||
log.Fatalf("Failed to ping database: %v", err) | ||
} | ||
} | ||
|
||
// Stop метод для закрытие БД | ||
func (db *DataBase) Stop() error { | ||
if db.DB != nil { | ||
err := db.DB.Close() | ||
{ | ||
if err != nil { | ||
log.Fatalf("Failed to closed database: %v", err) | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package migration | ||
|
||
import ( | ||
"embed" | ||
"log" | ||
|
||
"database/sql" | ||
"github.com/pressly/goose/v3" | ||
"news-api/internal/config" | ||
) | ||
|
||
//go:embed migrations/ | ||
var embedMigrations embed.FS | ||
|
||
func Migrations(cfg *config.Config, db *sql.DB) { | ||
|
||
goose.SetBaseFS(embedMigrations) | ||
|
||
if err := goose.SetDialect("postgres"); err != nil { | ||
log.Fatalf("Failed to set goose dialect: %v", err) | ||
} | ||
|
||
if err := goose.Up(db, "migrations"); err != nil { | ||
log.Fatalf("Failed to apply migrations: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1 @@ | ||
package models | ||
|
||
import _ "gopkg.in/reform.v1" | ||
|
||
//go:generate reform | ||
|
||
// reform:News | ||
type News struct { | ||
ID int64 `reform:"id,pk"` | ||
Title string `reform:"title"` | ||
Content string `reform:"content"` | ||
} | ||
|
||
// reform:NewsCategories | ||
type NewsCategories struct { | ||
NewsID int64 `reform:"news_id"` | ||
CategoryID int64 `reform:"category_id"` | ||
} |
This file was deleted.
Oops, something went wrong.