github.com/thanos-io/thanos@v0.32.5/pkg/reloader/example_test.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package reloader_test
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"io"
    10  	"log"
    11  	"net/http"
    12  	"net/url"
    13  	"time"
    14  
    15  	"github.com/thanos-io/thanos/pkg/reloader"
    16  )
    17  
    18  func ExampleReloader() {
    19  	u, err := url.Parse("http://localhost:9090")
    20  	if err != nil {
    21  		log.Fatal(err)
    22  	}
    23  	rl := reloader.New(nil, nil, &reloader.Options{
    24  		ReloadURL:     reloader.ReloadURLFromBase(u),
    25  		CfgFile:       "/path/to/cfg",
    26  		CfgOutputFile: "/path/to/cfg.out",
    27  		WatchedDirs:   []string{"/path/to/dirs"},
    28  		WatchInterval: 3 * time.Minute,
    29  		RetryInterval: 5 * time.Second,
    30  		DelayInterval: 1 * time.Second,
    31  	})
    32  
    33  	ctx, cancel := context.WithCancel(context.Background())
    34  	go func() {
    35  		if err := rl.Watch(ctx); err != nil {
    36  			log.Fatal(err)
    37  		}
    38  	}()
    39  
    40  	reloadHandler := func(w http.ResponseWriter, req *http.Request) {
    41  		if _, err := io.WriteString(w, "Reloaded\n"); err != nil {
    42  			http.Error(w, err.Error(), http.StatusInternalServerError)
    43  		}
    44  	}
    45  
    46  	http.HandleFunc("/-/reload", reloadHandler)
    47  	log.Fatal(http.ListenAndServe(":9090", nil))
    48  
    49  	cancel()
    50  }
    51  
    52  func ExampleReloadURLFromBase() {
    53  	u, err := url.Parse("http://localhost:9090")
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	fmt.Println(reloader.ReloadURLFromBase(u))
    58  	//    Output: http://localhost:9090/-/reload
    59  }