api

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2021 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	EnvProduction = "production"
	EnvTest       = "test"
	EnvDev        = "development"
)

ENV values accepted

View Source
var (
	Color_name = map[int32]string{
		0: "BLACK",
		1: "WHITE",
	}
	Color_value = map[string]int32{
		"BLACK": 0,
		"WHITE": 1,
	}
)

Enum value maps for Color.

View Source
var ChessService_ServiceDesc = grpc.ServiceDesc{
	ServiceName: "ChessService",
	HandlerType: (*ChessServiceServer)(nil),
	Methods: []grpc.MethodDesc{
		{
			MethodName: "StartGame",
			Handler:    _ChessService_StartGame_Handler,
		},
		{
			MethodName: "JoinGame",
			Handler:    _ChessService_JoinGame_Handler,
		},
		{
			MethodName: "Move",
			Handler:    _ChessService_Move_Handler,
		},
	},
	Streams: []grpc.StreamDesc{
		{
			StreamName:    "Watch",
			Handler:       _ChessService_Watch_Handler,
			ServerStreams: true,
		},
	},
	Metadata: "api/service.proto",
}

ChessService_ServiceDesc is the grpc.ServiceDesc for ChessService service. It's only intended for direct use with grpc.RegisterService, and not to be introspected or modified (even as a copy)

View Source
var DBConn *gorm.DB

DBConn stores DB Connection

View Source
var File_api_service_proto protoreflect.FileDescriptor
View Source
var HTTPRouter *mux.Router

HTTPRouter Mux router for HTTP server

MessageBroker a pub/sub messagre broker system to send updates on watchers

Functions

func EnsureValidToken

