1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/app/restfulapi/service.go

59 lines
1.1 KiB
Go
Raw Normal View History

package restfulapi
2021-09-06 08:11:30 -04:00
import (
"context"
2021-10-28 06:34:19 -04:00
"net"
"sync"
2021-09-06 08:40:44 -04:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/features"
feature_stats "github.com/v2fly/v2ray-core/v4/features/stats"
2021-09-06 08:11:30 -04:00
)
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
type restfulService struct {
listener net.Listener
config *Config
access sync.Mutex
2021-09-06 08:40:44 -04:00
stats feature_stats.Manager
2021-09-06 08:11:30 -04:00
ctx context.Context
}
2021-09-07 13:23:08 -04:00
func (rs *restfulService) Type() interface{} {
2021-09-06 08:11:30 -04:00
return (*struct{})(nil)
}
2021-09-07 13:23:08 -04:00
func (rs *restfulService) Start() error {
defer rs.access.Unlock()
rs.access.Lock()
return rs.start()
2021-09-06 08:11:30 -04:00
}
2021-09-07 13:23:08 -04:00
func (rs *restfulService) Close() error {
defer rs.access.Unlock()
rs.access.Lock()
if rs.listener != nil {
return rs.listener.Close()
2021-09-06 08:11:30 -04:00
}
return nil
}
2021-09-06 08:40:44 -04:00
2021-09-07 13:23:08 -04:00
func (rs *restfulService) init(config *Config, stats feature_stats.Manager) {
rs.stats = stats
rs.config = config
2021-09-06 08:40:44 -04: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
}