Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Olegsuus committed Jun 18, 2024
1 parent 3745f5d commit eb5458b
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 87 deletions.
12 changes: 12 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions cmd/server/main.go
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)
}
}
8 changes: 4 additions & 4 deletions config.yaml
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.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pressly/goose/v3 v3.20.0 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -33,8 +36,10 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
Expand All @@ -284,6 +286,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/pressly/goose/v3 v3.20.0 h1:uPJdOxF/Ipj7ABVNOAMJXSxwFXZGwMGHNqjC8e61VA0=
github.com/pressly/goose/v3 v3.20.0/go.mod h1:BRfF2GcG4FTG12QfdBVy3q1yveaf4ckL9vWwEcIO3lA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand All @@ -292,6 +296,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sagikazarmark/crypt v0.1.0/go.mod h1:B/mN0msZuINBtQ1zZLEQcegFJJf9vnYIR88KRMEuODE=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down Expand Up @@ -341,6 +347,8 @@ go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -456,6 +464,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
43 changes: 43 additions & 0 deletions internal/app/app.go
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)
}
}
1 change: 1 addition & 0 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package app
13 changes: 13 additions & 0 deletions internal/app/server.go
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) {

}
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Config struct {
Database DatabaseConfig
}

func InitConfig() *Config {
func GetConfig() *Config {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
Expand Down
32 changes: 0 additions & 32 deletions internal/database/database.go

This file was deleted.

44 changes: 44 additions & 0 deletions internal/database/storage.go
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
}
26 changes: 26 additions & 0 deletions internal/migrations/migrations.go
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)
}
}
17 changes: 0 additions & 17 deletions internal/models/models.go
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"`
}
18 changes: 0 additions & 18 deletions internal/router/router.go

This file was deleted.

0 comments on commit eb5458b

Please sign in to comment.