service

package
v0.0.0-...-4d5bab8 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyExists = errors.New("record already exists")

ErrAlreadyExists is returned when a record with the same ID already exists in the store

Functions

This section is empty.

Types

type AuthInterceptor

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

func NewAuthInterceptor

func NewAuthInterceptor(jwtManager *JWTManager, accessibleRoles map[string][]string) *AuthInterceptor

NewAuthInterceptor returns a new auth interceptor

func (*AuthInterceptor) Stream

func (interceptor *AuthInterceptor) Stream() grpc.StreamServerInterceptor

Stream returns a server interceptor function to authenticate and authorize stream RPC

func (*AuthInterceptor) Unary

func (interceptor *AuthInterceptor) Unary() grpc.UnaryServerInterceptor

Unary returns a server interceptor function to authenticate and authorize unary RPC

type AuthServer

type AuthServer struct {

	// pb.AuthServiceServer
	// for backward compatibility
	pb.UnimplementedAuthServiceServer
	// contains filtered or unexported fields
}

AuthServer is the server for authentication

func NewAuthServer

func NewAuthServer(userStore UserStore, jwtManager *JWTManager) *AuthServer

NewAuthServer returns a new auth server

func (*AuthServer) Login

func (server *AuthServer) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error)

Login is a unary RPC to login user

type DiskImageStore

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

DiskImageStore images on disk and its info on memory

func NewDiskImageStore

func NewDiskImageStore(imageFolder string) *DiskImageStore

NewDiskImageStore returns a new DiskImageStore

func (*DiskImageStore) Save

func (store *DiskImageStore) Save(
	laptopID string,
	imageType string,
	imageData bytes.Buffer,
) (string, error)

Save saves a new laptop image to the store

type ImageInfo

type ImageInfo struct {
	LaptopID string
	Type     string
	Path     string
}

ImageInfo contains information of the laptop image

type ImageStore

type ImageStore interface {
	// Save saves a new laptop image to the store
	Save(laptopID string, imageType string, imageData bytes.Buffer) (string, error)
}

ImageStore is an interface to store images

type InMemoryLaptopStore

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

InMemoryLaptopStore stores laptop in memory

func NewInMemoryLaptopStore

func NewInMemoryLaptopStore() *InMemoryLaptopStore

NewInMemoryLaptopStore returns a new InMemoryLaptopStore

func (*InMemoryLaptopStore) Find

func (store *InMemoryLaptopStore) Find(id string) (*pb.Laptop, error)

func (*InMemoryLaptopStore) Save

func (store *InMemoryLaptopStore) Save(laptop *pb.Laptop) error

Save saves the laptop to the store

func (*InMemoryLaptopStore) Search

func (store *InMemoryLaptopStore) Search(
	ctx context.Context,
	filter *pb.Filter,
	found func(laptop *pb.Laptop) error,
) error

Search searches for laptops with filter, returns one by one via the found function

type InMemoryRatingStore

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

InMemoryRatingStore stores laptop ratings in memory

func NewInMemoryRatingStore

func NewInMemoryRatingStore() *InMemoryRatingStore

NewInMemoryRatingStore returns a new InMemoryRatingStore

func (*InMemoryRatingStore) Add

func (store *InMemoryRatingStore) Add(laptopID string, score float64) (*Rating, error)

Add adds a new laptop score to the store and returns its rating

type InMemoryUserStore

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

InMemoryUserStore stores users in memory

func NewInMemoryUserStore

func NewInMemoryUserStore() *InMemoryUserStore

NewInMemoryUserStore returns a new in-memory user store

func (*InMemoryUserStore) Find

func (store *InMemoryUserStore) Find(username string) (*User, error)

Find finds a user by username

func (*InMemoryUserStore) Save

func (store *InMemoryUserStore) Save(user *User) error

Save saves a user to the store

type JWTManager

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

JWTManager is a JSON web token manager

func NewJWTManager

func NewJWTManager(secretKey string, tokenDuration time.Duration) *JWTManager

NewJWTManager returns a new JWT manager

func (*JWTManager) Generate

func (manager *JWTManager) Generate(user *User) (string, error)

Generate generates and signs a new token for a user

func (*JWTManager) Verify

func (manager *JWTManager) Verify(accessToken string) (*UserClaims, error)

Verify verifies the access token string and return a user claim if the token is valid

type LaptopServer

type LaptopServer struct {

	// pb.LaptopServiceServer/
	// for backward compatibility
	pb.UnimplementedLaptopServiceServer
	// contains filtered or unexported fields
}

LaptopServer is the server that provides laptop services

func NewLaptopServer

func NewLaptopServer(laptopStore LaptopStore, imageStore ImageStore, ratingStore RatingStore) *LaptopServer

NewLaptopServer returns a new LaptopServer

func (*LaptopServer) CreateLaptop

func (server *LaptopServer) CreateLaptop(
	ctx context.Context,
	req *pb.CreateLaptopRequest,
) (*pb.CreateLaptopResponse, error)

CreateLaptop is a unary RPC to create a new laptop

func (*LaptopServer) RateLaptop

func (server *LaptopServer) RateLaptop(stream pb.LaptopService_RateLaptopServer) error

RateLaptop is a bidirectional-streaming RPC that allows client to rate a stream of laptops with a score, and returns a stream of average score for each of them

func (*LaptopServer) SearchLaptop

func (server *LaptopServer) SearchLaptop(
	req *pb.SearchLaptopRequest,
	stream pb.LaptopService_SearchLaptopServer,
) error

SearchLaptop is a server-streaming RPC to search for laptops

func (*LaptopServer) UploadImage

func (server *LaptopServer) UploadImage(stream pb.LaptopService_UploadImageServer) error

UploadImage is a client-streaming RPC to upload a laptop image

type LaptopStore

type LaptopStore interface {
	// Save saves the laptop to the store
	Save(laptop *pb.Laptop) error
	// Find finds a laptop by ID
	Find(id string) (*pb.Laptop, error)
	// Search searches for laptops with filter, returns one by one via the found function
	Search(ctx context.Context, filter *pb.Filter, found func(laptop *pb.Laptop) error) error
}

LaptopStore is an interface to store laptop

type Rating

type Rating struct {
	Count uint32
	Sum   float64
}

Rating contains the rating information of a laptop

type RatingStore

type RatingStore interface {
	// Add adds a new laptop score to the store and returns its rating
	Add(laptopID string, score float64) (*Rating, error)
}

RatingStore is an interface to store laptop ratings

type User

type User struct {
	Username       string
	HashedPassword string
	Role           string
}

User contains user's information

func NewUser

func NewUser(username string, password string, role string) (*User, error)

NewUser returns a new user

func (*User) Clone

func (user *User) Clone() *User

Clone returns a clone of this user

func (*User) IsCorrectPassword

func (user *User) IsCorrectPassword(password string) bool

IsCorrectPassword if the provided password is correct or not

type UserClaims

type UserClaims struct {
	jwt.StandardClaims
	Username string `json:"username"`
	Role     string `json:"role"`
}

UserClaims is a custom JWT claims that contains some user's information

type UserStore

type UserStore interface {
	// Save saves a user to the store
	Save(user *User) error
	// Find finds a user by username
	Find(username string) (*User, error)
}

UserStore is an interface to store users

Jump to

Keyboard shortcuts

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