users

package
v0.0.0-...-f5cdc36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2023 License: Apache-2.0 Imports: 52 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserExchange = "users"

	AvatarsQueueName   = "avatars_queue"
	AvatarsConsumerTag = "user_avatar_consumer"
	AvatarsWorkers     = 5
	AvatarsBindingKey  = "update_avatar_key"
)

Variables

View Source
var File_app_users_user_proto protoreflect.FileDescriptor

Functions

func RegisterUserServiceServer

func RegisterUserServiceServer(s *grpc.Server, srv UserServiceServer)

func SessionsGRPCClientFactory

func SessionsGRPCClientFactory(
	logger logger.Logger,
	cfg *config.Config,
	tracer opentracing.Tracer,
) func(ctx context.Context) (*grpc.ClientConn, sessions.AuthorizationServiceClient, error)

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

func NewApplication

func NewApplication(logger logger.Logger, cfg *config.Config, redisConn *redis.Client, pgxPool *pgxpool.Pool, tracer opentracing.Tracer) Application

func (*Application) MapRoutes

func (a *Application) MapRoutes()

func (*Application) Run

func (a *Application) Run() error

type GetByIDRequest

type GetByIDRequest struct {
	UserID string `protobuf:"bytes,1,opt,name=UserID,proto3" json:"UserID,omitempty"`
	// contains filtered or unexported fields
}

func (*GetByIDRequest) Descriptor deprecated

func (*GetByIDRequest) Descriptor() ([]byte, []int)

Deprecated: Use GetByIDRequest.ProtoReflect.Descriptor instead.

func (*GetByIDRequest) GetUserID

func (x *GetByIDRequest) GetUserID() string

func (*GetByIDRequest) ProtoMessage

func (*GetByIDRequest) ProtoMessage()

func (*GetByIDRequest) ProtoReflect

func (x *GetByIDRequest) ProtoReflect() protoreflect.Message

func (*GetByIDRequest) Reset

func (x *GetByIDRequest) Reset()

func (*GetByIDRequest) String

func (x *GetByIDRequest) String() string

type GetByIDResponse

type GetByIDResponse struct {
	User *User `protobuf:"bytes,1,opt,name=User,proto3" json:"User,omitempty"`
	// contains filtered or unexported fields
}

func (*GetByIDResponse) Descriptor deprecated

func (*GetByIDResponse) Descriptor() ([]byte, []int)

Deprecated: Use GetByIDResponse.ProtoReflect.Descriptor instead.

func (*GetByIDResponse) GetUser

func (x *GetByIDResponse) GetUser() *User

func (*GetByIDResponse) ProtoMessage

func (*GetByIDResponse) ProtoMessage()

func (*GetByIDResponse) ProtoReflect

func (x *GetByIDResponse) ProtoReflect() protoreflect.Message

func (*GetByIDResponse) Reset

func (x *GetByIDResponse) Reset()

func (*GetByIDResponse) String

func (x *GetByIDResponse) String() string

type GetUsersByIDsRequest

type GetUsersByIDsRequest struct {
	UsersIDs []string `protobuf:"bytes,1,rep,name=UsersIDs,proto3" json:"UsersIDs,omitempty"`
	// contains filtered or unexported fields
}

func (*GetUsersByIDsRequest) Descriptor deprecated

func (*GetUsersByIDsRequest) Descriptor() ([]byte, []int)

Deprecated: Use GetUsersByIDsRequest.ProtoReflect.Descriptor instead.

func (*GetUsersByIDsRequest) GetUsersIDs

func (x *GetUsersByIDsRequest) GetUsersIDs() []string

func (*GetUsersByIDsRequest) ProtoMessage

func (*GetUsersByIDsRequest) ProtoMessage()

func (*GetUsersByIDsRequest) ProtoReflect

func (x *GetUsersByIDsRequest) ProtoReflect() protoreflect.Message

func (*GetUsersByIDsRequest) Reset

func (x *GetUsersByIDsRequest) Reset()

func (*GetUsersByIDsRequest) String

func (x *GetUsersByIDsRequest) String() string

type GetUsersByIDsResponse

type GetUsersByIDsResponse struct {
	Users []*User `protobuf:"bytes,1,rep,name=Users,proto3" json:"Users,omitempty"`
	// contains filtered or unexported fields
}

func (*GetUsersByIDsResponse) Descriptor deprecated

