github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/volts.go (about) 1 package volts 2 3 import ( 4 "os" 5 "os/signal" 6 "syscall" 7 8 "github.com/volts-dev/volts/client" 9 "github.com/volts-dev/volts/server" 10 ) 11 12 type ( 13 14 // Service is an interface that wraps the lower level libraries 15 // within volts. Its a convenience method for building 16 // and initialising services. 17 IService interface { 18 // The service name 19 Name() string 20 // Config returns the current options 21 Config() *Config 22 // Client is used to call services 23 Client() client.IClient 24 // Server is for handling requests and events 25 Server() server.IServer 26 // Run the service 27 Run() error 28 // 29 Stop() error 30 // The service implementation 31 String() string 32 } 33 34 service struct { 35 config *Config 36 } 37 ) 38 39 // NewService creates and returns a new Service based on the packages within. 40 func New(opts ...Option) IService { 41 return &service{ 42 config: newConfig(opts...), 43 } 44 } 45 46 func (self *service) Name() string { 47 return self.config.Server.Name() 48 } 49 50 func (self *service) Config() *Config { 51 return self.config 52 } 53 54 func (self *service) Start() error { 55 for _, fn := range self.config.BeforeStart { 56 if err := fn(); err != nil { 57 return err 58 } 59 } 60 61 if err := self.config.Server.Start(); err != nil { 62 return err 63 } 64 65 for _, fn := range self.config.AfterStart { 66 if err := fn(); err != nil { 67 return err 68 } 69 } 70 71 return nil 72 } 73 74 func (self *service) Stop() error { 75 var gerr error 76 77 for _, fn := range self.config.BeforeStop { 78 if err := fn(); err != nil { 79 gerr = err 80 } 81 } 82 83 if err := self.config.Server.Stop(); err != nil { 84 return err 85 } 86 87 for _, fn := range self.config.AfterStop { 88 if err := fn(); err != nil { 89 gerr = err 90 } 91 } 92 93 return gerr 94 } 95 96 func (self *service) Run() error { 97 /* 98 // register the debug handler 99 self.Config.Server.Handle( 100 self.Config.Server.NewHandler( 101 handler.NewHandler(self.opts.Client), 102 server.InternalHandler(true), 103 ), 104 ) 105 106 // start the profiler 107 if self.Config.Profile != nil { 108 // to view mutex contention 109 rtime.SetMutexProfileFraction(5) 110 // to view blocking profile 111 rtime.SetBlockProfileRate(1) 112 113 if err := self.opts.Profile.Start(); err != nil { 114 return err 115 } 116 defer self.opts.Profile.Stop() 117 } 118 */ 119 120 if err := self.Start(); err != nil { 121 return err 122 } 123 124 ch := make(chan os.Signal, 1) 125 if self.config.Signal { 126 signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL) 127 } 128 129 select { 130 // wait on kill signal 131 case <-ch: 132 // wait on context cancel 133 case <-self.config.Context.Done(): 134 } 135 136 return self.Stop() 137 } 138 139 func (self *service) Client() client.IClient { 140 return self.config.Client 141 } 142 143 func (self *service) Server() server.IServer { 144 return self.config.Server 145 } 146 147 func (self *service) String() string { 148 if self.config.Server != nil { 149 return self.config.Server.String() 150 } 151 152 return "volts service" 153 }