我有 viper 在 config.yml 中提供以下值并在此处附加其余配置:
servers:
- projectname: test
directory: D:\playgroud\test
port: 1029
- projectname: test1
directory: D:\playground\test1
port: 1030
配置.go 包含
package config
import ()
type Configuration struct {
Servers []ServerConfiguration
}
package main
import (
"config"
"github.com/spf13/viper"
"log"
"net/http"
)
服务器.go
package config
type ServerConfiguration struct {
ProjectName string
Directory string
Port string
}
Main.go
func main() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
var configuration config.Configuration
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
err := viper.Unmarshal(&configuration)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
counter := 0
finish := make(chan bool)
for _, h := range configuration.Servers {
counter = counter +1
log.Println(counter)
counter := http.NewServeMux()
counter.HandleFunc("/"+h.ProjectName, foo8001 )
go func() {
http.ListenAndServe(":"+h.Port, counter)
}()
}
<-finish
}
func foo8001(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Listening on 8001: foo "))
}
但是当我运行服务器时,我只看到一个在 1030 和 1029 的服务似乎该过程已经结束。
这是输出:
2018/02/25 03:53:30 1
2018/02/25 03:53:30 2
我在这里做错了什么,将不胜感激任何见解以更好地理解它,也将不胜感激任何想法或建议。
提前致谢!