2024-08-20 20:01:26 +03:00
|
|
|
//go:build !wasm
|
|
|
|
|
2024-04-12 17:29:33 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// NewHandler is used to embed the rui application in third-party web frameworks (net/http, gin, echo...).
|
|
|
|
//
|
|
|
|
// Example for echo:
|
|
|
|
//
|
|
|
|
// e := echo.New()
|
|
|
|
// e.Any(`/ui/*`, func()echo.HandlerFunc{
|
|
|
|
// rui.AddEmbedResources(&resources)
|
|
|
|
//
|
|
|
|
// h := rui.NewHandler("/ui", CreateSessionContent, rui.AppParams{
|
|
|
|
// Title: `Awesome app`,
|
|
|
|
// Icon: `favicon.png`,
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// return func(c echo.Context) error {
|
|
|
|
// h.ServeHTTP(c.Response(), c.Request())
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// })
|
2024-04-12 17:29:33 +03:00
|
|
|
func NewHandler(urlPrefix string, createContentFunc func(Session) SessionContent, params AppParams) *httpHandler {
|
|
|
|
app := new(application)
|
|
|
|
app.params = params
|
2024-04-29 12:32:24 +03:00
|
|
|
app.sessions = map[int]sessionInfo{}
|
2024-04-12 17:29:33 +03:00
|
|
|
app.createContentFunc = createContentFunc
|
|
|
|
apps = append(apps, app)
|
|
|
|
|
|
|
|
h := &httpHandler{
|
|
|
|
app: app,
|
|
|
|
prefix: `/` + strings.Trim(urlPrefix, `/`),
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|