draincloud-core/internal/handler/handler.go

67 lines
1.7 KiB
Go
Raw Normal View History

2024-12-29 20:38:20 +00:00
package handler
import (
"context"
"git.optclblast.xyz/draincloud/draincloud-core/internal/common"
)
type Writer interface {
Write(ctx context.Context, resp any)
SetCookie(name string, value string, maxAge int, path string, domain string, secure bool, httpOnly bool)
2024-12-29 20:38:20 +00:00
}
type Handler interface {
GetName() string
GetRequiredResolveParams() []string
GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error
2025-01-02 23:33:15 +00:00
GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error
}
type BaseHandler struct {
2024-12-29 20:38:20 +00:00
Name string
RequiredResolveParams []string
ProcessFn func(ctx context.Context, req *common.Request, w Writer) error
2025-01-02 23:33:15 +00:00
PreprocessFn func(ctx context.Context, req *common.Request, w Writer) error
2024-12-29 20:38:20 +00:00
}
func New() *BaseHandler {
return new(BaseHandler)
}
func (h *BaseHandler) WithName(name string) *BaseHandler {
h.Name = name
return h
}
func (h *BaseHandler) WithRequiredResolveParams(params ...string) *BaseHandler {
h.RequiredResolveParams = params
return h
}
func (h *BaseHandler) WithProcessFunc(fn func(ctx context.Context, req *common.Request, w Writer) error) *BaseHandler {
h.ProcessFn = fn
return h
}
2025-01-02 23:33:15 +00:00
func (h *BaseHandler) WithPreprocessFunc(fn func(ctx context.Context, req *common.Request, w Writer) error) *BaseHandler {
h.PreprocessFn = fn
return h
}
func (h *BaseHandler) GetName() string {
return h.Name
}
func (h *BaseHandler) GetRequiredResolveParams() []string {
return h.RequiredResolveParams
}
func (h *BaseHandler) GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error {
return h.ProcessFn
}
2025-01-02 23:33:15 +00:00
func (h *BaseHandler) GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error {
return h.PreprocessFn
}