summaryrefslogtreecommitdiff
path: root/http/service.go
diff options
context:
space:
mode:
Diffstat (limited to 'http/service.go')
-rw-r--r--http/service.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/http/service.go b/http/service.go
new file mode 100644
index 00000000..e38e1a03
--- /dev/null
+++ b/http/service.go
@@ -0,0 +1,67 @@
+package http
+
+import (
+ "github.com/sirupsen/logrus"
+ "github.com/spiral/roadrunner/service"
+ "net/http"
+ "context"
+)
+
+type Service struct {
+ cfg *serviceConfig
+ http *http.Server
+ srv *Server
+}
+
+func (s *Service) Name() string {
+ return "http"
+}
+
+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()
+
+ 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())
+}