htmgo/h/base.go

69 lines
1.2 KiB
Go
Raw Normal View History

2024-09-11 00:52:18 +00:00
package h
import (
"github.com/gofiber/fiber/v2"
2024-09-11 17:31:40 +00:00
"html"
2024-09-11 00:52:18 +00:00
"net/http"
"reflect"
"runtime"
)
type Headers = map[string]string
type Partial struct {
Headers *Headers
Root *Node
}
2024-09-11 17:31:40 +00:00
func (p *Partial) ToNode() *Node {
return p.Root
}
2024-09-11 00:52:18 +00:00
type Page struct {
Root *Node
HttpMethod string
}
func NewPage(root *Node) *Page {
return &Page{
HttpMethod: http.MethodGet,
Root: root,
}
}
func NewPageWithHttpMethod(httpMethod string, root *Node) *Page {
return &Page{
HttpMethod: httpMethod,
Root: root,
}
}
2024-09-11 21:08:35 +00:00
func WrapPartial(ctx *fiber.Ctx, cb func(ctx *fiber.Ctx) *Partial) *Node {
return cb(ctx).Root
}
2024-09-11 00:52:18 +00:00
func NewPartialWithHeaders(headers *Headers, root *Node) *Partial {
return &Partial{
Headers: headers,
Root: root,
}
}
func NewPartial(root *Node) *Partial {
return &Partial{
Root: root,
}
}
func GetFunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func GetPartialPath(partial func(ctx *fiber.Ctx) *Partial) string {
return GetFunctionName(partial)
}
func GetPartialPathWithQs(partial func(ctx *fiber.Ctx) *Partial, qs string) string {
2024-09-11 17:31:40 +00:00
return html.EscapeString(GetPartialPath(partial) + "?" + qs)
2024-09-11 00:52:18 +00:00
}