refactor: update api version

This commit is contained in:
Steven
2024-02-19 20:45:54 +08:00
parent b5f5ae2483
commit fafacc92eb
103 changed files with 2140 additions and 3986 deletions

View File

@ -39,7 +39,7 @@ func (s *FrontendService) Serve(ctx context.Context, e *echo.Echo) {
HTML5: true,
Root: "dist",
Skipper: func(c echo.Context) bool {
return util.HasPrefixes(c.Path(), "/api", "/slash.api.v2", "/robots.txt", "/sitemap.xml", "/s/:shortcutName")
return util.HasPrefixes(c.Path(), "/api", "/slash.api.v1", "/robots.txt", "/sitemap.xml", "/s/:shortcutName")
},
}))

View File

@ -6,7 +6,6 @@ import (
"log/slog"
"net"
"net/http"
"strings"
"time"
"github.com/google/uuid"
@ -16,7 +15,6 @@ import (
"go.uber.org/zap"
apiv1 "github.com/yourselfhosted/slash/api/v1"
apiv2 "github.com/yourselfhosted/slash/api/v2"
"github.com/yourselfhosted/slash/internal/log"
storepb "github.com/yourselfhosted/slash/proto/gen/store"
"github.com/yourselfhosted/slash/server/metric"
@ -36,7 +34,7 @@ type Server struct {
licenseService *license.LicenseService
// API services.
apiV2Service *apiv2.APIV2Service
apiV2Service *apiv1.APIV2Service
}
func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store) (*Server, error) {
@ -60,34 +58,6 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
`"status":${status},"error":"${error}"}` + "\n",
}))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
Skipper: grpcRequestSkipper,
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
}))
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Skipper: grpcRequestSkipper,
Timeout: 30 * time.Second,
}))
e.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
Skipper: grpcRequestSkipper,
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
middleware.RateLimiterMemoryStoreConfig{Rate: 30, Burst: 60, ExpiresIn: 3 * time.Minute},
),
IdentifierExtractor: func(ctx echo.Context) (string, error) {
id := ctx.RealIP()
return id, nil
},
ErrorHandler: func(context echo.Context, err error) error {
return context.JSON(http.StatusForbidden, nil)
},
DenyHandler: func(context echo.Context, identifier string, err error) error {
return context.JSON(http.StatusTooManyRequests, nil)
},
}))
// Serve frontend.
frontendService := NewFrontendService(profile, store)
frontendService.Serve(ctx, e)
@ -109,12 +79,8 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
})
rootGroup := e.Group("")
// Register API v1 routes.
apiV1Service := apiv1.NewAPIV1Service(profile, store, licenseService)
apiV1Service.Start(rootGroup, secret)
s.apiV2Service = apiv2.NewAPIV2Service(secret, profile, store, licenseService, s.Profile.Port+1)
// Register gRPC gateway as api v2.
s.apiV2Service = apiv1.NewAPIV2Service(secret, profile, store, licenseService, s.Profile.Port+1)
// Register gRPC gateway as api v1.
if err := s.apiV2Service.RegisterGateway(ctx, e); err != nil {
return nil, errors.Wrap(err, "failed to register gRPC gateway")
}
@ -167,10 +133,6 @@ func (s *Server) GetEcho() *echo.Echo {
return s.e
}
func grpcRequestSkipper(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/slash.api.v2.")
}
func (s *Server) getSecretSessionName(ctx context.Context) (string, error) {
secretSessionSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_SECRET_SESSION,

View File

@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
apiv2pb "github.com/yourselfhosted/slash/proto/gen/api/v2"
apiv1pb "github.com/yourselfhosted/slash/proto/gen/api/v1"
storepb "github.com/yourselfhosted/slash/proto/gen/store"
"github.com/yourselfhosted/slash/server/profile"
"github.com/yourselfhosted/slash/store"
@ -17,7 +17,7 @@ type LicenseService struct {
Profile *profile.Profile
Store *store.Store
cachedSubscription *apiv2pb.Subscription
cachedSubscription *apiv1pb.Subscription
}
// NewLicenseService creates a new LicenseService.
@ -25,21 +25,21 @@ func NewLicenseService(profile *profile.Profile, store *store.Store) *LicenseSer
return &LicenseService{
Profile: profile,
Store: store,
cachedSubscription: &apiv2pb.Subscription{
Plan: apiv2pb.PlanType_FREE,
cachedSubscription: &apiv1pb.Subscription{
Plan: apiv1pb.PlanType_FREE,
},
}
}
func (s *LicenseService) LoadSubscription(ctx context.Context) (*apiv2pb.Subscription, error) {
func (s *LicenseService) LoadSubscription(ctx context.Context) (*apiv1pb.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,
subscription := &apiv1pb.Subscription{
Plan: apiv1pb.PlanType_FREE,
}
licenseKey := ""
if workspaceSetting != nil {
@ -54,7 +54,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) (*apiv2pb.Subscri
return nil, errors.Wrap(err, "failed to validate license key")
}
if validateResponse.Valid {
subscription.Plan = apiv2pb.PlanType_PRO
subscription.Plan = apiv1pb.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 {
@ -72,7 +72,7 @@ func (s *LicenseService) LoadSubscription(ctx context.Context) (*apiv2pb.Subscri
return subscription, nil
}
func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey string) (*apiv2pb.Subscription, error) {
func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey string) (*apiv1pb.Subscription, error) {
if licenseKey == "" {
return nil, errors.New("license key is required")
}
@ -95,7 +95,7 @@ func (s *LicenseService) UpdateSubscription(ctx context.Context, licenseKey stri
return s.LoadSubscription(ctx)
}
func (s *LicenseService) GetSubscription(ctx context.Context) (*apiv2pb.Subscription, error) {
func (s *LicenseService) GetSubscription(ctx context.Context) (*apiv1pb.Subscription, error) {
return s.LoadSubscription(ctx)
}