summaryrefslogtreecommitdiff
path: root/cmd/_____/http/service.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-05 16:23:14 +0300
committerWolfy-J <[email protected]>2018-06-05 16:23:14 +0300
commit76ff8d1c95e087749d559ee5a4f8f0348feafffa (patch)
tree112630d2d2cfe41d809065034c13b1066b8e05c2 /cmd/_____/http/service.go
parent3c86132f90ef6473b4073a8b1500d01b6114fc30 (diff)
Cs and refactoring
Diffstat (limited to 'cmd/_____/http/service.go')
-rw-r--r--cmd/_____/http/service.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/cmd/_____/http/service.go b/cmd/_____/http/service.go
new file mode 100644
index 00000000..008aeab8
--- /dev/null
+++ b/cmd/_____/http/service.go
@@ -0,0 +1,80 @@
+package http
+
+import (
+ "context"
+ "github.com/sirupsen/logrus"
+ "github.com/spiral/roadrunner/service"
+ "net/http"
+ "github.com/spiral/roadrunner"
+)
+
+const ServiceName = "http"
+
+type Service struct {
+ cfg *serviceConfig
+ http *http.Server
+ srv *Server
+}
+
+func (s *Service) Name() string {
+ return ServiceName
+}
+
+func (s *Service) Configure(cfg service.Config) (bool, error) {
+ config := &serviceConfig{}
+ if err := cfg.Unmarshal(config); err != nil {
+ return false, err
+ }
+
+ if !config.Enabled {
+ return false, nil
+ }
+
+ if err := config.Valid(); err != nil {
+ return false, err
+ }
+
+ s.cfg = config
+ return true, nil
+}
+
+func (s *Service) RPC() interface{} {
+ return &rpcServer{s}
+}
+
+func (s *Service) Serve() error {
+ logrus.Debugf("http: started")
+ defer logrus.Debugf("http: stopped")
+
+ rr, term, err := s.cfg.Pool.NewServer()
+ if err != nil {
+ return err
+ }
+ defer term()
+
+ //todo: remove
+ rr.Observe(func(event int, ctx interface{}) {
+ switch event {
+ case roadrunner.EventPoolError:
+ logrus.Error(ctx)
+ case roadrunner.EventWorkerError:
+ logrus.Errorf("%s: %s", ctx.(roadrunner.WorkerError).Worker, ctx.(roadrunner.WorkerError).Error())
+ }
+ })
+
+ s.srv = NewServer(s.cfg.httpConfig(), rr)
+ s.http = &http.Server{
+ Addr: s.cfg.httpAddr(),
+ Handler: s.srv,
+ }
+
+ if err := s.http.ListenAndServe(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (s *Service) Stop() error {
+ return s.http.Shutdown(context.Background())
+}