func (*GetUsersByIDsResponse) Descriptor() ([]byte, []int)

Deprecated: Use GetUsersByIDsResponse.ProtoReflect.Descriptor instead.

func (*GetUsersByIDsResponse) GetUsers

func (x *GetUsersByIDsResponse) GetUsers() []*User

func (*GetUsersByIDsResponse) ProtoMessage

func (*GetUsersByIDsResponse) ProtoMessage()

func (*GetUsersByIDsResponse) ProtoReflect

func (x *GetUsersByIDsResponse) ProtoReflect() protoreflect.Message

func (*GetUsersByIDsResponse) Reset

func (x *GetUsersByIDsResponse) Reset()

func (*GetUsersByIDsResponse) String

func (x *GetUsersByIDsResponse) String() string

type HTTPTransport

type HTTPTransport struct {
	// contains filtered or unexported fields
}

func NewHTTPServer

func NewHTTPServer(
	group *echo.Group,
	service Service,
	logger logger.Logger,
	validate *validator.Validate,
	cfg *config.Config,
	mw *SessionManager,
) HTTPTransport

func (*HTTPTransport) Delete

func (h *HTTPTransport) Delete() echo.HandlerFunc

func (*HTTPTransport) GetCSRFToken

func (h *HTTPTransport) GetCSRFToken() echo.HandlerFunc

GetCSRFToken godoc @Summary Get csrf token @Tags UserDO @Description Get csrf token, required session @Accept json @Produce json @Success 204 "" @Router /api/v1/users/csrf [get]

func (*HTTPTransport) GetMe

func (h *HTTPTransport) GetMe() echo.HandlerFunc

GetMe godoc @Summary Get current user data @Tags UserDO @Description Get current user data, required session cookie @Accept json @Produce json @Success 200 {object} UserResponse @Router /api/v1/users/me [get]

func (*HTTPTransport) GetUserByID

func (h *HTTPTransport) GetUserByID() echo.HandlerFunc

GetUserByID godoc @Summary Get user by id @Tags UserDO @Description Get user data by id @Accept json @Produce json @Param id path int false "user uuid" @Success 200 {object} UserResponse @Router /api/v1/users/{id} [get]

func (*HTTPTransport) Login

func (h *HTTPTransport) Login() echo.HandlerFunc

Login godoc @Summary Login user @Tags UserDO @Description login user, returns user data and session @Accept json @Produce json @Param data body Login true "email and password" @Success 200 {object} UserResponse @Router /api/v1/users/login [post]

func (*HTTPTransport) Logout

func (h *HTTPTransport) Logout() echo.HandlerFunc

Logout godoc @Summary Logout user @Tags UserDO @Description Logout user, return no content @Accept json @Produce json @Success 204 "" @Router /api/v1/users/logout [post]

func (*HTTPTransport) MapUserRoutes

func (h *HTTPTransport) MapUserRoutes()

func (*HTTPTransport) Register

func (h *HTTPTransport) Register() echo.HandlerFunc

Register godoc @Summary Register new user @Tags UserDO @Description register new user account, returns user data and session @Accept json @Produce json @Param data body UserDO true "user data" @Success 201 {object} UserResponse @Router /api/v1/users/register [post]

func (*HTTPTransport) Update

func (h *HTTPTransport) Update() echo.HandlerFunc

Update godoc @Summary Update user @Tags UserDO @Description update user profile @Accept json @Produce json @Success 200 {object} UserResponse @Router /api/v1/users/{id} [put]

func (*HTTPTransport) UpdateAvatar

func (h *HTTPTransport) UpdateAvatar() echo.HandlerFunc

UpdateAvatar godoc @Summary Update user avatar @Tags UserDO @Description Upload user avatar image @Accept mpfd @Produce json @Param id path int false "user uuid" @Success 200 {object} UserResponse @Router /api/v1/users/{id}/avatar [put]

type Login

type Login struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required,min=6,max=250"`
}

type Publisher

type Publisher interface {
	CreateExchangeAndQueue(exchange, queueName, bindingKey string) (*amqp.Channel, error)
	Publish(ctx context.Context, exchange, routingKey, contentType string, headers amqp.Table, body []byte) error
}

type PublisherImpl

type PublisherImpl struct {
	// contains filtered or unexported fields
}

func NewUserPublisher

