func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	path := r.URL.Path
	statusCode, err := func() (int, error) {
		if !s.checkOriginFunc(r) {
			return http.StatusForbidden, fmt.Errorf("origin not allowed")
		}
		if s.enableCORS {
			origin := r.Header["Origin"]
			if len(origin) == 0 || len(origin[0]) == 0 {
				return 400, fmt.Errorf("failed to set header: Access-Control-Allow-Origin: HTTP request origin header is empty")
			}
			w.Header().Set("Access-Control-Allow-Origin", origin[0])    
			w.Header().Set("Access-Control-Allow-Methods", "POST,GET")  
		}
		if len(path) < s.httpURLStripLength {
			return http.StatusBadRequest, fmt.Errorf("invalid request")
		}
		path = path[s.httpURLStripLength:]
		if path == httpURLWebSocketSuffix {
			s.webSocketServer.HandleRequest(w, r)
		} else if path == httpURLAjaxSocketSuffix {
			s.ajaxSocketServer.HandleRequest(w, r)
		} else {
			return http.StatusBadRequest, fmt.Errorf("invalid request")
		}
		return http.StatusAccepted, nil
	}()
	if err != nil {
		w.WriteHeader(statusCode)
		remoteAddr, _ := utils.RemoteAddress(r)
		userAgent := r.Header.Get("User-Agent")
		log.L.WithFields(logrus.Fields{
			"remoteAddress": remoteAddr,
			"userAgent":     userAgent,
			"url":           r.URL.Path,
		}).Warningf("handle HTTP request: %v", err)
	}
}
ServeHTTP implements the HTTP Handler interface of the http package.
package backend
import (
	"fmt"
	"net/http"
	"github.com/Sirupsen/logrus"
	"github.com/desertbit/glue/backend/sockets/ajaxsocket"
	"github.com/desertbit/glue/backend/sockets/websocket"
	"github.com/desertbit/glue/log"
	"github.com/desertbit/glue/utils"
)
const (
	httpURLAjaxSocketSuffix = "ajax"
	httpURLWebSocketSuffix  = "ws"
)
type Server struct {
	onNewSocketConnection func(BackendSocket)
	httpURLStripLength int
	checkOriginFunc func(r *http.Request) bool
	enableCORS bool
	webSocketServer  *websocket.Server
	ajaxSocketServer *ajaxsocket.Server
}
func NewServer(httpURLStripLength int, enableCORS bool, checkOrigin func(r *http.Request) bool) *Server {
	s := &Server{
		onNewSocketConnection: func(BackendSocket) {},
		httpURLStripLength: httpURLStripLength,
		enableCORS:         enableCORS,
		checkOriginFunc:    checkOrigin,
	}
	s.webSocketServer = websocket.NewServer(func(ws *websocket.Socket) {
		s.triggerOnNewSocketConnection(ws)
	})
	s.ajaxSocketServer = ajaxsocket.NewServer(func(as *ajaxsocket.Socket) {
		s.triggerOnNewSocketConnection(as)
	})
	return s
}
func (s *Server) OnNewSocketConnection(f func(BackendSocket)) {
	s.onNewSocketConnection = f
}