This commit is contained in:
2023-08-23 14:38:52 +08:00
parent 2a9ea8dc80
commit 055ed5eb69
4 changed files with 38 additions and 11 deletions

View File

@@ -14,9 +14,12 @@ rest:
- ipv4: 0.0.0.0
ipv6:
port: 3030
- ipv4:
ipv6: ::0
port: 6060
- ipv4: 0.0.0.0
ipv6:
port: 8443
schema: https
certFile: /usr/local/omc/etc/certs/tsa-omc.pem
keyFile: /usr/local/omc/etc/certs/tsa-omc_pri.pem
database:
type: mysql

View File

@@ -32,9 +32,12 @@ type YamlConfig struct {
} `yaml:"logger"`
Rest []struct {
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Port uint16 `yaml:"port"`
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Port uint16 `yaml:"port"`
Schema string `yaml:"schema"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
} `yaml:"rest"`
Database DbConfig `yaml:"database"`

View File

@@ -14,9 +14,12 @@ rest:
- ipv4: 0.0.0.0
ipv6:
port: 3040
- ipv4:
ipv6: ::0
port: 6070
- ipv4: 0.0.0.0
ipv6:
port: 8443
schema: https
certFile: /usr/local/omc/etc/certs/tsa-omc.pem
keyFile: /usr/local/omc/etc/certs/tsa-omc_pri.pem
database:
type: mysql

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"ems.agt/lib/dborm"
"ems.agt/lib/global"
@@ -70,6 +71,14 @@ func HttpListen(addr string, router http.Handler) {
}
}
func HttpListenTLS(addr, certFile, keyFile string, router http.Handler) {
err := http.ListenAndServeTLS(addr, certFile, keyFile, router)
if err != nil {
fmt.Println("ListenAndServeTLS err:", err)
os.Exit(6)
}
}
func main() {
conf := config.GetYamlConfig()
@@ -117,12 +126,21 @@ func main() {
// ipv4 goroutines
if rest.IPv4 != "" {
listen := rest.IPv4 + ":" + strconv.Itoa(int(rest.Port))
go HttpListen(listen, router)
if strings.ToLower(rest.Schema) == "https" {
go HttpListenTLS(listen, rest.CertFile, rest.KeyFile, router)
} else {
go HttpListen(listen, router)
}
}
// ipv6 goroutines
if rest.IPv6 != "" {
listenv6 := "[" + rest.IPv6 + "]" + ":" + strconv.Itoa(int(rest.Port))
go HttpListen(listenv6, router)
if strings.ToLower(rest.Schema) == "https" {
go HttpListenTLS(listenv6, rest.CertFile, rest.KeyFile, router)
} else {
go HttpListen(listenv6, router)
}
}
}