africas-talking #50
@ -232,7 +232,7 @@ func main() {
|
||||
|
||||
rp := &atRequestParser{}
|
||||
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
||||
sh := httpserver.ToSessionHandler(bsh)
|
||||
sh := httpserver.ToSessionHandler(bsh, httpserver.WithAtOutput())
|
||||
Alfred-mk marked this conversation as resolved
Outdated
|
||||
s := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
||||
Handler: sh,
|
||||
|
@ -43,14 +43,30 @@ func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
|
||||
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 {
|
||||
return &SessionHandler{
|
||||
type SessionHandler struct {
|
||||
handlers.RequestHandler
|
||||
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) {
|
||||
@ -108,7 +124,11 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
w.WriteHeader(200)
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
if f.useAtOutput {
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
Similary here, Please do not put AT speicifc code in the generic http code. Please use your own extension of the class. Similary here, Please do not put AT speicifc code in the generic http code. Please use your own extension of the class.
|
||||
rqs, err = f.AtOutput(rqs)
|
||||
} else {
|
||||
rqs, err = f.Output(rqs)
|
||||
}
|
||||
if err != nil {
|
||||
f.writeError(w, 500, err)
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user
In general, chainable functions should be called ON the object instead of being an argument TO The object.