Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
feat: complete getting my user and users with token
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadne committed Aug 29, 2021
1 parent 511b3f3 commit 9568ba5
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 121 deletions.
4 changes: 2 additions & 2 deletions services/auth/internal/network/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func (s *grpcServer) TokenMetadata(ctx context.Context, token *contracts.TokenCo
return nil, err
}

_, err = s.cache.GetUserId(accessDetails)
userId, err := s.cache.GetUserId(accessDetails)
if err != nil {
return nil, err
}

return &contracts.TokenMetadataResponse{IsValid: true}, nil
return &contracts.TokenMetadataResponse{IsValid: true, Id: userId}, nil
}
2 changes: 2 additions & 0 deletions services/user/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ USER_DATABASE_SCHEMA=
USER_REST_URL=

USER_GRPC_URL=

AUTH_GRPC_URL=
10 changes: 7 additions & 3 deletions services/user/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/mohammadne/bookman/user/config"
"github.com/mohammadne/bookman/user/internal/database"
"github.com/mohammadne/bookman/user/internal/network"
grpc_server "github.com/mohammadne/bookman/user/internal/network/grpc/server"
"github.com/mohammadne/bookman/user/internal/network/grpc"
grpc_client "github.com/mohammadne/bookman/user/internal/network/grpc/clients"
"github.com/mohammadne/bookman/user/internal/network/rest"
"github.com/mohammadne/bookman/user/pkg/logger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,10 +53,13 @@ func main(environment config.Environment) {

db := database.NewMysqlDatabase(cfg.Database, log)

authGrpc := grpc_client.NewUser(cfg.GrpcAuth, log)
authGrpc.Setup()

// serving application servers
servers := []network.Server{
rest.New(cfg.Rest, log, db),
grpc_server.New(cfg.Grpc, log, db),
rest.New(cfg.Rest, log, db, authGrpc),
grpc.NewServer(cfg.GrpcServer, log, db),
}

for _, server := range servers {
Expand Down
11 changes: 6 additions & 5 deletions services/user/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package config

import (
"github.com/mohammadne/bookman/user/internal/database"
grpc_server "github.com/mohammadne/bookman/user/internal/network/grpc/server"
"github.com/mohammadne/bookman/user/internal/network/grpc"
"github.com/mohammadne/bookman/user/internal/network/rest"
"github.com/mohammadne/bookman/user/pkg/logger"
)

type Config struct {
Logger *logger.Config
Database *database.Config
Rest *rest.Config
Grpc *grpc_server.Config
Logger *logger.Config
Database *database.Config
Rest *rest.Config
GrpcServer *grpc.Config
GrpcAuth *grpc.Config
}
8 changes: 5 additions & 3 deletions services/user/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/mohammadne/bookman/user/internal/database"
grpc_server "github.com/mohammadne/bookman/user/internal/network/grpc/server"
"github.com/mohammadne/bookman/user/internal/network/grpc"
"github.com/mohammadne/bookman/user/internal/network/rest"
"github.com/mohammadne/bookman/user/pkg/logger"
)
Expand All @@ -23,14 +23,16 @@ func Load(env Environment) *Config {
cfg.Logger = &logger.Config{}
cfg.Database = &database.Config{}
cfg.Rest = &rest.Config{}
cfg.Grpc = &grpc_server.Config{}
cfg.GrpcServer = &grpc.Config{}
cfg.GrpcAuth = &grpc.Config{}

// process
envconfig.MustProcess("user", cfg)
envconfig.MustProcess("user_logger", cfg.Logger)
envconfig.MustProcess("user_database", cfg.Database)
envconfig.MustProcess("user_rest", cfg.Rest)
envconfig.MustProcess("user_grpc", cfg.Grpc)
envconfig.MustProcess("user_grpc", cfg.GrpcServer)
envconfig.MustProcess("auth_grpc", cfg.GrpcAuth)

return cfg
}
7 changes: 2 additions & 5 deletions services/user/internal/models/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package models

import "encoding/json"