func NewUserPublisher(cfg *config.Config, logger logger.Logger) (PublisherImpl, error)

func (*PublisherImpl) CreateExchangeAndQueue

func (p *PublisherImpl) CreateExchangeAndQueue(exchange, queueName, bindingKey string) (*amqp.Channel, error)

func (*PublisherImpl) Publish

func (p *PublisherImpl) Publish(ctx context.Context, exchange, routingKey, contentType string, headers amqp.Table, body []byte) error

Publish message

type RabbitConsumerImpl

type RabbitConsumerImpl struct {
	// contains filtered or unexported fields
}

func NewConsumer

func NewConsumer(logger logger.Logger, cfg *config.Config, service Service) RabbitConsumerImpl

func (*RabbitConsumerImpl) CreateExchangeAndQueue

func (c *RabbitConsumerImpl) CreateExchangeAndQueue(exchangeName, queueName, bindingKey string) (*amqp.Channel, error)

Consume messages

func (*RabbitConsumerImpl) Dial

func (c *RabbitConsumerImpl) Dial() error

func (*RabbitConsumerImpl) RunConsumers

func (c *RabbitConsumerImpl) RunConsumers(ctx context.Context, cancel context.CancelFunc)

type RedisRepository

type RedisRepository interface {
	SaveUser(ctx context.Context, user *UserResponse) error
	GetUserByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)
	DeleteUser(ctx context.Context, userID uuid.UUID) error
}

type RedisRepositoryImpl

type RedisRepositoryImpl struct {
	// contains filtered or unexported fields
}

func NewRedisRepository

func NewRedisRepository(redisConn *redis.Client, prefix string, expiration time.Duration) RedisRepositoryImpl

func (*RedisRepositoryImpl) DeleteUser

func (u *RedisRepositoryImpl) DeleteUser(ctx context.Context, userID uuid.UUID) error

func (*RedisRepositoryImpl) GetUserByID

func (u *RedisRepositoryImpl) GetUserByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)

func (*RedisRepositoryImpl) SaveUser

func (u *RedisRepositoryImpl) SaveUser(ctx context.Context, user *UserResponse) error

type Repository

type Repository interface {
	Create(ctx context.Context, user *UserDO) (*UserResponse, error)
	GetByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)
	GetByEmail(ctx context.Context, email string) (*UserDO, error)
	Update(ctx context.Context, user *UserUpdate) (*UserResponse, error)
	UpdateAvatar(ctx context.Context, msg images.UploadedImageMsg) (*UserResponse, error)
	GetUsersByIDs(ctx context.Context, userIDs []string) ([]*UserResponse, error)
}

type RepositoryImpl

type RepositoryImpl struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository(db *pgxpool.Pool) RepositoryImpl

func (*RepositoryImpl) Create

func (u *RepositoryImpl) Create(ctx context.Context, user *UserDO) (*UserResponse, error)

Create new user

func (*RepositoryImpl) GetByEmail

func (u *RepositoryImpl) GetByEmail(ctx context.Context, email string) (*UserDO, error)

func (*RepositoryImpl) GetByID

func (u *RepositoryImpl) GetByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)

Get user by id

func (*RepositoryImpl) GetUsersByIDs

func (u *RepositoryImpl) GetUsersByIDs(ctx context.Context, userIDs []string) ([]*UserResponse, error)

func (*RepositoryImpl) Update

func (u *RepositoryImpl) Update(ctx context.Context, user *UserUpdate) (*UserResponse, error)

func (*RepositoryImpl) UpdateAvatar

func (u *RepositoryImpl) UpdateAvatar(ctx context.Context, msg images.UploadedImageMsg) (*UserResponse, error)

type RequestCtxSession

type RequestCtxSession struct{}

type RequestCtxUser

type RequestCtxUser struct{}

type Role

type Role string
const (
	RoleGuest  Role = "guest"
	RoleMember Role = "member"
	RoleAdmin  Role = "admin"
)

func (*Role) Scan

func (e *Role) Scan(src interface{}) error

func (*Role) ToString

func (e *Role) ToString() string

type ServerImpl

type ServerImpl struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(service Service, logger logger.Logger) ServerImpl

func (*ServerImpl) GetUserByID

func (e *ServerImpl) GetUserByID(ctx context.Context, r *GetByIDRequest) (*GetByIDResponse, error)

