Code refactoring after #352
This commit is contained in:
@@ -623,15 +623,6 @@ api:
|
|||||||
base_path: "/rtc" # default "", API prefix for serve on suburl (/api => /rtc/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)
|
static_dir: "www" # default "", folder for static files (custom web interface)
|
||||||
origin: "*" # default "", allow CORS requests (only * supported)
|
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:**
|
**PS:**
|
||||||
|
|||||||
+29
-30
@@ -23,9 +23,9 @@ func Init() {
|
|||||||
BasePath string `yaml:"base_path"`
|
BasePath string `yaml:"base_path"`
|
||||||
StaticDir string `yaml:"static_dir"`
|
StaticDir string `yaml:"static_dir"`
|
||||||
Origin string `yaml:"origin"`
|
Origin string `yaml:"origin"`
|
||||||
TLSListen string `yaml:"tls_listen"`
|
TLSListen string `yaml:"tls_listen"`
|
||||||
TLSCert string `yaml:"tls_cert"`
|
TLSCert string `yaml:"tls_cert"`
|
||||||
TLSPrivateKey string `yaml:"tls_private_key"`
|
TLSKey string `yaml:"tls_key"`
|
||||||
} `yaml:"api"`
|
} `yaml:"api"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,34 +80,33 @@ func Init() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Initialize the HTTPS server
|
// Initialize the HTTPS server
|
||||||
if cfg.Mod.TLSListen != "" {
|
if cfg.Mod.TLSListen != "" && cfg.Mod.TLSCert != "" && cfg.Mod.TLSKey != "" {
|
||||||
tlsConfig := &tls.Config{}
|
cert, err := tls.X509KeyPair([]byte(cfg.Mod.TLSCert), []byte(cfg.Mod.TLSKey))
|
||||||
if cfg.Mod.TLSCert != "" && cfg.Mod.TLSPrivateKey != "" {
|
if err != nil {
|
||||||
tlsListener, err := net.Listen("tcp", cfg.Mod.TLSListen)
|
log.Error().Err(err).Caller().Send()
|
||||||
if err != nil {
|
return
|
||||||
log.Fatal().Err(err).Msg("[api] tls listen")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Info().Str("addr", cfg.Mod.TLSListen).Msg("[api] tls listen")
|
|
||||||
|
|
||||||
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}
|
|
||||||
|
|
||||||
tlsServer := &http.Server{
|
|
||||||
Handler: Handler,
|
|
||||||
TLSConfig: tlsConfig,
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
if err := tlsServer.ServeTLS(tlsListener, "", ""); err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("[api] tls serve")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tlsListener, err := net.Listen("tcp4", cfg.Mod.TLSListen)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Caller().Send()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user