Skip to content

Commit

Permalink
Add simple SdI request handler
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Malaszkiewicz <[email protected]>
Co-authored-by: Grzeg Lisowski <[email protected]>
  • Loading branch information
torrocus and noplisu committed Jun 4, 2024
1 parent 02abd45 commit bcba28c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions sdi/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sdi

import (
"bytes"
"io"
"log"
"net/http"
"net/http/httputil"
)

// SdIMessageHandler processes SOAP requests from SdI (Sistema di Interscambio)
func SdIMessageHandler(w http.ResponseWriter, req *http.Request) {
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
log.Printf("Failed to dump incoming request: %s\n", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Printf("Incoming request:\n%s", requestDump)

responseBody := []byte("Hello World")
response := &http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
Proto: "HTTP/2.0",
ProtoMajor: 2,
ProtoMinor: 0,
Body: io.NopCloser(bytes.NewReader(responseBody)),
ContentLength: int64(len(responseBody)),
Header: make(http.Header),
}
response.Header.Set("Content-Type", "text/plain")

responseDump, err := httputil.DumpResponse(response, true)
if err != nil {
log.Printf("Failed to dump outgoing response: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Printf("Outgoing response:\n%s", responseDump)

for k, v := range response.Header {
for _, vv := range v {
w.Header().Add(k, vv)
}
}

w.WriteHeader(response.StatusCode)

_, err = w.Write(responseBody)
if err != nil {
log.Printf("Failed to send response: %v", err)
return
}
}

0 comments on commit bcba28c

Please sign in to comment.