func (*ServerImpl) GetUsersByIDs

type Service

type Service interface {
	Register(ctx context.Context, user *UserDO) (*UserResponse, error)
	Login(ctx context.Context, login Login) (*UserDO, error)
	GetByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)
	CreateSession(ctx context.Context, userID uuid.UUID) (string, error)
	GetSessionByID(ctx context.Context, sessionID string) (*sessions.SessionDO, error)
	GetCSRFToken(ctx context.Context, sessionID string) (string, error)
	DeleteSession(ctx context.Context, sessionID string) error
	Update(ctx context.Context, user *UserUpdate) (*UserResponse, error)
	UpdateUploadedAvatar(ctx context.Context, delivery amqp.Delivery) error
	UpdateAvatar(ctx context.Context, data *images.UpdateAvatarMsg) error
	GetUsersByIDs(ctx context.Context, userIDs []string) ([]*UserResponse, error)
}

type ServiceImpl

type ServiceImpl struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(
	repository Repository,
	redisRepository RedisRepository,
	log logger.Logger,
	amqpPublisher Publisher,
	grpcClientFactory func(ctx context.Context) (*grpc.ClientConn, sessions.AuthorizationServiceClient, error),
) ServiceImpl

func (*ServiceImpl) CreateSession

func (s *ServiceImpl) CreateSession(ctx context.Context, userID uuid.UUID) (string, error)

func (*ServiceImpl) DeleteSession

func (s *ServiceImpl) DeleteSession(ctx context.Context, sessionID string) error

func (*ServiceImpl) GetByID

func (s *ServiceImpl) GetByID(ctx context.Context, userID uuid.UUID) (*UserResponse, error)

func (*ServiceImpl) GetCSRFToken

func (s *ServiceImpl) GetCSRFToken(ctx context.Context, sessionID string) (string, error)

func (*ServiceImpl) GetSessionByID

func (s *ServiceImpl) GetSessionByID(ctx context.Context, sessionID string) (*sessions.SessionDO, error)

func (*ServiceImpl) GetUsersByIDs

func (s *ServiceImpl) GetUsersByIDs(ctx context.Context, userIDs []string) ([]*UserResponse, error)

func (*ServiceImpl) Login

func (s *ServiceImpl) Login(ctx context.Context, login Login) (*UserDO, error)

func (*ServiceImpl) Register

func (s *ServiceImpl) Register(ctx context.Context, user *UserDO) (*UserResponse, error)

func (*ServiceImpl) Update

func (s *ServiceImpl) Update(ctx context.Context, user *UserUpdate) (*UserResponse, error)

func (*ServiceImpl) UpdateAvatar

func (s *ServiceImpl) UpdateAvatar(ctx context.Context, data *images.UpdateAvatarMsg) error

func (*ServiceImpl) UpdateUploadedAvatar

func (s *ServiceImpl) UpdateUploadedAvatar(ctx context.Context, delivery amqp.Delivery) error

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

func NewSessionManager

func NewSessionManager(logger logger.Logger, cfg *config.Config, service Service) SessionManager

func (*SessionManager) SessionMiddleware

func (m *SessionManager) SessionMiddleware(next echo.HandlerFunc) echo.HandlerFunc

type UnimplementedUserServiceServer

type UnimplementedUserServiceServer struct {
}

UnimplementedUserServiceServer can be embedded to have forward compatible implementations.

func (*UnimplementedUserServiceServer) GetUserByID

func (*UnimplementedUserServiceServer) GetUsersByIDs

type User

type User struct {
	CreatedAt *timestamp.Timestamp `protobuf:"bytes,7,opt,name=CreatedAt,proto3" json:"CreatedAt,omitempty"`
	UpdatedAt *timestamp.Timestamp `protobuf:"bytes,8,opt,name=UpdatedAt,proto3" json:"UpdatedAt,omitempty"`
	UserID    string               `protobuf:"bytes,1,opt,name=UserID,proto3" json:"UserID,omitempty"`
	FirstName string               `protobuf:"bytes,2,opt,name=FirstName,proto3" json:"FirstName,omitempty"`
	LastName  string               `protobuf:"bytes,3,opt,name=LastName,proto3" json:"LastName,omitempty"`
	Email     string               `protobuf:"bytes,4,opt,name=Email,proto3" json:"Email,omitempty"`
	Avatar    string               `protobuf:"bytes,5,opt,name=Avatar,proto3" json:"Avatar,omitempty"`
	Role      string               `protobuf:"bytes,6,opt,name=Role,proto3" json:"Role,omitempty"`
	// contains filtered or unexported fields
}

