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

Sprint 10 #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ vendor/

# IDEs directories
.idea
.vscode
.vscode
30 changes: 11 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,36 @@ go 1.17

require (
github.com/caarlos0/env/v6 v6.9.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi/v5 v5.0.7
github.com/go-resty/resty/v2 v2.7.0
github.com/google/uuid v1.3.0
github.com/gostaticanalysis/sqlrows v0.0.0-20200307153552-ea5697937269
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/lib/pq v1.10.6
github.com/reillywatson/lintservemux v0.0.0-20191102120836-0e75fcfb6a46
github.com/stretchr/testify v1.8.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/tools v0.1.12
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.27.1
honnef.co/go/tools v0.3.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-critic/go-critic v0.6.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
github.com/gostaticanalysis/sqlrows v0.0.0-20200307153552-ea5697937269 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.0 // indirect
github.com/jgautheron/goconst v1.5.1 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quasilyte/go-ruleguard/dsl v0.3.21 // indirect
github.com/reillywatson/lintservemux v0.0.0-20191102120836-0e75fcfb6a46 // indirect
github.com/stretchr/objx v0.4.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.3.3 // indirect
)
283 changes: 106 additions & 177 deletions go.sum

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zhel1/yandex-practicum-go/internal/storage/inmemory"
"github.com/zhel1/yandex-practicum-go/internal/storage/inpsql"
"log"
"net"
nethttp "net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -50,38 +51,59 @@ func Run() {
log.Fatal(err)
}

var ipnet *net.IPNet = nil
if cfg.TrustedSubnet != "" {
_, ipnet, err = net.ParseCIDR(cfg.TrustedSubnet)
if err != nil {
log.Fatal(err)
}
}

deps := service.Deps{
Storage: strg,
BaseURL: cfg.BaseURL,
TokenManager: tokenManager,
Storage: strg,
BaseURL: cfg.BaseURL,
TokenManager: tokenManager,
TrustedSubnet: ipnet,
}

services := service.NewServices(deps)

// HTTP server
handlers := http.NewHandler(services)
httpSrv := server.NewHTTPServer(&cfg, handlers.Init())

//GRPC server
grpcSrv := server.NewGRPCServer(&cfg, services)

// HTTP Server
srv := server.NewServer(&cfg, handlers.Init())
connectionsClosed := make(chan struct{})
interrupt := make(chan os.Signal, 1)

signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

go func() {
<-interrupt
if err := srv.Stop(context.Background()); err != nil {
if err := httpSrv.Stop(context.Background()); err != nil {
log.Printf("HTTP server shutdown: %v", err)
}

grpcSrv.Stop()

if err := strg.Close(); err != nil {
log.Printf("Storage shutdown: %v", err)
}

close(connectionsClosed)
}()

if err := srv.Run(); err != nethttp.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
go func() {
if err := httpSrv.Run(); err != nethttp.ErrServerClosed {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}()

go func() {
grpcSrv.Run()
}()

<-connectionsClosed
log.Println("Server shutdown gracefully")
}
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
UserKey string `env:"USER_KEY" envDefault:"PaSsW0rD" json:"user_key"`
DatabaseDSN string `env:"DATABASE_DSN" json:"database_dsn"`
EnableHTTPS bool `env:"ENABLE_HTTPS" json:"enable_https"`
TrustedSubnet string `env:"TRUSTED_SUBNET" json:"trusted_subnet"`
Config string `env:"CONFIG" json:"-"`
}

Expand Down Expand Up @@ -47,6 +48,7 @@ func (c *Config) Parse() error {
flag.StringVar(&tempConf.UserKey, "p", "", "UserKey for encryption cookie")
flag.StringVar(&tempConf.DatabaseDSN, "d", "", "The line with the address to connect to the database")
flag.BoolVar(&tempConf.EnableHTTPS, "s", false, "Enable HTTPS mode in web-server")
flag.StringVar(&tempConf.TrustedSubnet, "t", "192.168.1.0/24", "Trusted subnet")
flag.StringVar(&tempConf.Config, "config", "", "Config file")
flag.StringVar(&tempConf.Config, "c", "", "Config file")
flag.Parse()
Expand Down Expand Up @@ -82,6 +84,9 @@ func (c *Config) Parse() error {
if isFlagPassed("s") {
c.EnableHTTPS = tempConf.EnableHTTPS
}
if isFlagPassed("t") {
c.TrustedSubnet = tempConf.TrustedSubnet
}

// settings redefinition from evn
err := env.Parse(c)
Expand Down
11 changes: 6 additions & 5 deletions internal/dto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package dto
import "errors"

var (
ErrNotFound = errors.New("not found")
ErrDeleted = errors.New("marked as deleted")
ErrAlreadyExists = errors.New("already exists")
ErrExecutionPSQL = errors.New("execution PSQL error")
ErrStatementPSQL = errors.New("statement PSQL error")
ErrNotFound = errors.New("not found")
ErrDeleted = errors.New("marked as deleted")
ErrAlreadyExists = errors.New("already exists")
ErrExecutionPSQL = errors.New("execution PSQL error")
ErrStatementPSQL = errors.New("statement PSQL error")
ErrInvalidArgument = errors.New("bad userID")
)
7 changes: 7 additions & 0 deletions internal/dto/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dto

//Statistic struct
type Statistic struct {
URLsCount int `json:"urls"`
UsersCount int `json:"users"`
}
3 changes: 2 additions & 1 deletion internal/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/zhel1/yandex-practicum-go/internal/http/middleware"
v1 "github.com/zhel1/yandex-practicum-go/internal/http/v1"
"github.com/zhel1/yandex-practicum-go/internal/service"
"github.com/zhel1/yandex-practicum-go/internal/utils"
"io"
"net/http"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func (h *Handler) initAPI(router chi.Router) {
// AddLink accepts a URL string in the request body for shortening.
func (h *Handler) AddLink() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := middleware.TakeUserID(r.Context())
userID, err := utils.ExtractValueFromContext(r.Context(), dto.UserIDCtxName)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
22 changes: 18 additions & 4 deletions internal/http/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ func (ht *HandlersTestSuite) TestGetLink() {
wantCode: http.StatusTemporaryRedirect,
},
{
name: "Positive test #1",
name: "Positive test #2",
value: "1234568",
wantCode: http.StatusTemporaryRedirect,
},
{
name: "Negative test #2. No link in database.",
name: "Negative test #3. No link in database.",
value: "1234569",
wantCode: http.StatusBadRequest,
},
{
name: "Negative test #3 . Not existing path.",
name: "Negative test #4 . Not existing path.",
value: "1234567/1234567",
wantCode: http.StatusNotFound,
},
{
name: "Negative test #4. Empty path (redirection test).",
name: "Negative test #5. Empty path (redirection test).",
value: "",
wantCode: http.StatusNotFound,
},
Expand Down Expand Up @@ -186,3 +186,17 @@ func (ht *HandlersTestSuite) TestAddLink() {
})
}
}

