-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw-context-impl.go
65 lines (52 loc) · 1.76 KB
/
hw-context-impl.go
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
package hardwire
import (
"errors"
"fmt"
echo "github.com/labstack/echo/v4"
hw "github.com/ncpa0/hardwire/hw-context"
resources "github.com/ncpa0/hardwire/resources"
"github.com/ncpa0/hardwire/utils"
. "github.com/ncpa0cpl/ezs"
)
type HwContext struct{}
func (ctx *HwContext) GetResourceHandler(e echo.Context, resourceKey string) (func(rootPath string, params map[string]string) (interface{}, error), error) {
entry, found := resources.GetResource(resourceKey)
if !found {
e.String(404, "Invalid request")
return nil, fmt.Errorf("Resource od key '%s' not found", resourceKey)
}
handler := resources.GetResourceHandler(entry)
return func(rootPath string, params map[string]string) (interface{}, error) {
dynReqCtx := resources.NewDynamicRequestContext(e, params, rootPath)
return handler(dynReqCtx)
}, nil
}
func (ctx *HwContext) GetResource(ectx echo.Context, resourceKey string) (interface{}, error) {
handler, err := ctx.GetResourceHandler(ectx, resourceKey)
if err != nil {
return nil, err
}
routePathname := ectx.Request().Header.Get("Hardwire-Dynamic-Fragment-Request")
if routePathname == "" {
return nil, errors.New("missing hardwire header")
}
hxCurrentUrl := ectx.Request().Header.Get("Hx-Current-Url")
params := utils.ParseUrlParams(routePathname, hxCurrentUrl)
resource, err := handler(routePathname, params)
if err != nil {
return "", err
}
return resource, nil
}
func (ctx *HwContext) BuildFragment(fragment hw.BuildableFragment, resources *Map[string, interface{}]) (string, error) {
resKey := fragment.ResourceKeys()[0]
resource, ok := resources.Get(resKey)
if !ok || resource == nil {
return "", errors.New("resource not found")
}
html, err := fragment.Build(resource)
if err != nil {
return "", err
}
return html, nil
}