Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: move some server code to enterprise service (#3670)

Danny Xueb4e78b41583
diff --git a/enterprise/api/license.go b/enterprise/api/license.go
index b387b3dd0f..42e7255fe3 100644
--- a/enterprise/api/license.go
+++ b/enterprise/api/license.go
@@ -2,6 +2,7 @@
package api
import (
+ "context"
"strings"
"time"
@@ -58,7 +59,7 @@ func (l *License) OrgID() string {
// LicenseService is the service for enterprise license.
type LicenseService interface {
// StoreLicense will store license into file.
- StoreLicense(patch *SubscriptionPatch) error
+ StoreLicense(ctx context.Context, patch *SubscriptionPatch) error
// LoadLicense will load license from file and validate it.
- LoadLicense() (*License, error)
+ LoadLicense(ctx context.Context) (*License, error)
}
diff --git a/enterprise/service/license.go b/enterprise/service/license.go
index 161729d4b7..44f1a3d048 100644
--- a/enterprise/service/license.go
+++ b/enterprise/service/license.go
@@ -3,12 +3,16 @@ package service
import (
"context"
+ "encoding/json"
+ "time"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
+ "go.uber.org/zap"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/common/log"
enterpriseAPI "github.com/bytebase/bytebase/enterprise/api"
"github.com/bytebase/bytebase/enterprise/config"
"github.com/bytebase/bytebase/store"
@@ -44,26 +48,66 @@ func NewLicenseService(mode common.ReleaseMode, store *store.Store) (*LicenseSer
}
// StoreLicense will store license into file.
-func (s *LicenseService) StoreLicense(patch *enterpriseAPI.SubscriptionPatch) error {
+func (s *LicenseService) StoreLicense(ctx context.Context, patch *enterpriseAPI.SubscriptionPatch) error {
if patch.License != "" {
if _, err := s.parseLicense(patch.License); err != nil {
return err
}
}
- return s.writeLicense(patch)
+ _, err := s.store.PatchSetting(ctx, &api.SettingPatch{
+ UpdaterID: patch.UpdaterID,
+ Name: api.SettingEnterpriseLicense,
+ Value: patch.License,
+ })
+ return err
}
// LoadLicense will load license from file and validate it.
-func (s *LicenseService) LoadLicense() (*enterpriseAPI.License, error) {
- tokenString, err := s.readLicense()
+func (s *LicenseService) LoadLicense(ctx context.Context) (*enterpriseAPI.License, error) {
+ // Find enterprise license.
+ settingName := api.SettingEnterpriseLicense
+ settings, err := s.store.FindSetting(ctx, &api.SettingFind{
+ Name: &settingName,
+ })
if err != nil {
- return nil, err
+ return nil, errors.Wrapf(err, "failed to load enterprise license from settings")
+ }
+ tokenString := ""
+ if len(settings) > 0 {
+ tokenString = settings[0].Value
}
- if tokenString == "" {
- return nil, common.Errorf(common.NotFound, "cannot find license")
+ if tokenString != "" {
+ license, err := s.parseLicense(tokenString)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse enterprise license")
+ }
+ if license != nil {
+ log.Debug(
+ "Load valid license",
+ zap.String("plan", license.Plan.String()),
+ zap.Time("expiresAt", time.Unix(license.ExpiresTs, 0)),
+ zap.Int("instanceCount", license.InstanceCount),
+ )
+ return license, nil
+ }
}
- return s.parseLicense(tokenString)
+ // Find free trial license.
+ settingName = api.SettingEnterpriseTrial
+ settings, err = s.store.FindSetting(ctx, &api.SettingFind{
+ Name: &settingName,
+ })
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to load trial license from settings")
+ }
+ if len(settings) == 0 {
+ return nil, common.Wrapf(err, common.NotFound, "cannot find license")
+ }
+ var data enterpriseAPI.License
+ if err := json.Unmarshal([]byte(settings[0].Value), &data); err != nil {
+ return nil, errors.Wrapf(err, "failed to parse trial license")
+ }
+ return &data, nil
}
func (s *LicenseService) parseLicense(license string) (*enterpriseAPI.License, error) {
@@ -140,30 +184,6 @@ func (s *LicenseService) parseClaims(claims *Claims) (*enterpriseAPI.License, er
return license, nil
}
-func (s *LicenseService) readLicense() (string, error) {
- settingName := api.SettingEnterpriseLicense
- ctx := context.Background()
- settings, err := s.store.FindSetting(ctx, &api.SettingFind{
- Name: &settingName,
- })
-
- if len(settings) == 0 {
- return "", common.Wrapf(err, common.NotFound, "cannot find license")
- }
-
- return settings[0].Value, nil
-}
-
-func (s *LicenseService) writeLicense(patch *enterpriseAPI.SubscriptionPatch) error {
- ctx := context.Background()
- _, err := s.store.PatchSetting(ctx, &api.SettingPatch{
- UpdaterID: patch.UpdaterID,
- Name: api.SettingEnterpriseLicense,
- Value: patch.License,
- })
- return err
-}
-
func convertPlanType(candidate string) (api.PlanType, error) {
switch candidate {
case api.TEAM.String():
diff --git a/server/subscription.go b/server/subscription.go
index 1d93fe8c22..e6a27b7f7a 100644
--- a/server/subscription.go
+++ b/server/subscription.go
@@ -9,12 +9,9 @@ import (
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
- "github.com/pkg/errors"
- "go.uber.org/zap"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
enterpriseAPI "github.com/bytebase/bytebase/enterprise/api"
metricAPI "github.com/bytebase/bytebase/metric"
"github.com/bytebase/bytebase/plugin/metric"
@@ -31,19 +28,19 @@ func (s *Server) registerSubscriptionRoutes(g *echo.Group) {
g.PATCH("/subscription", func(c echo.Context) error {
patch := &enterpriseAPI.SubscriptionPatch{}
+ ctx := c.Request().Context()
if err := jsonapi.UnmarshalPayload(c.Request().Body, patch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformed create subscription request").SetInternal(err)
}
patch.UpdaterID = c.Get(getPrincipalIDContextKey()).(int)
- if err := s.LicenseService.StoreLicense(patch); err != nil {
+ if err := s.LicenseService.StoreLicense(ctx, patch); err != nil {
if common.ErrorCode(err) == common.Invalid {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to store license").SetInternal(err)
}
- ctx := c.Request().Context()
s.subscription = s.loadSubscription(ctx)
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
@@ -161,7 +158,7 @@ func (s *Server) loadSubscription(ctx context.Context) enterpriseAPI.Subscriptio
InstanceCount: 5,
}
- license, _ := s.loadLicense(ctx)
+ license, _ := s.LicenseService.LoadLicense(ctx)
if license != nil {
subscription = enterpriseAPI.Subscription{
Plan: license.Plan,
@@ -177,47 +174,6 @@ func (s *Server) loadSubscription(ctx context.Context) enterpriseAPI.Subscriptio
return subscription
}
-// loadLicense will get and parse valid license from file.
-func (s *Server) loadLicense(ctx context.Context) (*enterpriseAPI.License, error) {
- license, err := s.LicenseService.LoadLicense()
- if license != nil {
- log.Info(
- "Load valid license",
- zap.String("plan", license.Plan.String()),
- zap.Time("expiresAt", time.Unix(license.ExpiresTs, 0)),
- zap.Int("instanceCount", license.InstanceCount),
- )
-
- return license, nil
- }
-
- if common.ErrorCode(err) == common.NotFound {
- log.Debug("Failed to find license", zap.String("error", err.Error()))
- } else {
- log.Warn("Failed to load valid license", zap.String("error", err.Error()))
- }
-
- // find free trial license in console
- settingName := api.SettingEnterpriseTrial
- settings, err := s.store.FindSetting(ctx, &api.SettingFind{
- Name: &settingName,
- })
- if err != nil {
- log.Warn("Failed to load trial license from settings", zap.String("error", err.Error()))
- return nil, err
- }
- if len(settings) == 0 {
- return nil, common.Wrapf(err, common.NotFound, "cannot find license")
- }
-
- var data enterpriseAPI.License
- if err := json.Unmarshal([]byte(settings[0].Value), &data); err != nil {
- return nil, errors.Wrapf(err, "failed to unmarshal value %q", settings[0].Value)
- }
-
- return &data, nil
-}
-
func (s *Server) feature(feature api.FeatureType) bool {
return api.Feature(feature, s.getEffectivePlan())
}