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

@ -51,6 +51,9 @@ linters-settings:
disabled: true disabled: true
- name: early-return - name: early-return
disabled: true disabled: true
- name: exported
arguments:
- "disableStutteringCheck"
gocritic: gocritic:
disabled-checks: disabled-checks:
- ifElseChain - ifElseChain

View File

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

View File

@ -2,71 +2,36 @@ package v2
import ( import (
"context" "context"
"time"
"github.com/boojack/slash/plugin/license"
apiv2pb "github.com/boojack/slash/proto/gen/api/v2" 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/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store" "github.com/boojack/slash/store"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
) )
type SubscriptionService struct { type SubscriptionService struct {
apiv2pb.UnimplementedSubscriptionServiceServer apiv2pb.UnimplementedSubscriptionServiceServer
Profile *profile.Profile Profile *profile.Profile
Store *store.Store Store *store.Store
LicenseService *license.LicenseService
} }
// NewSubscriptionService creates a new SubscriptionService. // 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{ return &SubscriptionService{
Profile: profile, Profile: profile,
Store: store, Store: store,
LicenseService: licenseService,
} }
} }
func (s *SubscriptionService) GetSubscription(ctx context.Context, _ *apiv2pb.GetSubscriptionRequest) (*apiv2pb.GetSubscriptionResponse, error) { func (s *SubscriptionService) GetSubscription(ctx context.Context, _ *apiv2pb.GetSubscriptionRequest) (*apiv2pb.GetSubscriptionResponse, error) {
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ subscription, err := s.LicenseService.LoadSubscription(ctx)
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_LICENSE_KEY,
})
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err) return nil, status.Errorf(codes.Internal, "failed to load subscription: %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 &apiv2pb.GetSubscriptionResponse{ return &apiv2pb.GetSubscriptionResponse{
Subscription: subscription, 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) { func (s *SubscriptionService) UpdateSubscription(ctx context.Context, request *apiv2pb.UpdateSubscriptionRequest) (*apiv2pb.UpdateSubscriptionResponse, error) {
licenseKey := request.LicenseKey subscription, err := s.LicenseService.UpdateSubscription(ctx, request.LicenseKey)
if licenseKey == "" {
return nil, status.Errorf(codes.InvalidArgument, "license key is required")
}
validateResponse, err := license.ValidateLicenseKey(licenseKey, "")
if err != nil { 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 &apiv2pb.UpdateSubscriptionResponse{
return nil, status.Errorf(codes.InvalidArgument, "invalid license key") Subscription: subscription,
} }, nil
_, 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
} }

View File

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

View File

@ -12,6 +12,7 @@ import (
apiv2 "github.com/boojack/slash/api/v2" apiv2 "github.com/boojack/slash/api/v2"
storepb "github.com/boojack/slash/proto/gen/store" storepb "github.com/boojack/slash/proto/gen/store"
"github.com/boojack/slash/server/profile" "github.com/boojack/slash/server/profile"
"github.com/boojack/slash/server/service/license"
"github.com/boojack/slash/store" "github.com/boojack/slash/store"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -24,6 +25,8 @@ type Server struct {
Profile *profile.Profile Profile *profile.Profile
Store *store.Store Store *store.Store
licenseService *license.LicenseService
// API services. // API services.
apiV2Service *apiv2.APIV2Service apiV2Service *apiv2.APIV2Service
} }
@ -34,10 +37,13 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
e.HideBanner = true e.HideBanner = true
e.HidePort = true e.HidePort = true
licenseService := license.NewLicenseService(profile, store)
s := &Server{ s := &Server{
e: e, e: e,
Profile: profile, Profile: profile,
Store: store, Store: store,
licenseService: licenseService,
} }
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
@ -90,10 +96,10 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
rootGroup := e.Group("") rootGroup := e.Group("")
// Register API v1 routes. // Register API v1 routes.
apiV1Service := apiv1.NewAPIV1Service(profile, store) apiV1Service := apiv1.NewAPIV1Service(profile, store, licenseService)
apiV1Service.Start(rootGroup, secret) apiV1Service.Start(rootGroup, secret)
s.apiV2Service = apiv2.NewAPIV2Service(secret, profile, store, s.Profile.Port+1) s.apiV2Service = apiv2.NewAPIV2Service(secret, profile, store, licenseService, s.Profile.Port+1)
// Register gRPC gateway as api v2. // Register gRPC gateway as api v2.
if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil { if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil {
return nil, fmt.Errorf("failed to register gRPC gateway: %w", err) return nil, fmt.Errorf("failed to register gRPC gateway: %w", err)

View File

@ -0,0 +1,94 @@
package license
import (
"context"
"time"
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/store"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
)
type LicenseService struct {
Profile *profile.Profile
Store *store.Store
CachedSubscription *apiv2pb.Subscription
}
// NewLicenseService creates a new LicenseService.
func NewLicenseService(profile *profile.Profile, store *store.Store) *LicenseService {
return &LicenseService{
Profile: profile,
Store: store,
CachedSubscription: &apiv2pb.Subscription{
Plan: apiv2pb.PlanType_FREE,
},
}
}
func (s *LicenseService) LoadSubscription(ctx context.Context) (*apiv2pb.Subscription, error) {
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_LICENSE_KEY,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get workspace setting")
}
subscription := &apiv2pb.Subscription{
Plan: apiv2pb.PlanType_FREE,
}
licenseKey := ""
if workspaceSetting != nil {
licenseKey = workspaceSetting.GetLicenseKey()
}
if licenseKey == "" {
return subscription, nil
}
validateResponse, err := validateLicenseKey(licenseKey, "")
if err != nil {
return nil, errors.Wrap(err, "failed to validate license key")
}
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, errors.Wrap(err, "failed to parse license key expires time")
}
subscription.ExpiresTime = timestamppb.New(expiresTime)
}
startedTime, err := time.Parse("2006-01-02 15:04:05", validateResponse.LicenseKey.CreatedAt)
if err != nil {
return nil, errors.Wrap(err, "failed to parse license key created time")
}
subscription.StartedTime = timestamppb.New(startedTime)
}
s.CachedSubscription = subscription
return subscription, nil
}
func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey string) (*apiv2pb.Subscription, error) {
if licenseKey == "" {
return nil, errors.New("license key is required")
}
validateResponse, err := validateLicenseKey(licenseKey, "")
if err != nil {
return nil, errors.Wrap(err, "failed to validate license key")
}
if !validateResponse.Valid {
return nil, errors.New("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, errors.Wrap(err, "failed to upsert workspace setting")
}
return s.LoadSubscription(ctx)
}

View File

@ -22,7 +22,6 @@ const (
subscriptionProProductID = 98995 subscriptionProProductID = 98995
) )
//revive:disable-next-line
type LicenseKey struct { type LicenseKey struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Status string `json:"status"` Status string `json:"status"`
@ -31,7 +30,6 @@ type LicenseKey struct {
ExpiresAt *string `json:"updated_at"` ExpiresAt *string `json:"updated_at"`
} }
//revive:disable-next-line
type LicenseKeyMeta struct { type LicenseKeyMeta struct {
StoreID int32 `json:"store_id"` StoreID int32 `json:"store_id"`
OrderID int32 `json:"order_id"` OrderID int32 `json:"order_id"`
@ -59,7 +57,7 @@ type ActiveLicenseKeyResponse struct {
Meta *LicenseKeyMeta `json:"meta"` Meta *LicenseKeyMeta `json:"meta"`
} }
func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) { func validateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) {
data := map[string]string{"license_key": licenseKey} data := map[string]string{"license_key": licenseKey}
if instanceName != "" { if instanceName != "" {
data["instance_name"] = instanceName data["instance_name"] = instanceName
@ -104,7 +102,7 @@ func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicens
return &response, nil return &response, nil
} }
func ActiveLicenseKey(licenseKey string, instanceName string) (*ActiveLicenseKeyResponse, error) { func activeLicenseKey(licenseKey string, instanceName string) (*ActiveLicenseKeyResponse, error) {
data := map[string]string{"license_key": licenseKey, "instance_name": instanceName} data := map[string]string{"license_key": licenseKey, "instance_name": instanceName}
payload, err := json.Marshal(data) payload, err := json.Marshal(data)
if err != nil { if err != nil {

View File

@ -29,7 +29,7 @@ func TestValidateLicenseKey(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
response, err := ValidateLicenseKey(tt.key, "test-instance") response, err := validateLicenseKey(tt.key, "test-instance")
if tt.err != nil { if tt.err != nil {
require.EqualError(t, err, tt.err.Error()) require.EqualError(t, err, tt.err.Error())
return return
@ -60,7 +60,7 @@ func TestActiveLicenseKey(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
response, err := ActiveLicenseKey(tt.key, "test-instance") response, err := activeLicenseKey(tt.key, "test-instance")
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tt.expected, response.Activated) require.Equal(t, tt.expected, response.Activated)
}) })