forked from invopop/gobl.fatturapa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initiate SOAP server for receiving data from SdI
Co-authored-by: Alex Malaszkiewicz <[email protected]> Co-authored-by: Grzeg Lisowski <[email protected]>
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |