devserver

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(cfg Config) error

Run 启动 devserver(阻塞运行)

Types

type AccountHandler

type AccountHandler struct {
	// contains filtered or unexported fields
}

AccountHandler 处理 /api/accounts 路由

func (*AccountHandler) ServeHTTP

func (h *AccountHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type AccountStore

type AccountStore struct {
	// contains filtered or unexported fields
}

AccountStore JSON 文件存储

func NewAccountStore

func NewAccountStore(filePath string) *AccountStore

NewAccountStore 创建账号存储

func (*AccountStore) Create

func (s *AccountStore) Create(a DevAccount) DevAccount

Create 创建账号

func (*AccountStore) Delete

func (s *AccountStore) Delete(id int64) bool

Delete 删除账号

func (*AccountStore) First

func (s *AccountStore) First() *DevAccount

First 返回第一个账号(代理时简单选取)

func (*AccountStore) Get

func (s *AccountStore) Get(id int64) *DevAccount

Get 根据 ID 获取账号

func (*AccountStore) List

func (s *AccountStore) List() []DevAccount

List 返回所有账号

func (*AccountStore) Update

func (s *AccountStore) Update(id int64, a DevAccount) *DevAccount

Update 更新账号

type Config

type Config struct {
	Plugin         sdk.GatewayPlugin                             // 必填:网关插件实例
	Addr           string                                        // 监听地址,默认 ":18080"
	DataDir        string                                        // 数据目录,默认 "./devdata"
	ExtraRoutes    func(mux *http.ServeMux, store *AccountStore) // 插件自定义路由(如 OAuth)
	SchedulePolicy SchedulePolicy                                // 调度策略,默认 "none"(直连第一个账号)
}

Config devserver 配置

type DevAccount

type DevAccount struct {
	ID          int64             `json:"id"`
	Name        string            `json:"name"`
	AccountType string            `json:"account_type"`
	Credentials map[string]string `json:"credentials"`
	ProxyURL    string            `json:"proxy_url,omitempty"`
	Weight      int               `json:"weight,omitempty"` // 调度权重,默认 1
}

DevAccount 开发用账号(复用 sdk.Account 字段 + 额外元数据)

type ProxyHandler

type ProxyHandler struct {
	// contains filtered or unexported fields
}

ProxyHandler 将请求代理给插件

func (*ProxyHandler) ServeHTTP

func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type SchedulePolicy

type SchedulePolicy string

SchedulePolicy 调度策略

const (
	ScheduleNone       SchedulePolicy = "none"        // 直连指定账号(默认第一个)
	ScheduleWeightedRR SchedulePolicy = "weighted_rr" // 加权轮询 + failover
)

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler 账号调度器

func NewScheduler

func NewScheduler(store *AccountStore, policy SchedulePolicy) *Scheduler

NewScheduler 创建调度器

func (*Scheduler) IsRetryable

func (s *Scheduler) IsRetryable(outcome sdk.ForwardOutcome, err error) bool

IsRetryable 判断转发结果是否可 failover。

func (*Scheduler) Policy

func (s *Scheduler) Policy() SchedulePolicy

Policy 返回当前策略

func (*Scheduler) ReportResult

func (s *Scheduler) ReportResult(accountID int64, outcome sdk.ForwardOutcome)

ReportResult 上报转发结果,用于标记冷却。

func (*Scheduler) Select

func (s *Scheduler) Select() *DevAccount

Select 根据策略选择账号

func (*Scheduler) SetPinned

func (s *Scheduler) SetPinned(accountID int64)

SetPinned 设置直连模式使用的账号,0 表示使用第一个

func (*Scheduler) SetPolicy

func (s *Scheduler) SetPolicy(policy SchedulePolicy)

SetPolicy 运行时切换策略

func (*Scheduler) Status

func (s *Scheduler) Status() map[string]any

Status 返回调度状态快照

type SchedulerHandler

type SchedulerHandler struct {
	// contains filtered or unexported fields
}

SchedulerHandler 调度管理 API

func (*SchedulerHandler) ServeHTTP

func (h *SchedulerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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