Skip to content

Commit

Permalink
grpc
Browse files Browse the repository at this point in the history
  • Loading branch information
nglmq committed Sep 2, 2024
1 parent b1615fa commit cd21794
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 23 deletions.
6 changes: 3 additions & 3 deletions internal/app/grpc/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *URLShortenerServer) SaveJSON(ctx context.Context, req *pb.SaveJSONReque
return nil, fmt.Errorf("failed to save URL to database: %v", err)
}

shortURL := fmt.Sprintf(config.FlagBaseURL + "/" + existAlias)
shortURL := config.FlagBaseURL + "/" + existAlias
return &pb.SaveJSONResponse{ShortUrl: shortURL}, nil
}

Expand All @@ -61,7 +61,7 @@ func (s *URLShortenerServer) SaveJSONBatch(ctx context.Context, req *pb.SaveJSON
return nil, fmt.Errorf("failed to save URL to database: %v", err)
}

shortURL := fmt.Sprintf(config.FlagBaseURL + "/" + existAlias)
shortURL := config.FlagBaseURL + "/" + existAlias
results = append(results, &pb.BatchURLResponse{
CorrelationId: batchReq.GetCorrelationId(),
ShortUrl: shortURL,
Expand Down Expand Up @@ -91,7 +91,7 @@ func (s *URLShortenerServer) GetAllURLs(ctx context.Context, req *pb.GetAllURLsR
var urlMappings []*pb.URLMapping
for alias, originalURL := range urls {
urlMappings = append(urlMappings, &pb.URLMapping{
ShortUrl: fmt.Sprintf(config.FlagBaseURL + "/" + alias),
ShortUrl: config.FlagBaseURL + "/" + alias,
OriginalUrl: originalURL,
})
}
Expand Down
13 changes: 0 additions & 13 deletions internal/app/grpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,6 @@ func StartGRPCServer() error {
shortener.DBStorage = dbStorage
}

if config.FlagInMemoryStorage != "" && config.DBConnection == "" {
fileStore, err := storage.NewFileStorage(config.FlagInMemoryStorage)
if err != nil {
return err
}
shortener.FileStorage = fileStore

if err = fileStore.ReadURLsFromFile(store.URLs); err != nil {
log.Printf("Error reading URLs from file: %v", err)
return err
}
}

lis, err := net.Listen("tcp", ":3200")
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
Expand Down
7 changes: 3 additions & 4 deletions internal/app/handlers/json_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"strconv"
Expand Down Expand Up @@ -89,7 +88,7 @@ func (us *URLShortener) JSONHandler(w http.ResponseWriter, r *http.Request) {
return
}
if existAlias != alias {
shortenedURL := fmt.Sprintf(config.FlagBaseURL + "/" + existAlias)
shortenedURL := config.FlagBaseURL + "/" + existAlias
contentLength := len(shortenedURL)

responseJSON = JSONResponse{
Expand Down Expand Up @@ -129,7 +128,7 @@ func (us *URLShortener) JSONHandler(w http.ResponseWriter, r *http.Request) {
// }
//}

shortenedURL := fmt.Sprintf(config.FlagBaseURL + "/" + alias)
shortenedURL := config.FlagBaseURL + "/" + alias
contentLength := len(shortenedURL)

responseJSON = JSONResponse{
Expand Down Expand Up @@ -228,7 +227,7 @@ func (us *URLShortener) JSONBatchHandler(w http.ResponseWriter, r *http.Request)

responseJSON[i] = JSONBatchResponse{
CorrelationID: req.CorrelationID,
ShortURL: fmt.Sprintf(config.FlagBaseURL + "/" + alias),
ShortURL: config.FlagBaseURL + "/" + alias,
}
}

Expand Down
5 changes: 2 additions & 3 deletions internal/app/handlers/save.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (us *URLShortener) ShortURLHandler(w http.ResponseWriter, r *http.Request)
return
}
if existAlias != alias {
shortenedURL := fmt.Sprintf(config.FlagBaseURL + "/" + existAlias)
shortenedURL := config.FlagBaseURL + "/" + existAlias

contentLength := len(shortenedURL)

Expand All @@ -93,7 +92,7 @@ func (us *URLShortener) ShortURLHandler(w http.ResponseWriter, r *http.Request)
}
}

shortenedURL := fmt.Sprintf(config.FlagBaseURL + "/" + alias)
shortenedURL := config.FlagBaseURL + "/" + alias
contentLength := len(shortenedURL)

w.Header().Set("Content-Type", "text/plain")
Expand Down

0 comments on commit cd21794

Please sign in to comment.