1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-05 13:35:23 +00:00
v2fly/app/restfulapi/service.go

59 lines
1.1 KiB
Go
Raw Normal View History

package restfulapi
2021-09-06 12:11:30 +00:00
import (
"context"
2021-10-28 10:34:19 +00:00
"net"
"sync"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/features"
feature_stats "github.com/v2fly/v2ray-core/v5/features/stats"
2021-09-06 12:11:30 +00:00
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2021-09-06 12:11:30 +00:00
type restfulService struct {
listener net.Listener
config *Config
access sync.Mutex
2021-09-06 12:40:44 +00:00
stats feature_stats.Manager
2021-09-06 12:11:30 +00:00
ctx context.Context
}
2021-09-07 17:23:08 +00:00
func (rs *restfulService) Type() interface{} {
2021-09-06 12:11:30 +00:00
return (*struct{})(nil)
}
2021-09-07 17:23:08 +00:00
func (rs *restfulService) Start() error {
defer rs.access.Unlock()
rs.access.Lock()
return rs.start()
2021-09-06 12:11:30 +00:00
}
2021-09-07 17:23:08 +00:00
func (rs *restfulService) Close() error {
defer rs.access.Unlock()
rs.access.Lock()
if rs.listener != nil {
return rs.listener.Close()
2021-09-06 12:11:30 +00:00
}
return nil
}
2021-09-06 12:40:44 +00:00
2021-09-07 17:23:08 +00:00
func (rs *restfulService) init(config *Config, stats feature_stats.Manager) {
rs.stats = stats
rs.config = config
2021-09-06 12:40:44 +00:00
}
func newRestfulService(ctx context.Context, config *Config) (features.Feature, error) {
r := new(restfulService)
r.ctx = ctx
if err := core.RequireFeatures(ctx, func(stats feature_stats.Manager) {
r.init(config, stats)
}); err != nil {
return nil, err
}
return r, nil
}