Skip to content

Commit

Permalink
added extra logic
Browse files Browse the repository at this point in the history
  • Loading branch information
syth0le committed Nov 11, 2023
1 parent 08d4e7f commit b77484d
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DB_PASSWORD=eepha[l3eaph8Xo
JWT_TOKEN_SALT=xamah6Ael!iat0n
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work
.env
.idea
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.21.4-alpine as builder

WORKDIR /usr/src/app

COPY . .
RUN go mod download && go mod verify

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /usr/local/bin/app cmd/social-network/main.go --config=local_config.yaml

FROM alpine:3.18

COPY --from=builder /usr/local/bin/app /app

CMD ["/app"]
27 changes: 14 additions & 13 deletions cmd/social-network/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ func (c *Config) Validate() error {
}

type LoggerConfig struct {
Level utils.Level `yaml:"level"`
Encoding string `yaml:"encoding"`
Path string `yaml:"path"`
App string `yaml:"app"`
Enviroment string `yaml:"enviroment"`
Level utils.Level `yaml:"level"`
Encoding string `yaml:"encoding"`
Path string `yaml:"path"`
App string `yaml:"app"`
Environment string `yaml:"environment"`
}

func (c *LoggerConfig) Validate() error {
return nil // todo
}

type ServerConfig struct {
Enable bool `yaml:"enable_mock"`
Endpoint string `yaml:"endpoint`
Port string `yaml:"port"`
Enable bool `yaml:"enable"`
Endpoint string `yaml:"endpoint"`
Port int `yaml:"port" env:"PORT"`
JwtTokenSalt string `yaml:"jwt_token_salt" env:"JWT_TOKEN_SALT"`
}

func (c *ServerConfig) Validate() error {
Expand All @@ -36,11 +37,11 @@ func (c *ServerConfig) Validate() error {

type StorageConfig struct {
EnableMock bool `yaml:"enable_mock"`
Host string `yaml:"host" env:"HOST"`
Port int `yaml:"port" env:"PORT"`
Database string `yaml:"database" env:"DATABASE"`
Username string `yaml:"username" env:"USER"`
Password string `yaml:"password" env:"PASSWORD"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Database string `yaml:"database"`
Username string `yaml:"username"`
Password string `yaml:"password" env:"DB_PASSWORD"`
SSLMode string `yaml:"ssl_mode"`
ConnectionAttempts int `yaml:"connection_attempts"`
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/social-network/configuration/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ package configuration
import "social-network/internal/utils"

const (
defaultAppName = "social-network"
defaultEnviroment = "dev"
defaultAppName = "social-network"
defaultEnvironment = "dev"
)

func NewDefaultConfig() *Config {
return &Config{
Logger: &LoggerConfig{
Level: utils.InfoLevel,
Encoding: "console",
Path: "stdout",
App: defaultAppName,
Enviroment: defaultEnviroment,
Level: utils.InfoLevel,
Encoding: "console",
Path: "stdout",
App: defaultAppName,
Environment: defaultEnvironment,
},
Server: &ServerConfig{
Enable: false,
Endpoint: "",
Port: "",
Port: 0,
},
Storage: &StorageConfig{
EnableMock: false,
Expand Down
16 changes: 16 additions & 0 deletions cmd/social-network/local_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
logger:
level: "debug"
encoding: "console"
path: "stdout"

server:
enable: true
endpoint: "localhost"

storage:
host: "localhost"
port: 5432
database: "social-network-local"
username: "social-network-local-admin"
ssl_mode: "disable"
connection_attempts: 3
46 changes: 46 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: "3.9"

services:
backend:
container_name: "app"
build:
context: .
restart: always
ports:
- "8000:8000"
env_file:
- .env
environment:
- DB_PASSWORD=${DB_PASSWORD}
- PORT=8000
- JWT_TOKEN_SALT=${JWT_TOKEN_SALT}
depends_on:
- db
volumes:
- .:/app
db:
image: postgres:15-alpine
container_name: "db"
networks:
new:
aliases:
- database
env_file:
- .env.example
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=social-network-local
- POSTGRES_NAME=social-network-local
- POSTGRES_USER=social-network-local-admin
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5432:5432"
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d qtr_portal -U qtr_portal" ]
interval: 5s
retries: 5
restart: always

volumes:
postgres_data:
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ module social-network
go 1.21.3

require (
github.com/Masterminds/squirrel v1.5.4
github.com/go-chi/chi/v5 v5.0.10
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/golang-jwt/jwt/v5 v5.1.0
github.com/ilyakaznacheev/cleanenv v1.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.26.0
gopkg.in/validator.v2 v2.0.1
)

require (
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
)

require (
Expand Down
26 changes: 25 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Masterminds/squirrel v1.5.4 h1:uUcX/aBc8O7Fg9kaISIUsHXdKuqehiXAMQTYX8afzqM=
github.com/Masterminds/squirrel v1.5.4/go.mod h1:NNaOrjSoIDfDA40n7sr2tPNZRfjzjA400rg+riTZj10=
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/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a h1:v6zMvHuY9yue4+QkG/HQ/W67wvtQmWJ4SDo9aK/GIno=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a/go.mod h1:I79BieaU4fxrw4LMXby6q5OS9XnoR9UIKLOzDFjUmuw=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/golang-jwt/jwt/v5 v5.1.0 h1:UGKbA/IPjtS6zLcdB7i5TyACMgSbOTiR8qzXgw8HWQU=
github.com/golang-jwt/jwt/v5 v5.1.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0/go.mod h1:dXGbAdH5GtBTC4WfIxhKZfyBF/HBFgRZSWwZ9g/He9o=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhRWSsG5rVo6hYhAB/ADZrk=
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
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/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
Expand All @@ -25,8 +46,11 @@ go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
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/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY=
gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
Expand Down
33 changes: 23 additions & 10 deletions internal/model/user.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package model

import (
"fmt"
"gopkg.in/validator.v2"
"time"
)

type UserID string

func (id UserID) String() string {
Expand All @@ -13,7 +19,7 @@ type User struct {
SecondName string
Age int
Sex string
Birthdate string
Birthdate time.Time
Biography string
City string
}
Expand All @@ -29,13 +35,20 @@ type UserLogin struct {
}

type UserRegister struct {
Username string
HashedPassword string
FirstName string
SecondName string
Age int
Sex string
Birthdate string
Biography string
City string
ID string `validator:"nonzero"`
Username string `validator:"nonzero"`
HashedPassword string `validator:"nonzero"`
FirstName string `validator:"nonzero"`
SecondName string `validator:"nonzero"`
Sex string `validator:"nonzero"`
Birthdate time.Time `validator:"nonzero"`
Biography string `validator:"nonzero"`
City string `validator:"nonzero"`
}

func (u *UserRegister) Validate() error {
if err := validator.Validate(u); err != nil {
return fmt.Errorf("validate: %w", err)
}
return nil
}
31 changes: 18 additions & 13 deletions internal/storage/postgres/args.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package postgres

import "fmt"
import (
"fmt"
"strings"
)

const (
UserTable = "user_table"
Expand All @@ -13,16 +16,17 @@ const (
)

const (
fieldID = "id"
fieldUserID = "user_id"

fieldUsername = "username"
fieldFirstName = "first_name"
fieldSecondName = "second_name"
fieldAge = "age"
fieldSex = "sex"
fieldBirthdate = "birthdate"
fieldBiography = "biography"
fieldCity = "city"
fieldUsername = "username"
fieldHashedPassword = "hashed_password"
fieldFirstName = "first_name"
fieldSecondName = "second_name"
fieldSex = "sex"
fieldBirthdate = "birthdate"
fieldBiography = "biography"
fieldCity = "city"

fieldCreatedAt = "created_at"
fieldDeletedAt = "deleted_at"
Expand All @@ -33,11 +37,12 @@ const (

var (
userFields = []string{
fieldUserID, fieldUsername, fieldFirstName, fieldSecondName,
fieldAge, fieldSex, fieldBirthdate, fieldBiography, fieldCity,
fieldCreatedAt, fieldDeletedAt,
fieldID, fieldUsername, fieldHashedPassword, fieldFirstName, fieldSecondName,
fieldSex, fieldBirthdate, fieldBiography, fieldCity, fieldCreatedAt,
}
tokenFields = []string{fieldUserID, fieldToken, fieldCreatedAt, fieldDeletedAt, fieldAlivedAt}
tokenFields = []string{fieldID, fieldUserID, fieldToken, fieldCreatedAt, fieldAlivedAt}

returningUser = returning + strings.Join(userFields, separator)
)

func tableField(table, field string) string {
Expand Down
Loading

0 comments on commit b77484d

Please sign in to comment.