Skip to content

Commit

Permalink
Merge pull request #763 from gopcua/removeFatals
Browse files Browse the repository at this point in the history
Issue #762: Remove log.Fatal's.
  • Loading branch information
magiconair authored Jan 11, 2025
2 parents a958e31 + 35483e1 commit 780b10f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
17 changes: 8 additions & 9 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"math/rand"
"net"
Expand Down Expand Up @@ -276,8 +277,7 @@ func loadPrivateKey(filename string) (*rsa.PrivateKey, error) {
// It also detects and sets the ApplicationURI from the URI within the certificate.
func Certificate(cert []byte) Option {
return func(cfg *Config) error {
setCertificate(cert, cfg)
return nil
return setCertificate(cert, cfg)
}
}

Expand All @@ -294,8 +294,7 @@ func CertificateFile(filename string) Option {
if err != nil {
return err
}
setCertificate(cert, cfg)
return nil
return setCertificate(cert, cfg)
}
}

Expand All @@ -316,23 +315,23 @@ func loadCertificate(filename string) ([]byte, error) {
return block.Bytes, nil
}

func setCertificate(cert []byte, cfg *Config) {
func setCertificate(cert []byte, cfg *Config) error {
cfg.sechan.Certificate = cert

// Extract the application URI from the certificate.
x509cert, err := x509.ParseCertificate(cert)
if err != nil {
log.Fatalf("Failed to parse certificate: %s", err)
return
return fmt.Errorf("failed to parse certificate: %s", err)
}
if len(x509cert.URIs) == 0 {
return
return nil
}
appURI := x509cert.URIs[0].String()
if appURI == "" {
return
return nil
}
cfg.session.ClientDescription.ApplicationURI = appURI
return nil
}

// SecurityFromEndpoint sets the server-related security parameters from
Expand Down
15 changes: 11 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ func New(opts ...Option) *Server {
for _, opt := range opts {
opt(cfg)
}
if len(cfg.endpoints) == 0 {
log.Fatalf("No endpoints defined!")
url := ""
if len(cfg.endpoints) != 0 {
url = cfg.endpoints[0]
}

s := &Server{
url: cfg.endpoints[0],
url: url,
cfg: cfg,
cb: newChannelBroker(cfg.logger),
sb: newSessionBroker(cfg.logger),
Expand Down Expand Up @@ -148,7 +150,8 @@ func New(opts ...Option) *Server {
n0, ok := s.namespaces[0].(*NodeNameSpace)
n0.srv = s
if !ok {
log.Fatalf("not a node namespace!")
// this should never happen because we just set namespace 0 to be a node namespace
log.Panic("Namespace 0 is not a node namespace!")
}
s.ImportNodeSet(&nodes)

Expand Down Expand Up @@ -237,6 +240,10 @@ func (s *Server) URLs() []string {
func (s *Server) Start(ctx context.Context) error {
var err error

if len(s.cfg.endpoints) == 0 {
return fmt.Errorf("cannot start server: no endpoints defined")
}

// Register all service handlers
s.initHandlers()

Expand Down

0 comments on commit 780b10f

Please sign in to comment.