func (*User) Descriptor deprecated

func (*User) Descriptor() ([]byte, []int)

Deprecated: Use User.ProtoReflect.Descriptor instead.

func (*User) GetAvatar

func (x *User) GetAvatar() string

func (*User) GetCreatedAt

func (x *User) GetCreatedAt() *timestamp.Timestamp

func (*User) GetEmail

func (x *User) GetEmail() string

func (*User) GetFirstName

func (x *User) GetFirstName() string

func (*User) GetLastName

func (x *User) GetLastName() string

func (*User) GetRole

func (x *User) GetRole() string

func (*User) GetUpdatedAt

func (x *User) GetUpdatedAt() *timestamp.Timestamp

func (*User) GetUserID

func (x *User) GetUserID() string

func (*User) ProtoMessage

func (*User) ProtoMessage()

func (*User) ProtoReflect

func (x *User) ProtoReflect() protoreflect.Message

func (*User) Reset

func (x *User) Reset()

func (*User) String

func (x *User) String() string

type UserDO

type UserDO struct {
	Role      *Role               `json:"role"`
	CreatedAt *time.Time          `json:"created_at"`
	UpdatedAt *time.Time          `json:"updated_at"`
	FirstName string              `json:"first_name" validate:"required,min=3,max=25"`
	LastName  string              `json:"last_name" validate:"required,min=3,max=25"`
	Email     string              `json:"email" validate:"required,email"`
	Password  string              `json:"password" validate:"required,min=6,max=250"`
	Avatar    postgres.NullString `json:"avatar" validate:"max=250" swaggertype:"string"`
	UserID    uuid.UUID           `json:"user_id"`
}

func (*UserDO) ComparePasswords

func (u *UserDO) ComparePasswords(password string) error

Compare user password and payload

func (*UserDO) HashPassword

func (u *UserDO) HashPassword() error

Hash user password with bcrypt

func (*UserDO) PrepareCreate

func (u *UserDO) PrepareCreate() error

Prepare user for register

func (*UserDO) SanitizePassword

func (u *UserDO) SanitizePassword()

Sanitize user password

type UserResponse

type UserResponse struct {
	Role      *Role               `json:"role"`
	CreatedAt *time.Time          `json:"created_at"`
	UpdatedAt *time.Time          `json:"updated_at"`
	FirstName string              `json:"first_name" validate:"required,min=3,max=25"`
	LastName  string              `json:"last_name" validate:"required,min=3,max=25"`
	Email     string              `json:"email" validate:"required,email"`
	Avatar    postgres.NullString `json:"avatar" validate:"max=250" swaggertype:"string"`
	UserID    uuid.UUID           `json:"user_id"`
}

func (*UserResponse) ToProto

func (r *UserResponse) ToProto() *User

type UserServiceClient

type UserServiceClient interface {
	GetUserByID(ctx context.Context, in *GetByIDRequest, opts ...grpc.CallOption) (*GetByIDResponse, error)
	GetUsersByIDs(ctx context.Context, in *GetUsersByIDsRequest, opts ...grpc.CallOption) (*GetUsersByIDsResponse, error)
}

UserServiceClient is the client API for UserService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

type UserServiceServer

type UserServiceServer interface {
	GetUserByID(context.Context, *GetByIDRequest) (*GetByIDResponse, error)
	GetUsersByIDs(context.Context, *GetUsersByIDsRequest) (*GetUsersByIDsResponse, error)
}

UserServiceServer is the server API for UserService service.

type UserUpdate

type UserUpdate struct {
	Role      *Role     `json:"role"`
	FirstName string    `json:"first_name" validate:"omitempty,min=3,max=25" swaggertype:"string"`
	LastName  string    `json:"last_name" validate:"omitempty,min=3,max=25" swaggertype:"string"`
	Email     string    `json:"email" validate:"omitempty,email" swaggertype:"string"`
	Avatar    string    `json:"avatar" validate:"max=250" swaggertype:"string"`
	UserID    uuid.UUID `json:"user_id"`
}

Directories

Path Synopsis
Code generated by swaggo/swag.
Code generated by swaggo/swag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL