summaryrefslogtreecommitdiff
path: root/service/http/handler.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-12 20:45:24 +0300
committerWolfy-J <[email protected]>2018-06-12 20:45:24 +0300
commit99c1121d8fd31b7b61b6c22e181dc3c05a3f9f82 (patch)
tree25e6baf4eaf4e4ebd37c2431e485dfe3f4e3ac11 /service/http/handler.go
parentf8d8e9a55d31c6ee98b1f292e49b31b474883a4f (diff)
static pool logic update, better watching techniques
Diffstat (limited to 'service/http/handler.go')
-rw-r--r--service/http/handler.go113
1 files changed, 113 insertions, 0 deletions
diff --git a/service/http/handler.go b/service/http/handler.go
new file mode 100644
index 00000000..1df00e5a
--- /dev/null
+++ b/service/http/handler.go
@@ -0,0 +1,113 @@
+package http
+
+import (
+ "net/http"
+ "strconv"
+ "github.com/spiral/roadrunner"
+ "github.com/pkg/errors"
+)
+
+const (
+ // EventResponse thrown after the request been processed. See Event as payload.
+ EventResponse = iota + 500
+
+ // EventError thrown on any non job error provided by road runner server.
+ EventError
+)
+
+// Event represents singular http response event.
+type Event struct {
+ // Method of the request.
+ Method string
+
+ // Uri requested by the client.
+ Uri string
+
+ // Status is response status.
+ Status int
+
+ // Associated error, if any.
+ Error error
+}
+
+// Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers,
+// parsed files and query, payload will include parsed form dataTree (if any).
+type Handler struct {
+ cfg *Config
+ listener func(event int, ctx interface{})
+ rr *roadrunner.Server
+}
+
+// AddListener attaches pool event watcher.
+func (h *Handler) Listen(l func(event int, ctx interface{})) {
+ h.listener = l
+}
+
+// Handle serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled.
+func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ // validating request size
+ if h.cfg.MaxRequest != 0 {
+ if length := r.Header.Get("content-length"); length != "" {
+ if size, err := strconv.ParseInt(length, 10, 64); err != nil {
+ h.handleError(w, r, err)
+ return
+ } else if size > h.cfg.MaxRequest*1024*1024 {
+ h.handleError(w, r, errors.New("request body max size is exceeded"))
+ return
+ }
+ }
+ }
+
+ req, err := NewRequest(r, h.cfg.Uploads)
+ if err != nil {
+ h.handleError(w, r, err)
+ return
+ }
+
+ if err = req.Open(); err != nil {
+ h.handleError(w, r, err)
+ return
+ }
+ defer req.Close()
+
+ p, err := req.Payload()
+ if err != nil {
+ h.handleError(w, r, err)
+ return
+ }
+
+ rsp, err := h.rr.Exec(p)
+ if err != nil {
+ h.handleError(w, r, err)
+ return
+ }
+
+ resp, err := NewResponse(rsp)
+ if err != nil {
+ h.handleError(w, r, err)
+ return
+ }
+
+ h.handleResponse(req, resp)
+ resp.Write(w)
+}
+
+// handleResponse triggers response event.
+func (h *Handler) handleResponse(req *Request, resp *Response) {
+ h.throw(EventResponse, &Event{Method: req.Method, Uri: req.Uri, Status: resp.Status})
+}
+
+// handleError sends error.
+func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) {
+ h.throw(EventError, &Event{Method: r.Method, Uri: uri(r), Status: 500, Error: err})
+
+ w.WriteHeader(500)
+ w.Write([]byte(err.Error()))
+}
+
+// throw invokes event srv if any.
+func (h *Handler) throw(event int, ctx interface{}) {
+ if h.listener != nil {
+ h.listener(event, ctx)
+ }
+}