have AtOutput as an option

This commit is contained in:
Alfred Kamanda 2024-09-13 16:03:31 +03:00
parent 512460fdeb
commit b53658e038
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
2 changed files with 26 additions and 6 deletions

View File

@ -232,7 +232,7 @@ func main() {
rp := &atRequestParser{} rp := &atRequestParser{}
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
sh := httpserver.ToSessionHandler(bsh) sh := httpserver.ToSessionHandler(bsh, httpserver.WithAtOutput())
s := &http.Server{ s := &http.Server{
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))), Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
Handler: sh, Handler: sh,

View File

@ -43,14 +43,30 @@ func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
return v, nil return v, nil
} }
type SessionHandler struct {
handlers.RequestHandler
type SessionHandlerOption func(*SessionHandler)
func WithAtOutput() SessionHandlerOption {
return func(sh *SessionHandler) {
sh.useAtOutput = true
}
} }
func ToSessionHandler(h handlers.RequestHandler) *SessionHandler { type SessionHandler struct {
return &SessionHandler{ handlers.RequestHandler
RequestHandler: h, useAtOutput bool
} }
func ToSessionHandler(h handlers.RequestHandler, opts ...SessionHandlerOption) *SessionHandler {
sh := &SessionHandler{
RequestHandler: h,
useAtOutput: false,
}
for _, opt := range opts {
opt(sh)
}
return sh
} }
func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) { func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) {
@ -108,7 +124,11 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200) w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
if f.useAtOutput {
rqs, err = f.AtOutput(rqs)
} else {
rqs, err = f.Output(rqs) rqs, err = f.Output(rqs)
}
if err != nil { if err != nil {
f.writeError(w, 500, err) f.writeError(w, 500, err)
return return