mirror of
https://github.com/oarkflow/mq.git
synced 2025-10-04 23:52:48 +08:00
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
|
|
"github.com/oarkflow/mq"
|
|
"github.com/oarkflow/mq/examples/tasks"
|
|
)
|
|
|
|
func main() {
|
|
// Load the server's certificate and key
|
|
cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
|
|
if err != nil {
|
|
log.Fatalf("Failed to load server certificate and key: %v", err)
|
|
}
|
|
|
|
// Load the CA certificate
|
|
caCert, err := ioutil.ReadFile("ca.crt")
|
|
if err != nil {
|
|
log.Fatalf("Failed to read CA certificate: %v", err)
|
|
}
|
|
caCertPool := x509.NewCertPool()
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
// Configure TLS for the server
|
|
tlsConfig := &tls.Config{
|
|
Certificates: []tls.Certificate{cert},
|
|
ClientCAs: caCertPool,
|
|
ClientAuth: tls.RequireAndVerifyClientCert, // Mutual TLS
|
|
}
|
|
|
|
// Start a TLS listener
|
|
listener, err := tls.Listen("tcp", ":8443", tlsConfig)
|
|
if err != nil {
|
|
log.Fatalf("Failed to start TLS listener: %v", err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
b := mq.NewBroker(mq.WithCallback(tasks.Callback))
|
|
b.NewQueue("queue1")
|
|
b.NewQueue("queue2")
|
|
|
|
log.Println("TLS-enabled broker started on :8443")
|
|
|
|
// Handle incoming connections
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
fmt.Println("Error accepting connection:", err)
|
|
continue
|
|
}
|
|
go handleConnection(b, conn)
|
|
}
|
|
}
|
|
|
|
func handleConnection(b *mq.Broker, conn net.Conn) {
|
|
defer conn.Close()
|
|
ctx := context.Background()
|
|
b.Start(ctx)
|
|
}
|