Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial #3

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.22.2
WORKDIR /
COPY . ./app
WORKDIR /app
RUN go mod download
RUN go mod tidy
RUN go build cmd/main.go

EXPOSE 8080

CMD [ "./main" ]
16 changes: 16 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"log"

"github.com/go-park-mail-ru/2024_2_GOATS/internal/app"
)

func main() {
a, ctx, err := app.New()
if err != nil {
log.Fatalf(err.Error())
}

a.Run(ctx)
}
29 changes: 29 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import "time"

type Config struct {
Listener Listener `yaml:"listener"`
Postgres Postgres `yaml:"postgres"`
}

type ConfigContextKey struct{}

type Postgres struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Name string `yaml:"name"`
}

type Listener struct {
Address string `yaml:"address"`
Port int `yaml:"port"`
Timeout time.Duration `yaml:"timeout"`
IdleTimeout time.Duration `yaml:"idle_timeout"`
}

func NewConfig() *Config {
return &Config{}
Starlexxx marked this conversation as resolved.
Show resolved Hide resolved
}
37 changes: 37 additions & 0 deletions config/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

func WrapContext(cfg *Config) (context.Context, error) {
filename, _ := filepath.Abs(viper.GetString("CFG_PATH"))
data, err := os.ReadFile(filename)
Starlexxx marked this conversation as resolved.
Show resolved Hide resolved

if err != nil {
return nil, fmt.Errorf("failed to open env: %w", err)
}

err = yaml.Unmarshal(data, cfg)
if err != nil {
return nil, fmt.Errorf("cannot unmarshall config.yml: %w", err)
}

return context.WithValue(context.Background(), ConfigContextKey{}, cfg), nil
}

func FromContext(ctx context.Context) *Config {
value, ok := ctx.Value(ConfigContextKey{}).(*Config)

if !ok {
return nil
}

return value
}
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
volumes:
postgres_volume:
redis:

services:
app:
build:
context: ./
dockerfile: Dockerfile
restart: always
ports:
- 8080:8080
networks:
- netflix
redis:
image: 'redis:latest'
command: redis-server
volumes:
- redis:/data
networks:
- netflix
db:
image: postgres:latest
container_name: db
environment:
- "POSTGRES_DB=netflix"
- "POSTGRES_USER=test"
- "POSTGRES_PASSWORD=test"
volumes:
- ./dev/SQL:/docker-entrypoint-initdb.d/
- "postgres_volume:/var/lib/postgresql/data"
networks:
- netflix

networks:
netflix:
default:
external:
name: netflix # Не забыть создать сеть перед деплоем
32 changes: 32 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/go-park-mail-ru/2024_2_GOATS

go 1.22.2

require (
github.com/gorilla/mux v1.8.1
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
61 changes: 61 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
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=
16 changes: 16 additions & 0 deletions internal/app/api/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package api

import (
"context"
"log"
"net/url"

"github.com/go-park-mail-ru/2024_2_GOATS/config"
)

func (i *Implementation) GetCollection(ctx context.Context, query url.Values) {
log.Println("From api: ", config.FromContext(ctx))
// r := i.service.GetCollection(ctx)

// return json.Marshal(r)
}
21 changes: 21 additions & 0 deletions internal/app/api/implementation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

import "context"

type ServiceInterface interface {
Starlexxx marked this conversation as resolved.
Show resolved Hide resolved
Login(ctx context.Context)
Register(ctx context.Context)
GetCollection(ctx context.Context)
}

type Implementation struct {
ctx context.Context
service ServiceInterface
}

func NewImplementation(ctx context.Context, srv ServiceInterface) *Implementation {
return &Implementation{
ctx: ctx,
service: srv,
}
}
83 changes: 83 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package app

import (
"context"
"database/sql"
"fmt"
"log"
"net/http"

_ "github.com/lib/pq"
"github.com/spf13/viper"

"github.com/go-park-mail-ru/2024_2_GOATS/config"
"github.com/go-park-mail-ru/2024_2_GOATS/internal/app/api"
"github.com/go-park-mail-ru/2024_2_GOATS/internal/app/repository"
"github.com/go-park-mail-ru/2024_2_GOATS/internal/app/router"
"github.com/go-park-mail-ru/2024_2_GOATS/internal/app/service"
"github.com/go-park-mail-ru/2024_2_GOATS/internal/db"
)

type App struct {
Starlexxx marked this conversation as resolved.
Show resolved Hide resolved
Database *sql.DB
}

func New() (*App, context.Context, error) {
setupViper()
ctx, err := config.WrapContext(config.NewConfig())
if err != nil {
return nil, nil, err
}

db, err := db.SetupDatabase(ctx)
if err != nil {
return nil, nil, err
}

return &App{Database: db}, ctx, nil
}

func (a *App) Run(ctx context.Context) {
repoLayer := repository.NewRepository(a.Database)
srvLayer := service.NewService(repoLayer)
apiLayer := api.NewImplementation(ctx, srvLayer)
mux := router.Setup(ctx, apiLayer)

ctxValues := config.FromContext(ctx)

srv := &http.Server{
Addr: fmt.Sprintf(":%d", ctxValues.Listener.Port),
Handler: mux,
ReadTimeout: ctxValues.Listener.Timeout,
WriteTimeout: ctxValues.Listener.Timeout,
IdleTimeout: ctxValues.Listener.IdleTimeout,
}

log.Printf("Server is listening: %s:%d", ctxValues.Listener.Address, ctxValues.Listener.Port)

if err := srv.ListenAndServe(); err != nil {
log.Fatalf(err.Error())
}
}

func setupViper() {
Starlexxx marked this conversation as resolved.
Show resolved Hide resolved
viper.SetConfigName(".env")
viper.SetConfigType("env")
viper.AddConfigPath(".")

err := viper.ReadInConfig()
if err != nil {
fmt.Printf("Failed to read .env file: %v\n", err)
return
}

viper.SetConfigName("config")
viper.SetConfigType("yml")
viper.AddConfigPath(viper.GetString("VIPER_CFG_PATH"))

err = viper.MergeInConfig()
if err != nil {
fmt.Printf("Failed to read config.yml file: %v\n", err)
return
}
}
25 changes: 25 additions & 0 deletions internal/app/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handler

import (
"context"
"net/http"

"github.com/go-park-mail-ru/2024_2_GOATS/internal/app/api"
)

func Login(ctx context.Context, api *api.Implementation, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
}

func Register(ctx context.Context, api *api.Implementation, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

})
}

func MovieCollections(ctx context.Context, api *api.Implementation, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
api.GetCollection(ctx, r.URL.Query())
})
}
13 changes: 13 additions & 0 deletions internal/app/repository/get_collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package repository

import (
"context"
"fmt"

"github.com/go-park-mail-ru/2024_2_GOATS/config"
)

func (r *Repo) GetCollection(ctx context.Context) {
fmt.Println("From repo:", config.FromContext(ctx))
r.Database.QueryContext(ctx, "SELECT * FROM movie_collections")
}
Loading