native

package
v4.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package native implements the plan package's v2 routes in the package itself, on the kernel's v2_plan and v2_event tables adopted in place (kernel.storage.adopt). An administrator's plan assignment changes the subscriber through the kernel's KernelSubscriber contract (kernel.subscriber.entitlements.v1): the package never writes v2_user. Legacy handlers and native routes share the tables and the subscriber request ledger, so a route can switch between them at any time. Responses are byte-compatible with the legacy handlers (internal/tests/plancompat).

The five /api/v2/speed-limit routes are Flux forward limits, whose rows name forward tunnels: they belong to the forward package.

Index

Constants

View Source
const AssignRouteID = "plan.admin.plans.id.assign.post"

AssignRouteID is the administrator's plan assignment.

Variables

This section is empty.

Functions

func AssignRequestID

func AssignRequestID(planID uint64, userID uint, expireAt *int64, token string) string

AssignRequestID names an administrator's plan assignment in the kernel's subscriber request ledger, so a retried request is applied once. token identifies the HTTP request: its Idempotency-Key, else its X-Request-ID, else a fresh random value. A new request is a new grant, as in v2: an administrator who assigns the same plan again resets the traffic again. The kernel's legacy handler derives the same id (service.PlanAssignmentRequestID), so a retry is recognized whichever side serves it.

Types

type Event

type Event struct {
	ID          uint       `gorm:"primaryKey" json:"id"`
	Type        string     `gorm:"size:100;index" json:"type"`
	Payload     *string    `gorm:"type:text" json:"payload"`
	Status      string     `gorm:"size:20;default:'pending'" json:"status"`
	CreatedAt   time.Time  `json:"created_at"`
	ProcessedAt *time.Time `json:"processed_at"`
}

Event is a v2_event row: the plan domain's event log (plan.created, plan.updated, plan.deleted, plan.assigned). Nothing else writes it.

func (Event) TableName

func (Event) TableName() string

TableName is the adopted kernel table.

type Plan

type Plan struct {
	ID                 uint    `gorm:"primaryKey" json:"id"`
	GroupID            uint    `gorm:"index" json:"group_id"`
	TransferEnable     int64   `json:"transfer_enable"` // GB
	SpeedLimit         *int64  `json:"speed_limit"`
	DeviceLimit        *int    `json:"device_limit"`
	Name               string  `gorm:"size:255" json:"name"`
	Content            *string `gorm:"type:text" json:"content"`
	Show               int     `gorm:"default:0" json:"show"`
	Sort               *int    `json:"sort"`
	Renew              int     `gorm:"default:1" json:"renew"`
	ResetPrice         *int64  `json:"reset_price"`
	ResetTrafficMethod *int    `json:"reset_traffic_method"`
	CapacityLimit      *int    `json:"capacity_limit"`

	// Prices
	MonthPrice     *int64 `json:"month_price"`
	QuarterPrice   *int64 `json:"quarter_price"`
	HalfYearPrice  *int64 `json:"half_year_price"`
	YearPrice      *int64 `json:"year_price"`
	TwoYearPrice   *int64 `json:"two_year_price"`
	ThreeYearPrice *int64 `json:"three_year_price"`
	OnetimePrice   *int64 `json:"onetime_price"`

	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

Plan is a v2_plan row. Its fields, tags and type name match the kernel model, so answers and JSON binding errors are the same.

func (Plan) TableName

func (Plan) TableName() string

TableName is the adopted kernel table.

type Service

type Service struct {
	// Open returns the package's storage connection, on which the adopted
	// tables are visible.
	Open func(ctx context.Context) (*gorm.DB, error)
	// Subscriber is the kernel's KernelSubscriber; without it the
	// assignment route has no native handler and stays legacy.
	Subscriber Subscriber
	// Now defaults to time.Now.
	Now func() time.Time
	// NewToken names a request that carries neither an Idempotency-Key nor
	// an X-Request-ID; it defaults to a random UUID.
	NewToken func() string
}

Service holds what the native routes need.

func (*Service) AdminAssign

AdminAssign is POST /api/v2/admin/plans/:id/assign: the plan's group, traffic and limits for the subscriber, the expiry when given, and the traffic counters reset; the subscriber's subscription groups are kept. The kernel applies it through KernelSubscriber.ApplyEntitlement.

func (*Service) AdminCreate

AdminCreate is POST /api/v2/admin/plans.

func (*Service) AdminDelete

AdminDelete is DELETE /api/v2/admin/plans/:id.

func (*Service) AdminGet

AdminGet is GET /api/v2/admin/plans/:id.

func (*Service) AdminList

AdminList is GET /api/v2/admin/plans.

func (*Service) AdminUpdate

AdminUpdate is PUT /api/v2/admin/plans/:id. The body replaces every column but the creation time.

func (*Service) Handlers

func (s *Service) Handlers() map[string]pluginhostsdk.NativeHandler

Handlers returns the native handlers by route id.

func (*Service) UserPlans

UserPlans is GET /api/v2/user/plan: the plans on sale.

type Subscriber

type Subscriber interface {
	ApplyEntitlement(ctx context.Context, in *kernelsubscriberv1.ApplyEntitlementRequest, opts ...grpc.CallOption) (*kernelsubscriberv1.ApplyEntitlementResponse, error)
}

Subscriber is the part of KernelSubscriber the native routes call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL