From e41e61966f390cde83fad4fd3599b9841822c171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=91=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D0=BD?= Date: Sat, 6 Apr 2024 23:12:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D1=87=D0=B8=D0=BA=20http?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http_handler.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 http_handler.go diff --git a/http_handler.go b/http_handler.go new file mode 100644 index 0000000..706dd8c --- /dev/null +++ b/http_handler.go @@ -0,0 +1,36 @@ +package rui + +import ( + "net/http" + "strings" +) + +type httpHandler struct { + app *application + prefix string +} + +func (h *httpHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { + switch req.Method { + case http.MethodGet: + path := `/` + strings.TrimPrefix(req.URL.Path, `/`) + req.URL.Path = `/` + strings.TrimPrefix(strings.TrimPrefix(path, h.prefix), `/`) + + h.app.ServeHTTP(w, req) + } +} + +func NewHandler(urlPrefix string, createContentFunc func(Session) SessionContent, params AppParams) *httpHandler { + app := new(application) + app.params = params + app.sessions = map[int]Session{} + app.createContentFunc = createContentFunc + apps = append(apps, app) + + h := &httpHandler{ + app: app, + prefix: `/` + strings.Trim(urlPrefix, `/`), + } + + return h +}