Skip to content

Commit

Permalink
Initiate SOAP server for receiving data from SdI
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 3e643b6 commit 02abd45
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions sdi/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sdi

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"golang.org/x/net/http2"
)

// ServerConfig defines soap server configuration options
type ServerConfig struct {
Host string
Port string
Verbose bool
CACert *x509.CertPool
CertAuth tls.Certificate
ClientAuth tls.ClientAuthType
}

// RunServer sets up a server for receiving invoices from SdI
func RunServer(config *ServerConfig, requestHandler http.HandlerFunc) error {
tlsConfig := &tls.Config{
ClientAuth: config.ClientAuth,
ClientCAs: config.CACert,
Certificates: []tls.Certificate{config.CertAuth},
}

server := http.Server{
Addr: config.Host + ":" + config.Port,
Handler: http.HandlerFunc(requestHandler),
TLSConfig: tlsConfig,
}

if err := http2.ConfigureServer(&server, &http2.Server{}); err != nil {
log.Fatalf("Failed to configure HTTP/2: %v", err)
}

go func() {
if err := server.ListenAndServeTLS("", ""); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("HTTP server error: %v", err)
}
log.Println("Stopped serving new connections.")
}()

sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan

shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()

if err := server.Shutdown(shutdownCtx); err != nil {
log.Fatalf("HTTP shutdown error: %v", err)
}
log.Println("Graceful shutdown complete.")

return nil
}

0 comments on commit 02abd45

Please sign in to comment.