-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added grpc to delivery for trips * microservices added * changed main * attractions service fixed * userService * gRPC for users * fixing * added auth routes * services fixed * returned token in cookie * fixed login and search * scheme changed * made structure for survey's microservice * fixed names * added image path to searh response * fix * fixed getPlace * proto added * http handler for surveys * made http handlers for surveys * grpc server added * started repo * started usecase for surveys * finished repo * made usecase for surveys * grpc server added * SCHEME BD CHANGE * SCHEME BD CHANGE * SCHEME BD CHANGE * fixed mistakes with error handling * ne znay * ne znay * ne znay * ne znay * fixed authorization * fixed trip queries * added field description to db * added global search to microservices * added photos to trips * fixed deleting review * fixed trip queries * temporary loggig * fixed base64 decoding * saving only file_name * fixed review route * fixed routing * added photo deletion * deleted temporary logging * minor fix * temporary logging * deleted temporary logging * made logging --------- Co-authored-by: timurIsaevIY <isaevtimur2016@gmail.com> Co-authored-by: AnnHarvard <dana.shakleina@yandex.ru>
- Loading branch information
1 parent
4539932
commit bdfb3cc
Showing
17 changed files
with
261 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package dblogger | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"log/slog" | ||
"time" | ||
) | ||
|
||
type DB struct { | ||
db *sql.DB | ||
logger *slog.Logger | ||
} | ||
|
||
func NewDB(db *sql.DB, logger *slog.Logger) *DB { | ||
return &DB{ | ||
db: db, | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) { | ||
start := time.Now() | ||
rows, err := d.db.QueryContext(ctx, query, args...) | ||
duration := time.Since(start) | ||
|
||
d.logger.DebugContext(ctx, "Executing QueryContext", | ||
slog.String("query", query), | ||
slog.Any("args", args), | ||
slog.Duration("duration", duration), | ||
slog.String("error", errToString(err)), | ||
) | ||
|
||
return rows, err | ||
} | ||
|
||
func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { | ||
start := time.Now() | ||
result, err := d.db.ExecContext(ctx, query, args...) | ||
duration := time.Since(start) | ||
|
||
d.logger.DebugContext(ctx, "Executing ExecContext", | ||
slog.String("query", query), | ||
slog.Any("args", args), | ||
slog.Duration("duration", duration), | ||
slog.String("error", errToString(err)), | ||
) | ||
|
||
return result, err | ||
} | ||
|
||
func (d *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row { | ||
start := time.Now() | ||
row := d.db.QueryRowContext(ctx, query, args...) | ||
duration := time.Since(start) | ||
|
||
d.logger.DebugContext(ctx, "Executing QueryRowContext", | ||
slog.String("query", query), | ||
slog.Any("args", args), | ||
slog.Duration("duration", duration), | ||
) | ||
|
||
return row | ||
} | ||
|
||
func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) { | ||
start := time.Now() | ||
stmt, err := d.db.PrepareContext(ctx, query) | ||
duration := time.Since(start) | ||
|
||
d.logger.DebugContext(ctx, "Preparing statement", | ||
slog.String("query", query), | ||
slog.Duration("duration", duration), | ||
slog.String("error", errToString(err)), | ||
) | ||
|
||
return stmt, err | ||
} | ||
|
||
func (d *DB) Close() error { | ||
return d.db.Close() | ||
} | ||
|
||
func errToString(err error) string { | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return "nil" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package middleware | ||
|
||
import ( | ||
log "2024_2_ThereWillBeName/internal/pkg/logger" | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
RequestIDKey contextKey = "request_id" | ||
MethodKey contextKey = "method" | ||
PathKey contextKey = "path" | ||
) | ||
|
||
type responseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
func (rw *responseWriter) WriteHeader(code int) { | ||
rw.statusCode = code | ||
rw.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
func RequestLoggerMiddleware(logger *slog.Logger) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
requestID := uuid.New().String() | ||
|
||
logCtx := r.Context() | ||
logCtx = log.AppendCtx(logCtx, slog.String("request_id", requestID)) | ||
logCtx = log.AppendCtx(logCtx, slog.String("method", r.Method)) | ||
logCtx = log.AppendCtx(logCtx, slog.String("path", r.URL.Path)) | ||
r = r.WithContext(logCtx) | ||
|
||
startTime := time.Now() | ||
logger.Info("Request started", | ||
slog.String("request_id", requestID), | ||
slog.String("method", r.Method), | ||
slog.String("path", r.URL.Path), | ||
slog.Time("start_time", startTime), | ||
) | ||
|
||
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} | ||
|
||
next.ServeHTTP(rw, r) | ||
|
||
duration := time.Since(startTime) | ||
|
||
logger.Info("Request completed", | ||
slog.String("request_id", requestID), | ||
slog.String("method", r.Method), | ||
slog.String("path", r.URL.Path), | ||
slog.Int("status_code", rw.statusCode), | ||
slog.Duration("duration", duration), | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.