config for scheme and subrouter

This commit is contained in:
2023-08-29 10:53:43 +08:00
parent 5550bb996d
commit 589577b718
4 changed files with 21 additions and 11 deletions

View File

@@ -268,11 +268,21 @@ func NewRouter() *mux.Router {
r.Use(midware.ArrowIPAddr)
for _, router := range routers {
r.Methods(router.Method).
Path(router.Pattern).
Handler(router.Handler)
// r.Methods(router.Method).
// Path(router.Pattern).
// Handler(router.Handler)
// if router.Middleware != nil {
// r.Use(router.Middleware)
// }
// if router.Middleware != nil {
// r.HandleFunc(router.Pattern, router.Handler).Methods(router.Method).Subrouter().Use(router.Middleware)
// } else {
// r.HandleFunc(router.Pattern, router.Handler).Methods(router.Method)
// }
rt := r.Methods(router.Method).Subrouter()
rt.HandleFunc(router.Pattern, router.Handler)
if router.Middleware != nil {
r.Use(router.Middleware)
rt.Use(router.Middleware)
}
}

View File

@@ -35,7 +35,7 @@ type YamlConfig struct {
IPv4 string `yaml:"ipv4"`
IPv6 string `yaml:"ipv6"`
Port uint16 `yaml:"port"`
Schema string `yaml:"schema"`
Scheme string `yaml:"scheme"`
CaFile string `yaml:"caFile"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
@@ -46,7 +46,7 @@ type YamlConfig struct {
RootDir string `yaml:"rootDir"`
Listen []struct {
Addr string `yaml:"addr"`
Schema string `yaml:"schema"`
Scheme string `yaml:"scheme"`
CaFile string `yaml:"caFile"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`

View File

@@ -17,7 +17,7 @@ rest:
- ipv4: 0.0.0.0
ipv6:
port: 4443
schema: https
scheme: https
caFile: ./etc/certs/rootca.crt
certFile: ./etc/certs/tsa-omc.crt
keyFile: ./etc/certs/tsa-omc_pri.key
@@ -29,7 +29,7 @@ webServer:
- addr: :8080
schema: http
- addr: :8443
schema: https
scheme: https
caFile: ./etc/certs/rootca.crt
certFile: ./etc/certs/tsa-omc.crt
keyFile: ./etc/certs/tsa-omc_pri.key

View File

@@ -173,7 +173,7 @@ func main() {
// ipv4 goroutines
if rest.IPv4 != "" {
listen := rest.IPv4 + ":" + strconv.Itoa(int(rest.Port))
if strings.ToLower(rest.Schema) == "https" {
if strings.ToLower(rest.Scheme) == "https" {
go HttpListenTLS(listen, rest.CertFile, rest.KeyFile, router)
} else {
go HttpListen(listen, router)
@@ -183,7 +183,7 @@ func main() {
// ipv6 goroutines
if rest.IPv6 != "" {
listenv6 := "[" + rest.IPv6 + "]" + ":" + strconv.Itoa(int(rest.Port))
if strings.ToLower(rest.Schema) == "https" {
if strings.ToLower(rest.Scheme) == "https" {
go HttpListenTLS(listenv6, rest.CertFile, rest.KeyFile, router)
} else {
go HttpListen(listenv6, router)
@@ -195,7 +195,7 @@ func main() {
fs := http.FileServer(http.Dir(conf.WebServer.RootDir))
http.Handle("/", fs)
for _, listen := range conf.WebServer.Listen {
if strings.ToLower(listen.Schema) == "https" {
if strings.ToLower(listen.Scheme) == "https" {
go HttpListenWebServerTLS(listen.Addr, listen.CertFile, listen.KeyFile)
} else {
go HttpListenWebServer(listen.Addr)