summaryrefslogtreecommitdiff
path: root/service/http/service.go
blob: ecce1c1572d98199ec3fdf5db409e98ef1d3c7da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package http

import (
	"context"
	"github.com/spiral/roadrunner"
	"github.com/spiral/roadrunner/service/env"
	"github.com/spiral/roadrunner/service/http/attributes"
	"github.com/spiral/roadrunner/service/rpc"
	"net/http"
	"sync"
	"sync/atomic"
)

const (
	// ID contains default svc name.
	ID = "http"

	// httpKey indicates to php process that it's running under http service
	httpKey = "rr_http"
)

// http middleware type.
type middleware func(f http.HandlerFunc) http.HandlerFunc

// Service manages rr, http servers.
type Service struct {
	cfg      *Config
	env      env.Environment
	lsns     []func(event int, ctx interface{})
	mdws     []middleware
	mu       sync.Mutex
	rr       *roadrunner.Server
	stopping int32
	srv      *Handler
	http     *http.Server
}

// AddMiddleware adds new net/http middleware.
func (s *Service) AddMiddleware(m middleware) {
	s.mdws = append(s.mdws, m)
}

// AddListener attaches server event watcher.
func (s *Service) AddListener(l func(event int, ctx interface{})) {
	s.lsns = append(s.lsns, l)
}

// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) {
	if !cfg.Enable {
		return false, nil
	}

	s.cfg = cfg
	s.env = e
	if r != nil {
		r.Register(ID, &rpcServer{s})
	}

	return true, nil
}

// Serve serves the svc.
func (s *Service) Serve() error {
	s.mu.Lock()

	if s.env != nil {
		values, err := s.env.GetEnv()
		if err != nil {
			return err
		}

		for k, v := range values {
			s.cfg.Workers.SetEnv(k, v)
		}

		s.cfg.Workers.SetEnv(httpKey, "true")
	}

	rr := roadrunner.NewServer(s.cfg.Workers)

	s.rr = rr
	s.srv = &Handler{cfg: s.cfg, rr: s.rr}
	s.http = &http.Server{Addr: s.cfg.Address}

	s.rr.Listen(s.listener)
	s.srv.Listen(s.listener)

	if len(s.mdws) == 0 {
		s.http.Handler = s.srv
	} else {
		s.http.Handler = s
	}
	s.mu.Unlock()

	if err := rr.Start(); err != nil {
		return err
	}
	defer s.rr.Stop()

	return s.http.ListenAndServe()
}

// Stop stops the svc.
func (s *Service) Stop() {
	if atomic.LoadInt32(&s.stopping) != 0 {
		// already stopping
		return
	}

	s.mu.Lock()
	defer s.mu.Unlock()
	if s.http == nil {
		return
	}

	s.http.Shutdown(context.Background())
}

// middleware handles connection using set of mdws and rr PSR-7 server.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r = attributes.Init(r)

	// chaining middlewares
	f := s.srv.ServeHTTP
	for _, m := range s.mdws {
		f = m(f)
	}
	f(w, r)
}

// listener handles service, server and pool events.
func (s *Service) listener(event int, ctx interface{}) {
	for _, l := range s.lsns {
		l(event, ctx)
	}

	if event == roadrunner.EventServerFailure {
		// underlying rr server is dead
		s.Stop()
	}
}