Skip to content

Commit

Permalink
Code refactoring after #352
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 29, 2023
1 parent ac5bcda commit bc6e4f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,6 @@ api:
base_path: "/rtc" # default "", API prefix for serve on suburl (/api => /rtc/api)
static_dir: "www" # default "", folder for static files (custom web interface)
origin: "*" # default "", allow CORS requests (only * supported)
tls_listen: ":1985" # default "", HTTPS port
tls_cert: | # default "". PEM-encoded fullchain certificate for https
-----BEGIN CERTIFICATE-----
.....
-----END CERTIFICATE-----
tls_private_key: | # default "". PEM-encoded private key for https
-----BEGIN PRIVATE KEY-----
.....
-----END PRIVATE KEY-----
```

**PS:**
Expand Down
55 changes: 27 additions & 28 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func Init() {
BasePath string `yaml:"base_path"`
StaticDir string `yaml:"static_dir"`
Origin string `yaml:"origin"`
TLSListen string `yaml:"tls_listen"`
TLSCert string `yaml:"tls_cert"`
TLSPrivateKey string `yaml:"tls_private_key"`
TLSListen string `yaml:"tls_listen"`
TLSCert string `yaml:"tls_cert"`
TLSKey string `yaml:"tls_key"`
} `yaml:"api"`
}

Expand Down Expand Up @@ -80,34 +80,33 @@ func Init() {
}()

// Initialize the HTTPS server
if cfg.Mod.TLSListen != "" {
tlsConfig := &tls.Config{}
if cfg.Mod.TLSCert != "" && cfg.Mod.TLSPrivateKey != "" {
tlsListener, err := net.Listen("tcp", cfg.Mod.TLSListen)
if err != nil {
log.Fatal().Err(err).Msg("[api] tls listen")
return
}
log.Info().Str("addr", cfg.Mod.TLSListen).Msg("[api] tls listen")
if cfg.Mod.TLSListen != "" && cfg.Mod.TLSCert != "" && cfg.Mod.TLSKey != "" {
cert, err := tls.X509KeyPair([]byte(cfg.Mod.TLSCert), []byte(cfg.Mod.TLSKey))
if err != nil {
log.Error().Err(err).Caller().Send()
return
}

cert, err := tls.X509KeyPair([]byte(cfg.Mod.TLSCert), []byte(cfg.Mod.TLSPrivateKey))
if err != nil {
print(cfg.Mod.TLSCert)
log.Fatal().Err(err).Msg("[api] tls load cert/key")
return
}
tlsConfig.Certificates = []tls.Certificate{cert}
tlsListener, err := net.Listen("tcp4", cfg.Mod.TLSListen)
if err != nil {
log.Fatal().Err(err).Caller().Send()
return
}

tlsServer := &http.Server{
Handler: Handler,
TLSConfig: tlsConfig,
}
go func() {
if err := tlsServer.ServeTLS(tlsListener, "", ""); err != nil {
log.Fatal().Err(err).Msg("[api] tls serve")
}
}()
log.Info().Str("addr", cfg.Mod.TLSListen).Msg("[api] tls listen")

tlsServer := &http.Server{
Handler: Handler,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}

go func() {
if err := tlsServer.ServeTLS(tlsListener, "", ""); err != nil {
log.Fatal().Err(err).Msg("[api] tls serve")
}
}()
}
}

Expand Down

0 comments on commit bc6e4f4

Please sign in to comment.