service.go 1.1 KB

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