response file

This commit is contained in:
kekskurse 2024-09-13 22:33:57 +02:00
parent b80633c7b7
commit 37fe2268c9

15
main.go
View file

@ -18,6 +18,7 @@ type routeConfig struct {
Path string Path string
ResponseHTTPStatus int `yaml:"response_http_status"` ResponseHTTPStatus int `yaml:"response_http_status"`
ResponseBody string `yaml:"response_body"` ResponseBody string `yaml:"response_body"`
ResponseFile string `yaml:"response_file"`
ResponseHeaders map[string]string `yaml:"response_header"` ResponseHeaders map[string]string `yaml:"response_header"`
} }
@ -35,10 +36,24 @@ func (ro routeConfig) httpHandler(w http.ResponseWriter, r *http.Request) {
for v := range ro.ResponseHeaders { for v := range ro.ResponseHeaders {
w.Header().Add(v, ro.ResponseHeaders[v]) w.Header().Add(v, ro.ResponseHeaders[v])
} }
if ro.ResponseBody != "" {
_, err := w.Write([]byte(ro.ResponseBody)) _, err := w.Write([]byte(ro.ResponseBody))
if err != nil { if err != nil {
log.Error().Err(err).Msg("cant write body to response writer") log.Error().Err(err).Msg("cant write body to response writer")
} }
}
if ro.ResponseFile != "" {
content, err := os.ReadFile(os.ResponseFile)
if err != nil {
log.Error().Err(err).Msg("cant read response file")
}
_, err = w.Write(content)
if err != nil {
log.Error().Err(err).Msg("cant write body to response writer")
}
}
rde := requestData{ rde := requestData{
Path: r.URL.Path, Path: r.URL.Path,