hackathon/api/api.go

73 lines
1.8 KiB
Go

package api
import (
"context"
codequestpb "github.com/hhhapz/codequest/api/v1"
"github.com/hhhapz/codequest/models"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
)
// inProd tells the server whether it is in prod.
// It should be set using ldflags on build time.
var inProd bool
type OAuthStore interface {
Create(callback string) (code string)
Validate(ctx context.Context, state, code string) (*oauth2.Token, string, error)
}
type UserStore interface {
ConsumeToken(ctx context.Context, token *oauth2.Token) (*models.Token, error)
CreateToken(ctx context.Context, user *models.User) (*models.Token, error)
RevokeToken(ctx context.Context, token string) error
RevokeUserTokens(ctx context.Context, user *models.User) (int64, error)
User(ctx context.Context, email string) (*models.User, error)
Users(ctx context.Context) ([]*models.User, error)
UserByToken(ctx context.Context, token string) (*models.User, error)
UpdateUser(ctx context.Context, user *models.User) error
DeleteUser(ctx context.Context, user *models.User) error
}
type Server struct {
*AuthService
*UserService
}
func NewServer(os OAuthStore, us UserStore) (*grpc.Server, error) {
s := &Server{
AuthService: &AuthService{
oauthStore: os,
userStore: us,
},
UserService: &UserService{
userStore: us,
},
}
srv := grpc.NewServer(
grpc.UnaryInterceptor(AuthInterceptor(s.AuthService.defaultAuthFunc)),
)
reflection.Register(srv)
codequestpb.RegisterAuthServiceServer(srv, s.AuthService)
codequestpb.RegisterUserServiceServer(srv, s.UserService)
return srv, nil
}
// List of commonly used error values
var (
ErrInvalidArgument = grpc.Errorf(
codes.InvalidArgument,
"Invalid argument",
)
)
func err() {
}