func (ht *HandlersTestSuite) TestPing() {
ht.router.Use(ht.cookieHandler.CookieHandler)
ht.router.Get("/ping", ht.handler.Ping())
defer ht.ts.Close()

ht.T().Run("Ping", func(t *testing.T) {
client := resty.New()
resp, err := client.R().Get(ht.ts.URL + "/ping")
require.NoError(t, err)

assert.Equal(t, http.StatusOK, resp.StatusCode())
})
}
20 changes: 3 additions & 17 deletions internal/http/middleware/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func (h *CookieHandler) CookieHandler(next http.Handler) http.Handler {
var userID string
if errors.Is(err, http.ErrNoCookie) { //no cookie
userID = uuid.New().String()
http.SetCookie(w, h.CreateNewCookie(r.Context(), userID))
http.SetCookie(w, h.createNewCookie(r.Context(), userID))
} else if err != nil {
http.Error(w, "Cookie crumbled", http.StatusInternalServerError)
} else { //cookie found
userID, err = h.services.Users.CheckToken(r.Context(), userIDCookie.Value)
if err != nil {
http.SetCookie(w, h.CreateNewCookie(r.Context(), userID))
http.SetCookie(w, h.createNewCookie(r.Context(), userID))
}
}

Expand All @@ -48,21 +48,7 @@ func (h *CookieHandler) CookieHandler(next http.Handler) http.Handler {
})
}

func TakeUserID(context context.Context) (string, error) {
userIDCtx := ""
if id := context.Value(dto.UserIDCtxName); id != nil {
userIDCtx = id.(string)
}

if userIDCtx == "" {
return "", errors.New("empty user id")
}
return userIDCtx, nil
}

//**********************************************************************************************************************

func (h *CookieHandler) CreateNewCookie(ctx context.Context, userID string) *http.Cookie {
func (h *CookieHandler) createNewCookie(ctx context.Context, userID string) *http.Cookie {
token, err := h.services.Users.CreateNewToken(ctx, userID)
if err != nil {
panic(err.Error())
Expand Down
1 change: 1 addition & 0 deletions internal/http/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ func NewHandler(services *service.Services) *Handler {
func (h *Handler) Init(r chi.Router) {
h.initShortenRoutes(r)
h.initUserRoutes(r)
h.initInternalRoutes(r)
}
52 changes: 52 additions & 0 deletions internal/http/v1/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v1

import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-chi/chi/v5"
"net/http"
)

func (h *Handler) initInternalRoutes(r chi.Router) {
r.Route("/internal", func(r chi.Router) {
r.Get("/stats", h.GetStats())
})
}

// GetUserLinks returns to the user all links ever saved by him.
func (h *Handler) GetStats() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ipStr := r.Header.Get("X-Real-IP")

//TODO move it in middleware
trusted, err := h.services.Security.IsIpAddrTrusted(r.Context(), ipStr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if !trusted {
http.Error(w, "the ip address is not on a trusted network", http.StatusForbidden)
return
}

stat, err := h.services.Internal.GetStatistic(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusNoContent)
return
}

buf := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
if err = encoder.Encode(stat); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("content-type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, buf)
}
}
6 changes: 3 additions & 3 deletions internal/http/v1/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"github.com/go-chi/chi/v5"
"github.com/zhel1/yandex-practicum-go/internal/dto"
"github.com/zhel1/yandex-practicum-go/internal/http/middleware"
"github.com/zhel1/yandex-practicum-go/internal/utils"
"net/http"
)

Expand All @@ -22,7 +22,7 @@ func (h *Handler) initShortenRoutes(r chi.Router) {
// AddLinkJSON accepts a JSON object in the request body and returning a JSON object in response.
func (h *Handler) AddLinkJSON() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := middleware.TakeUserID(r.Context())
userID, err := utils.ExtractValueFromContext(r.Context(), dto.UserIDCtxName)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down Expand Up @@ -62,7 +62,7 @@ func (h *Handler) AddLinkJSON() http.HandlerFunc {
// AddLinkBatchJSON accepts in the request body a set of URLs for shortening in the format.
func (h *Handler) AddLinkBatchJSON() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, err := middleware.TakeUserID(r.Context())
userID, err := utils.ExtractValueFromContext(r.Context(), dto.UserIDCtxName)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
Loading