示例#1
0
func TestFile(t *testing.T) {
	data := []byte(`{"foo": "bar"}`)
	path := filepath.Join(os.TempDir(), fmt.Sprintf("file.%d", time.Now().UnixNano()))
	fh, err := os.Create(path)
	if err != nil {
		t.Error(err)
	}
	defer func() {
		fh.Close()
		os.Remove(path)
	}()

	_, err = fh.Write(data)
	if err != nil {
		t.Error(err)
	}

	f := NewSource(config.SourceName(path))
	c, err := f.Read()
	if err != nil {
		t.Error(err)
	}
	t.Logf("%+v", c)
	if string(c.Data) != string(data) {
		t.Error("data from file does not match")
	}
}
示例#2
0
func main() {
	// Create listener
	l, err := net.Listen("tcp", "localhost:10001")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	// Create Config Source
	f := file.NewSource(config.SourceName("routes.json"))
	conf := config.NewConfig(config.WithSource(f))

	// Create Router
	r := router.NewRouter(router.Config(conf))

	// Setup Handler
	wr := r.Handler()
	h := wr(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "not found", 404)
	}))

	// Start Server
	if err := http.Serve(l, h); err != nil {
		log.Fatal(err)
	}
}
示例#3
0
func (r *router) Init(ctx *cli.Context) error {
	// TODO: Make this more configurable and add more sources
	var conf config.Config

	if c := ctx.String("config_source"); len(c) == 0 && r.opts.Config == nil {
		return errors.New("config source must be defined")
	} else if len(c) > 0 {
		var source config.Source

		switch c {
		case "platform":
			source = config.NewSource()
		case "file":
			fileName := DefaultFile

			parts := strings.Split(c, ":")

			if len(parts) > 1 {
				fileName = parts[1]
			}

			source = file.NewSource(config.SourceName(fileName))
		default:
			return errors.New("Unknown config source " + c)
		}

		conf = config.NewConfig(config.WithSource(source))
	} else {
		conf = r.opts.Config
	}

	go r.run(conf)

	return nil
}
示例#4
0
func main() {
	flag.Parse()

	// Write our first entry
	if err := writeFile(0); err != nil {
		fmt.Println(err)
		return
	}
	defer os.Remove(configFile)

	// Create a config instance
	config := config.NewConfig(
		// aggressive config polling
		config.PollInterval(time.Millisecond*500),
		// use file as a config source
		config.WithSource(file.NewSource(config.SourceName(configFile))),
	)

	defer config.Close()

	// lets read the value while editing it a number of times
	for i := 0; i < 10; i++ {
		val := config.Get("key").String("default")
		fmt.Println("Got ", val)
		writeFile(i + 1)
		time.Sleep(time.Second)
	}

	// watch key that exists
	watch(config, "key")

	// watch key that does not exist
	watch(config, "foo")

	fmt.Println("Stopping config runner")
}