dongpao5261 2018-02-25 12:19
浏览 27
已采纳

使用viper config动态创建的带有net / http的go例程

I have viper providing the following values in config.yml & attached the rest of the configs here:

servers:
  - projectname: test
    directory: D:\playgroud\test
    port: 1029
  - projectname: test1
    directory: D:\playground\test1
    port: 1030

Coniguration.go contains

package config

import ()

type Configuration struct {
    Servers []ServerConfiguration
}



package main

    import (
        "config"
        "github.com/spf13/viper"
        "log"
        "net/http"
    )

Server.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 "))
    }

But when i run the server i only see one serving at 1030 and 1029 seems like the process has ended.

Here is the output:

2018/02/25 03:53:30 1
2018/02/25 03:53:30 2

What am i doing wrong here, Would appreciate any insight to understand it better, Also would appreciate any thought or advice.

Thanks in Advance!

  • 写回答

1条回答 默认 最新

  • dongxian8272 2018-02-25 12:47
    关注

    It's probably because your port is changing in loop before it starts the safest way to done it is to pass argument to goroutine and move all of call to h parameter to goroutine. Something like that should work

    for _, h := range configuration.Servers {
            counter = counter + 1
            log.Println(counter)
            go func(h ServerConfiguration) {
                server := http.NewServeMux()
                server.HandleFunc("/"+h.ProjectName, foo8001)
                http.ListenAndServe(":"+h.Port, server)
            }(h)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。