func EnsureValidToken(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

EnsureValidToken ensures a valid token exists within a request's metadata. If the token is missing or invalid, the interceptor blocks execution of the handler and returns an error. Otherwise, the interceptor invokes the unary handler.

func GetAccessTokenFromCtx

func GetAccessTokenFromCtx(ctx context.Context) (string, error)

GetAccessTokenFromCtx returns access token from ctx object metadata

func InitDbConn

func InitDbConn(host, port, user, password, dbname string) (*gorm.DB, error)

InitDbConn initialize database connection to postgresql

func InitHTTPRouter

func InitHTTPRouter(c ConfigHTTPRouter)

InitHTTPRouter initializes HTTPHandler, create mux Handler and define HTTTP routes

func Migrate

func Migrate() error

Migrate runs migrations

func RegisterChessServiceServer

func RegisterChessServiceServer(s grpc.ServiceRegistrar, srv ChessServiceServer)

Types

type ChessServiceClient

type ChessServiceClient interface {
	StartGame(ctx context.Context, in *StartGameRequest, opts ...grpc.CallOption) (*StartGameResponse, error)
	JoinGame(ctx context.Context, in *JoinGameRequest, opts ...grpc.CallOption) (*JoinGameResponse, error)
	Move(ctx context.Context, in *MoveRequest, opts ...grpc.CallOption) (*MoveResponse, error)
	Watch(ctx context.Context, in *WatchRequest, opts ...grpc.CallOption) (ChessService_WatchClient, error)
}

ChessServiceClient is the client API for ChessService service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.

type ChessServiceServer

type ChessServiceServer interface {
	StartGame(context.Context, *StartGameRequest) (*StartGameResponse, error)
	JoinGame(context.Context, *JoinGameRequest) (*JoinGameResponse, error)
	Move(context.Context, *MoveRequest) (*MoveResponse, error)
	Watch(*WatchRequest, ChessService_WatchServer) error
	// contains filtered or unexported methods
}

ChessServiceServer is the server API for ChessService service. All implementations must embed UnimplementedChessServiceServer for forward compatibility

type ChessService_WatchClient

type ChessService_WatchClient interface {
	Recv() (*WatchResponse, error)
	grpc.ClientStream
}

type ChessService_WatchServer

type ChessService_WatchServer interface {
	Send(*WatchResponse) error
	grpc.ServerStream
}

type Color

type Color int32
const (
	Color_BLACK Color = 0
	Color_WHITE Color = 1
)

func (Color) Descriptor

func (Color) Descriptor() protoreflect.EnumDescriptor

func (Color) Enum

func (x Color) Enum() *Color

func (Color) EnumDescriptor deprecated

func (Color) EnumDescriptor() ([]byte, []int)

Deprecated: Use Color.Descriptor instead.

func (Color) Number

func (x Color) Number() protoreflect.EnumNumber

func (Color) String

func (x Color) String() string

func (Color) Type

func (Color) Type() protoreflect.EnumType

type ConfigHTTPRouter

type ConfigHTTPRouter struct {
	URLLoc       url.URL
	GithubKey    string
	GithubSecret string
	// SessionKey Ensure your key is sufficiently random - i.e. use Go's
	// crypto/rand or securecookie.GenerateRandomKey(32) and persist the result.
	SessionKey string

	// Env environment
	Env string
}

ConfigHTTPRouter HTTP router configuration

type Game

type Game struct {
	gorm.Model
	Name          string
	UUID          uuid.UUID `gorm:"type:uuid;not null;default:uuid_generate_v1()"`
	WhitePlayer   Player
	WhitePlayerID sql.NullInt32
	BlackPlayer   Player
	BlackPlayerID sql.NullInt32
	Turn          uint
	Winner        int
	Movements     []Movement // TODO: this will cause problems, implement when its needed by the engine
	WhitePieces   pieces     `gorm:"type:jsonb;not null"`
	BlackPieces   pieces     `gorm:"type:jsonb;not null"`
	BoardSquares  Squares    `gorm:"type:jsonb;not null"`
}

Game Model

func (*Game) AfterSave

func (g *Game) AfterSave(tx *gorm.DB) (err error)

AfterSave ...

type JoinGameRequest

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

func (*JoinGameRequest) Descriptor deprecated

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

Deprecated: Use JoinGameRequest.ProtoReflect.Descriptor instead.

func (*JoinGameRequest) GetUuid

func (x *JoinGameRequest) GetUuid() string

func (*JoinGameRequest) ProtoMessage

func (*JoinGameRequest) ProtoMessage()

func (*JoinGameRequest) ProtoReflect

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

func (*JoinGameRequest) Reset

func (x *JoinGameRequest) Reset()

func (*JoinGameRequest) String

func (x *JoinGameRequest) String() string

type JoinGameResponse

type JoinGameResponse struct {
	Uuid  string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
	Name  string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
	Color Color  `protobuf:"varint,3,opt,name=color,proto3,enum=Color" json:"color,omitempty"`
	// contains filtered or unexported fields
}

func (*JoinGameResponse) Descriptor deprecated

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

Deprecated: Use JoinGameResponse.ProtoReflect.Descriptor instead.

func (*JoinGameResponse) GetColor

func (x *JoinGameResponse) GetColor() Color

func (*JoinGameResponse) GetName

func (x *JoinGameResponse) GetName() string

func (*JoinGameResponse) GetUuid

func (x *JoinGameResponse) GetUuid() string

func (*JoinGameResponse) ProtoMessage

func (*JoinGameResponse) ProtoMessage()

func (*JoinGameResponse) ProtoReflect

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

func (*JoinGameResponse) Reset

func (x *JoinGameResponse) Reset()

func (*JoinGameResponse) String

func (x *JoinGameResponse) String() string

type MoveRequest

type MoveRequest struct {
	Uuid       string `protobuf:"bytes,1,opt,name=uuid,proto3" json:"uuid,omitempty"`
	Color      Color  `protobuf:"varint,2,opt,name=color,proto3,enum=Color" json:"color,omitempty"`
	FromSquare string `protobuf:"bytes,3,opt,name=from_square,json=fromSquare,proto3" json:"from_square,omitempty"`
	ToSquare   string `protobuf:"bytes,4,opt,name=to_square,json=toSquare,proto3" json:"to_square,omitempty"`
	// contains filtered or unexported fields
}

func (*MoveRequest) Descriptor deprecated

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

Deprecated: Use MoveRequest.ProtoReflect.Descriptor instead.

func (*MoveRequest) GetColor

func (x *MoveRequest) GetColor() Color

func (*MoveRequest) GetFromSquare

func (x *MoveRequest) GetFromSquare() string

func (*MoveRequest) GetToSquare

func (x *MoveRequest) GetToSquare() string

func (*MoveRequest) GetUuid

func (x *MoveRequest) GetUuid() string

func (*MoveRequest) ProtoMessage

func (*MoveRequest) ProtoMessage()

func (*MoveRequest) ProtoReflect

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

func (*MoveRequest) Reset

func (x *MoveRequest) Reset()

func (*MoveRequest) String

func (x *MoveRequest) String() string

type MoveResponse

type MoveResponse struct {
	StatusCode string `protobuf:"bytes,1,opt,name=status_code,json=statusCode,proto3" json:"status_code,omitempty"`
	Error      string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
	Board      string `protobuf:"bytes,3,opt,name=board,proto3" json:"board,omitempty"`
	// contains filtered or unexported fields
}

func (*MoveResponse) Descriptor deprecated

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

Deprecated: Use MoveResponse.ProtoReflect.Descriptor instead.

func (*MoveResponse) GetBoard

func (x *MoveResponse) GetBoard() string

func (*MoveResponse) GetError

func (x *MoveResponse) GetError() string

func (*MoveResponse) GetStatusCode

func (x *MoveResponse) GetStatusCode() string

func (*MoveResponse) ProtoMessage

func (*MoveResponse) ProtoMessage()

func (*MoveResponse) ProtoReflect

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

func (*MoveResponse) Reset

func (x *MoveResponse) Reset()

func (*MoveResponse) String

func (x *MoveResponse) String() string

type Movement

type Movement struct {
	gorm.Model
	PieceMoved int
	PieceEaten int
	From       string
	To         string
	Player     Player
	PlayerID   uint
	Game       Game
	GameID     uint
}

Movement Model

type Piece

type Piece struct {
	PieceIdentifier engine.PieceIdentifier
	Color           engine.Color
}

Piece piece

type Player

type Player struct {
	gorm.Model
	Color  string
	User   User
	UserID uint
}

Player Model

type Server

type Server struct {
	UnimplementedChessServiceServer
	Db *gorm.DB
}

Server grpc server interface implementation

func (*Server) JoinGame

func (s *Server) JoinGame(ctx context.Context, r *JoinGameRequest) (*JoinGameResponse, error)

JoinGame joins a game, depending of the white or black player space left, it will be assigned to the user joining

func (*Server) Move

func (s *Server) Move(ctx context.Context, r *MoveRequest) (*MoveResponse, error)

Move Moves a player piece

func (*Server) StartGame

func (s *Server) StartGame(ctx context.Context, startGameRequest *StartGameRequest) (*StartGameResponse, error)

StartGame starts a new game

func (*Server) Watch

func (s *Server) Watch(r *WatchRequest, stream ChessService_WatchServer) error

Watch receive live updates on the current game loaded

type Square

type Square struct {
	engine.Square
	Piece Piece
}

Square square

type Squares

type Squares map[engine.SquareIdentifier]Square

Squares Game squares stored in Model

func (*Squares) Scan

func (s *Squares) Scan(value interface{}) error

Scan implement scanner intarface

type StartGameRequest

type StartGameRequest struct {
	Name  string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Color Color  `protobuf:"varint,2,opt,name=color,proto3,enum=Color" json:"color,omitempty"`
	// contains filtered or unexported fields
}

func (*StartGameRequest) Descriptor deprecated

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

Deprecated: Use StartGameRequest.ProtoReflect.Descriptor instead.

func (*StartGameRequest) GetColor

func (x *StartGameRequest) GetColor() Color

func (*StartGameRequest) GetName

func (x *StartGameRequest) GetName() string

func (*StartGameRequest) ProtoMessage

func (*StartGameRequest) ProtoMessage()

func (*StartGameRequest) ProtoReflect

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

func (*StartGameRequest) Reset

func (x *StartGameRequest) Reset()

func (*StartGameRequest) String

func (x *StartGameRequest) String() string

type StartGameResponse

type StartGameResponse struct {
	Uuid string `protobuf:"bytes,2,opt,name=uuid,proto3" json:"uuid,omitempty"`
	// contains filtered or unexported fields
}

func (*StartGameResponse) Descriptor deprecated

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

Deprecated: Use StartGameResponse.ProtoReflect.Descriptor instead.

func (*StartGameResponse) GetUuid

func (x *StartGameResponse) GetUuid() string

func (*StartGameResponse) ProtoMessage

func (*StartGameResponse) ProtoMessage()

func (*StartGameResponse) ProtoReflect

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

func (*StartGameResponse) Reset

func (x *StartGameResponse) Reset()

func (*StartGameResponse) String

func (x *StartGameResponse) String() string

type UnimplementedChessServiceServer

type UnimplementedChessServiceServer struct {
}

UnimplementedChessServiceServer must be embedded to have forward compatible implementations.

func (UnimplementedChessServiceServer) JoinGame

func (UnimplementedChessServiceServer) Move

func (UnimplementedChessServiceServer) StartGame

func (UnimplementedChessServiceServer) Watch

type UnsafeChessServiceServer

type UnsafeChessServiceServer interface {
	// contains filtered or unexported methods
}

UnsafeChessServiceServer may be embedded to opt out of forward compatibility for this service. Use of this interface is not recommended, as added methods to ChessServiceServer will result in compilation errors.

type User

type User struct {
	gorm.Model
	Email     string `gorm:"unique"`
	Name      string
	FirstName string
	LastName  string
	NickName  string
	UserID    string
	// TODO: hash AccessToken, AccessTokenSecret, RefreshToken
	AccessToken       string
	AccessTokenSecret string
	RefreshToken      string
	ExpiresAt         sql.NullTime
	IDToken           string
}

User Model

func GetUserFromAccessToken

func GetUserFromAccessToken(accessToken string) User

GetUserFromAccessToken returns user from database with accesstoken set

type WatchRequest

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

func (*WatchRequest) Descriptor deprecated

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

Deprecated: Use WatchRequest.ProtoReflect.Descriptor instead.

func (*WatchRequest) GetUuid

func (x *WatchRequest) GetUuid() string

func (*WatchRequest) ProtoMessage

func (*WatchRequest) ProtoMessage()

func (*WatchRequest) ProtoReflect

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

func (*WatchRequest) Reset

func (x *WatchRequest) Reset()

func (*WatchRequest) String

func (x *WatchRequest) String() string

type WatchResponse

type WatchResponse struct {
	Turn   string `protobuf:"bytes,1,opt,name=turn,proto3" json:"turn,omitempty"`
	Board  string `protobuf:"bytes,2,opt,name=board,proto3" json:"board,omitempty"`
	Status string `protobuf:"bytes,3,opt,name=status,proto3" json:"status,omitempty"`
	// contains filtered or unexported fields
}

func (*WatchResponse) Descriptor deprecated

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

Deprecated: Use WatchResponse.ProtoReflect.Descriptor instead.

func (*WatchResponse) GetBoard

func (x *WatchResponse) GetBoard() string

func (*WatchResponse) GetStatus

func (x *WatchResponse) GetStatus() string

func (*WatchResponse) GetTurn

func (x *WatchResponse) GetTurn() string

func (*WatchResponse) ProtoMessage

func (*WatchResponse) ProtoMessage()

func (*WatchResponse) ProtoReflect

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

func (*WatchResponse) Reset

func (x *WatchResponse) Reset()

func (*WatchResponse) String

func (x *WatchResponse) String() string

Jump to

Keyboard shortcuts

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