forked from romapres2010/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_echo.go
More file actions
39 lines (29 loc) · 1.39 KB
/
Copy pathhandler_echo.go
File metadata and controls
39 lines (29 loc) · 1.39 KB
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
package httpservice
import (
"context"
"fmt"
"net/http"
myctx "github.com/romapres2010/httpserver/ctx"
mylog "github.com/romapres2010/httpserver/log"
)
// EchoHandler handle echo page with request header and body
func (s *Service) EchoHandler(w http.ResponseWriter, r *http.Request) {
mylog.PrintfDebugMsg("START ==================================================================================")
// Запускаем обработчик, возврат ошибки игнорируем
_ = s.process("POST", w, r, func(ctx context.Context, requestBuf []byte, buf []byte) ([]byte, Header, int, error) {
reqID := myctx.FromContextRequestID(ctx) // RequestID передается через context
mylog.PrintfDebugMsg("START: reqID", reqID)
// формируем ответ
header := Header{}
header["Errcode"] = "0"
header["RequestID"] = fmt.Sprintf("%v", reqID)
// Считаем параметры из заголовка сообщения и перенесем их в ответный заголовок
for key := range r.Header {
header[key] = r.Header.Get(key)
}
mylog.PrintfDebugMsg("SUCCESS", reqID)
// входной буфер возвращаем в качестве выходного
return requestBuf, header, http.StatusOK, nil
})
mylog.PrintfDebugMsg("SUCCESS ==================================================================================")
}