1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 02:16:10 -05:00
v2fly/app/restful-api/service.go
2021-09-08 19:37:06 +01:00

58 lines
1.1 KiB
Go

package restful_api
import (
"context"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/features"
feature_stats "github.com/v2fly/v2ray-core/v4/features/stats"
"net"
"sync"
)
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
type restfulService struct {
listener net.Listener
config *Config
access sync.Mutex
stats feature_stats.Manager
ctx context.Context
}
func (rs *restfulService) Type() interface{} {
return (*struct{})(nil)
}
func (rs *restfulService) Start() error {
defer rs.access.Unlock()
rs.access.Lock()
return rs.start()
}
func (rs *restfulService) Close() error {
defer rs.access.Unlock()
rs.access.Lock()
if rs.listener != nil {
return rs.listener.Close()
}
return nil
}
func (rs *restfulService) init(config *Config, stats feature_stats.Manager) {
rs.stats = stats
rs.config = config
}
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
}