github.com/imannamdari/v2ray-core/v5@v5.0.5/app/restfulapi/service.go (about)

     1  package restfulapi
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"sync"
     7  
     8  	core "github.com/imannamdari/v2ray-core/v5"
     9  	"github.com/imannamdari/v2ray-core/v5/features"
    10  	feature_stats "github.com/imannamdari/v2ray-core/v5/features/stats"
    11  )
    12  
    13  //go:generate go run github.com/imannamdari/v2ray-core/v5/common/errors/errorgen
    14  
    15  type restfulService struct {
    16  	listener net.Listener
    17  	config   *Config
    18  	access   sync.Mutex
    19  
    20  	stats feature_stats.Manager
    21  
    22  	ctx context.Context
    23  }
    24  
    25  func (rs *restfulService) Type() interface{} {
    26  	return (*struct{})(nil)
    27  }
    28  
    29  func (rs *restfulService) Start() error {
    30  	defer rs.access.Unlock()
    31  	rs.access.Lock()
    32  	return rs.start()
    33  }
    34  
    35  func (rs *restfulService) Close() error {
    36  	defer rs.access.Unlock()
    37  	rs.access.Lock()
    38  	if rs.listener != nil {
    39  		return rs.listener.Close()
    40  	}
    41  	return nil
    42  }
    43  
    44  func (rs *restfulService) init(config *Config, stats feature_stats.Manager) {
    45  	rs.stats = stats
    46  	rs.config = config
    47  }
    48  
    49  func newRestfulService(ctx context.Context, config *Config) (features.Feature, error) {
    50  	r := new(restfulService)
    51  	r.ctx = ctx
    52  	if err := core.RequireFeatures(ctx, func(stats feature_stats.Manager) {
    53  		r.init(config, stats)
    54  	}); err != nil {
    55  		return nil, err
    56  	}
    57  	return r, nil
    58  }