chore: update server services

This commit is contained in:
Steven
2023-09-22 07:44:44 +08:00
parent 790a8a2e17
commit 92fba82927
9 changed files with 143 additions and 85 deletions

View File

@ -2,19 +2,22 @@ package v1
import (
"github.com/boojack/slash/server/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store"
"github.com/labstack/echo/v4"
)
type APIV1Service struct {
Profile *profile.Profile
Store *store.Store
Profile *profile.Profile
Store *store.Store
LicenseService *license.LicenseService
}
func NewAPIV1Service(profile *profile.Profile, store *store.Store) *APIV1Service {
func NewAPIV1Service(profile *profile.Profile, store *store.Store, licenseService *license.LicenseService) *APIV1Service {
return &APIV1Service{
Profile: profile,
Store: store,
Profile: profile,
Store: store,
LicenseService: licenseService,
}
}

View File

@ -2,71 +2,36 @@ package v2
import (
"context"
"time"
"github.com/boojack/slash/plugin/license"
apiv2pb "github.com/boojack/slash/proto/gen/api/v2"
storepb "github.com/boojack/slash/proto/gen/store"
"github.com/boojack/slash/server/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
type SubscriptionService struct {
apiv2pb.UnimplementedSubscriptionServiceServer
Profile *profile.Profile
Store *store.Store
Profile *profile.Profile
Store *store.Store
LicenseService *license.LicenseService
}
// NewSubscriptionService creates a new SubscriptionService.
func NewSubscriptionService(profile *profile.Profile, store *store.Store) *SubscriptionService {
func NewSubscriptionService(profile *profile.Profile, store *store.Store, licenseService *license.LicenseService) *SubscriptionService {
return &SubscriptionService{
Profile: profile,
Store: store,
Profile: profile,
Store: store,
LicenseService: licenseService,
}
}
func (s *SubscriptionService) GetSubscription(ctx context.Context, _ *apiv2pb.GetSubscriptionRequest) (*apiv2pb.GetSubscriptionResponse, error) {
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_LICENSE_KEY,
})
subscription, err := s.LicenseService.LoadSubscription(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
subscription := &apiv2pb.Subscription{
Plan: apiv2pb.PlanType_FREE,
}
licenseKey := ""
if workspaceSetting != nil {
licenseKey = workspaceSetting.GetLicenseKey()
}
if licenseKey == "" {
return &apiv2pb.GetSubscriptionResponse{
Subscription: subscription,
}, nil
}
validateResponse, err := license.ValidateLicenseKey(licenseKey, "")
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to validate license key: %v", err)
}
if validateResponse.Valid {
subscription.Plan = apiv2pb.PlanType_PRO
if validateResponse.LicenseKey.ExpiresAt != nil && *validateResponse.LicenseKey.ExpiresAt != "" {
expiresTime, err := time.Parse("2006-01-02 15:04:05", *validateResponse.LicenseKey.ExpiresAt)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse license key expired time: %v", err)
}
subscription.ExpiresTime = timestamppb.New(expiresTime)
}
startedTime, err := time.Parse("2006-01-02 15:04:05", validateResponse.LicenseKey.CreatedAt)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to parse license key created time: %v", err)
}
subscription.StartedTime = timestamppb.New(startedTime)
return nil, status.Errorf(codes.Internal, "failed to load subscription: %v", err)
}
return &apiv2pb.GetSubscriptionResponse{
Subscription: subscription,
@ -74,25 +39,11 @@ func (s *SubscriptionService) GetSubscription(ctx context.Context, _ *apiv2pb.Ge
}
func (s *SubscriptionService) UpdateSubscription(ctx context.Context, request *apiv2pb.UpdateSubscriptionRequest) (*apiv2pb.UpdateSubscriptionResponse, error) {
licenseKey := request.LicenseKey
if licenseKey == "" {
return nil, status.Errorf(codes.InvalidArgument, "license key is required")
}
validateResponse, err := license.ValidateLicenseKey(licenseKey, "")
subscription, err := s.LicenseService.UpdateSubscription(ctx, request.LicenseKey)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to validate license key: %v", err)
return nil, status.Errorf(codes.Internal, "failed to load subscription: %v", err)
}
if !validateResponse.Valid {
return nil, status.Errorf(codes.InvalidArgument, "invalid license key")
}
_, err = s.Store.UpsertWorkspaceSetting(ctx, &storepb.WorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_LICENSE_KEY,
Value: &storepb.WorkspaceSetting_LicenseKey{
LicenseKey: licenseKey,
},
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update license key: %v", err)
}
return &apiv2pb.UpdateSubscriptionResponse{}, nil
return &apiv2pb.UpdateSubscriptionResponse{
Subscription: subscription,
}, nil
}

View File

@ -6,6 +6,7 @@ import (
apiv2pb "github.com/boojack/slash/proto/gen/api/v2"
"github.com/boojack/slash/server/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store"
grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
@ -16,22 +17,23 @@ import (
)
type APIV2Service struct {
Secret string
Profile *profile.Profile
Store *store.Store
Secret string
Profile *profile.Profile
Store *store.Store
LicenseService *license.LicenseService
grpcServer *grpc.Server
grpcServerPort int
}
func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store, grpcServerPort int) *APIV2Service {
func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store, licenseService *license.LicenseService, grpcServerPort int) *APIV2Service {
authProvider := NewGRPCAuthInterceptor(store, secret)
grpcServer := grpc.NewServer(
grpc.ChainUnaryInterceptor(
authProvider.AuthenticationInterceptor,
),
)
apiv2pb.RegisterSubscriptionServiceServer(grpcServer, NewSubscriptionService(profile, store))
apiv2pb.RegisterSubscriptionServiceServer(grpcServer, NewSubscriptionService(profile, store, licenseService))
apiv2pb.RegisterWorkspaceServiceServer(grpcServer, NewWorkspaceService(profile, store))
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(secret, store))
apiv2pb.RegisterUserSettingServiceServer(grpcServer, NewUserSettingService(store))
@ -42,6 +44,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
Secret: secret,
Profile: profile,
Store: store,
LicenseService: licenseService,
grpcServer: grpcServer,
grpcServerPort: grpcServerPort,
}