2021-09-30 10:32:18 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
codequestpb "github.com/hhhapz/codequest/api/v1"
|
|
|
|
"github.com/hhhapz/codequest/models"
|
2021-12-22 14:38:22 +07:00
|
|
|
"github.com/hhhapz/codequest/question"
|
2021-09-30 10:32:18 +07:00
|
|
|
"golang.org/x/oauth2"
|
2021-12-19 15:30:21 +07:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/reflection"
|
2021-09-30 10:32:18 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// inProd tells the server whether it is in prod.
|
|
|
|
// It should be set using ldflags on build time.
|
|
|
|
var inProd bool
|
|
|
|
|
|
|
|
type OAuthStore interface {
|
2021-12-22 14:38:22 +07:00
|
|
|
Create(string) string
|
|
|
|
Validate(context.Context, string, string) (*oauth2.Token, string, error)
|
2021-09-30 10:32:18 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserStore interface {
|
2021-12-22 14:38:22 +07:00
|
|
|
ConsumeToken(context.Context, *oauth2.Token) (*models.Token, error)
|
2021-12-19 15:30:21 +07:00
|
|
|
|
2021-12-22 14:38:22 +07:00
|
|
|
CreateToken(context.Context, *models.User) (*models.Token, error)
|
|
|
|
RevokeToken(context.Context, string) error
|
|
|
|
RevokeUserTokens(context.Context, *models.User) (int64, error)
|
2021-09-30 10:32:18 +07:00
|
|
|
|
2021-12-22 14:38:22 +07:00
|
|
|
User(context.Context, string) (*models.User, error)
|
|
|
|
Users(context.Context) ([]*models.User, error)
|
2021-09-30 10:32:18 +07:00
|
|
|
|
2021-12-22 14:38:22 +07:00
|
|
|
UserByToken(context.Context, string) (*models.User, error)
|
2021-09-30 10:32:18 +07:00
|
|
|
|
2021-12-22 14:38:22 +07:00
|
|
|
UpdateUser(context.Context, *models.User) error
|
|
|
|
DeleteUser(context.Context, *models.User) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuestStore interface {
|
|
|
|
Questions(context.Context, *models.User) ([]*question.Data, error)
|
|
|
|
Question(context.Context, *models.User, string) (*question.Data, error)
|
|
|
|
Submissions(context.Context, *models.User, string, question.Part) (bool, int, error)
|
|
|
|
AddSubmission(context.Context, *models.QuestionAttempt) error
|
2021-09-30 10:32:18 +07:00
|
|
|
}
|
|
|
|
|
2021-12-19 15:30:21 +07:00
|
|
|
type Server struct {
|
|
|
|
*AuthService
|
|
|
|
*UserService
|
2021-12-22 14:38:22 +07:00
|
|
|
*QuestService
|
2021-12-19 15:30:21 +07:00
|
|
|
}
|
2021-09-30 10:32:18 +07:00
|
|
|
|
2021-12-22 14:38:22 +07:00
|
|
|
func NewServer(os OAuthStore, us UserStore, qs QuestStore) (*grpc.Server, error) {
|
2021-12-19 15:30:21 +07:00
|
|
|
s := &Server{
|
2021-09-30 10:32:18 +07:00
|
|
|
AuthService: &AuthService{
|
2021-12-19 15:30:21 +07:00
|
|
|
oauthStore: os,
|
|
|
|
userStore: us,
|
2021-09-30 10:32:18 +07:00
|
|
|
},
|
|
|
|
UserService: &UserService{
|
2021-12-19 15:30:21 +07:00
|
|
|
userStore: us,
|
2021-09-30 10:32:18 +07:00
|
|
|
},
|
2021-12-22 14:38:22 +07:00
|
|
|
QuestService: &QuestService{
|
|
|
|
userStore: us,
|
|
|
|
questStore: qs,
|
|
|
|
},
|
2021-09-30 10:32:18 +07:00
|
|
|
}
|
2021-12-19 15:30:21 +07:00
|
|
|
srv := grpc.NewServer(
|
|
|
|
grpc.UnaryInterceptor(AuthInterceptor(s.AuthService.defaultAuthFunc)),
|
|
|
|
)
|
|
|
|
reflection.Register(srv)
|
|
|
|
codequestpb.RegisterAuthServiceServer(srv, s.AuthService)
|
|
|
|
codequestpb.RegisterUserServiceServer(srv, s.UserService)
|
2021-12-22 14:38:22 +07:00
|
|
|
codequestpb.RegisterQuestServiceServer(srv, s.QuestService)
|
2021-12-19 15:30:21 +07:00
|
|
|
return srv, nil
|
2021-09-30 10:32:18 +07:00
|
|
|
}
|