From 2c07fba2773172b27c9221e2154cdaca24fdc840 Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Thu, 25 Sep 2025 04:55:27 +0500 Subject: [PATCH] modified: docker-compose.yaml modified: go.mod modified: go.sum modified: main.go add db for rest api --- serv_golang_rest_api/docker-compose.yaml | 32 ++++++++++++++++++- serv_golang_rest_api/go.mod | 17 +++++++++++ serv_golang_rest_api/go.sum | 36 ++++++++++++++++++++++ serv_golang_rest_api/main.go | 39 +++++++++++++++++++++++- 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/serv_golang_rest_api/docker-compose.yaml b/serv_golang_rest_api/docker-compose.yaml index 0f832fb..6fb936c 100644 --- a/serv_golang_rest_api/docker-compose.yaml +++ b/serv_golang_rest_api/docker-compose.yaml @@ -6,4 +6,34 @@ services: ports: - "8888:8080" container_name: serv_golang_rest_api - restart: unless-stopped \ No newline at end of file + restart: unless-stopped + depends_on: + - db + environment: + # Database connection settings + DB_HOST: db + DB_PORT: 5432 + DB_USER: postgres + DB_PASSWORD: postgres + DB_NAME: mydb + networks: + - app-network + + db: + image: postgres:15-alpine + ports: + - "5432:5432" + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: mydb + volumes: + - postgres_data:/var/lib/postgresql/data + networks: + - app-network + +volumes: + postgres_data: + +networks: + app-network: \ No newline at end of file diff --git a/serv_golang_rest_api/go.mod b/serv_golang_rest_api/go.mod index d926b66..86ff816 100644 --- a/serv_golang_rest_api/go.mod +++ b/serv_golang_rest_api/go.mod @@ -1,3 +1,20 @@ module serv_golang_rest_api go 1.22.5 + +require ( + gorm.io/driver/postgres v1.6.0 + gorm.io/gorm v1.31.0 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.6.0 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + golang.org/x/crypto v0.31.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/text v0.21.0 // indirect +) diff --git a/serv_golang_rest_api/go.sum b/serv_golang_rest_api/go.sum index e69de29..64e7fe8 100644 --- a/serv_golang_rest_api/go.sum +++ b/serv_golang_rest_api/go.sum @@ -0,0 +1,36 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY= +github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4= +gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= +gorm.io/gorm v1.31.0 h1:0VlycGreVhK7RF/Bwt51Fk8v0xLiiiFdbGDPIZQ7mJY= +gorm.io/gorm v1.31.0/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= diff --git a/serv_golang_rest_api/main.go b/serv_golang_rest_api/main.go index 6da4acc..a907388 100644 --- a/serv_golang_rest_api/main.go +++ b/serv_golang_rest_api/main.go @@ -4,20 +4,57 @@ import ( "encoding/json" "log" "net/http" + + "gorm.io/driver/postgres" + "gorm.io/gorm" ) type Response struct { Message string `json:"message"` } +// Example model +type User struct { + gorm.Model + Name string + Email string +} + func handler(w http.ResponseWriter, r *http.Request) { + // Get DB connection from context + db, ok := r.Context().Value("db").(*gorm.DB + if !ok { + http.Error(w, "Database connection not found", http.StatusInternalServerError) + return + } + + // Example usage + db.AutoMigrate(&User{}) + user := User{Name: "Test", Email: "test@example.com"} + db.Create(&user) + w.Header().Set("Content-Type", "application/json") response := Response{Message: "ok"} json.NewEncoder(w).Encode(response) } func main() { - http.HandleFunc("/", handler) + // Create DB connection string + dsn := "host=db user=postgres password=postgres dbname=mydb port=5432 sslmode=disable TimeZone=UTC" + db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) + if err != nil { + log.Fatal("Failed to connect to database:", err) + } + + // Initialize database + db.AutoMigrate(&User{}) + + // Setup HTTP server with DB context + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + r = r.WithContext(context.WithValue(r.Context(), "db", db)) + handler(w, r) + }) + log.Println("Сервер запущен на http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) } \ No newline at end of file