type User struct {
Id uint64 `json:"id"`
FirstName string `json:"first_name"`
Expand All @@ -11,13 +9,12 @@ type User struct {
DateCreated string `json:"date_created,omitempty"`
}

func (user *User) Marshall(isPublic bool) interface{} {
func (user *User) Marshall(isPublic bool) *User {
user.Password = ""
if isPublic {
user.Email = ""
user.DateCreated = ""
}

userJson, _ := json.Marshal(user)
return userJson
return user
}
34 changes: 0 additions & 34 deletions services/user/internal/network/grpc/client/auth.go

This file was deleted.

35 changes: 0 additions & 35 deletions services/user/internal/network/grpc/client/client.go

This file was deleted.

5 changes: 0 additions & 5 deletions services/user/internal/network/grpc/client/config.go

This file was deleted.

61 changes: 61 additions & 0 deletions services/user/internal/network/grpc/clients/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package grpc_clients

import (
context "context"

"github.com/mohammadne/bookman/user/internal/network/grpc/contracts"

"github.com/mohammadne/bookman/user/internal/network/grpc"
"github.com/mohammadne/go-pkgs/failures"
"github.com/mohammadne/go-pkgs/logger"
grpcPkg "google.golang.org/grpc"
)

type Auth interface {
GetTokenMetadata(string) (uint64, failures.Failure)
}

type authClient struct {
logger logger.Logger
config *grpc.Config

client contracts.AuthClient
}

func NewUser(cfg *grpc.Config, log logger.Logger) *authClient {
return &authClient{config: cfg, logger: log}
}

func (g *authClient) Setup() {
authConnection, err := grpcPkg.Dial(g.config.Url, grpcPkg.WithInsecure())
if err != nil {
g.logger.Fatal(
"error getting auth grpc connection",
logger.String("address", g.config.Url),
logger.Error(err),
)
}

g.client = contracts.NewAuthClient(authConnection)
}

var (
errGettingToken = failures.Rest{}.NewInternalServer("error getting token metadata")
invalidToken = failures.Rest{}.NewInternalServer("invalid token")
)

func (g *authClient) GetTokenMetadata(token string) (uint64, failures.Failure) {
contract := &contracts.TokenContract{Token: token}
response, err := g.client.TokenMetadata(context.Background(), contract)
if err != nil {
g.logger.Error("grpc get token metadata", logger.Error(err))
return 0, errGettingToken
}

if !response.IsValid {
g.logger.Error("not valid token metadata", logger.Error(err))
return 0, invalidToken
}

return response.Id, nil
}
5 changes: 5 additions & 0 deletions services/user/internal/network/grpc/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package grpc

type Config struct {
Url string
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mohammadne/bookman/user/internal/network/grpc/contracts"
"github.com/mohammadne/go-pkgs/failures"
"github.com/mohammadne/go-pkgs/logger"
grpc "google.golang.org/grpc"
"google.golang.org/grpc"
)

type grpcServer struct {
Expand All @@ -23,7 +23,7 @@ type grpcServer struct {
contracts.UnimplementedUserServer
}

func New(cfg *Config, log logger.Logger, db database.Database) *grpcServer {
func NewServer(cfg *Config, log logger.Logger, db database.Database) *grpcServer {
s := &grpcServer{config: cfg, logger: log, database: db}

s.server = grpc.NewServer()
Expand All @@ -33,7 +33,7 @@ func New(cfg *Config, log logger.Logger, db database.Database) *grpcServer {
}

func (s *grpcServer) Serve(<-chan struct{}) {
listener, err := net.Listen("tcp", s.config.URL)
listener, err := net.Listen("tcp", s.config.Url)
if err != nil {
panic(err)
}
Expand Down
6 changes: 0 additions & 6 deletions services/user/internal/network/grpc/server/config.go

This file was deleted.

45 changes: 30 additions & 15 deletions services/user/internal/network/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,49 @@ import (
"strconv"

"github.com/labstack/echo/v4"
"github.com/mohammadne/bookman/user/internal/models"
"github.com/mohammadne/go-pkgs/failures"
"github.com/mohammadne/go-pkgs/logger"
)

// get is responsible to provide HTTP Get Location functionality
func (rest *restEcho) getUser(ctx echo.Context) error {
idStr := ctx.Param("id")
if idStr == "" {
rest.logger.Error("user id is nil")
return ctx.String(http.StatusBadRequest, "bad request")
user, failure := rest.getUserByIdString(ctx.Param("id"))
if failure != nil {
return ctx.JSON(failure.Status(), failure)
}

id, parseErr := strconv.ParseInt(idStr, 10, 64)
if parseErr != nil {
rest.logger.Error("user id is malformed")
return ctx.String(http.StatusBadRequest, "bad request")
}
return ctx.JSON(http.StatusOK, user.Marshall(true))
}

user, readErr := rest.database.FindUserById(id)
if readErr != nil {
return ctx.JSON(readErr.Status(), readErr)
func (rest *restEcho) getMyUser(ctx echo.Context) error {
user, failure := rest.getUserByIdString(ctx.Get("token_user_id").(string))
if failure != nil {
rest.logger.Error(failure.Message(), logger.Error(failure))
return ctx.JSON(failure.Status(), failure)
}

return ctx.JSON(http.StatusOK, user.Marshall(false))
}

func (rest *restEcho) getMyUser(ctx echo.Context) error {
func (rest *restEcho) searchUsers(ctx echo.Context) error {
return nil
}

func (rest *restEcho) searchUsers(ctx echo.Context) error {
return nil
func (rest *restEcho) getUserByIdString(idStr string) (*models.User, failures.Failure) {
if idStr == "" {
return nil, failures.Rest{}.NewBadRequest("invalid user id is given")
}

id, parseErr := strconv.ParseInt(idStr, 10, 64)
if parseErr != nil {
return nil, failures.Rest{}.NewBadRequest("given user id is malformed")
}

user, failure := rest.database.FindUserById(id)
if failure != nil {
return nil, failure
}

return user, nil
}
Loading

0 comments on commit 9568ba5

Please sign in to comment.