github.com/outbrain/consul@v1.4.5/connect/example_test.go (about) 1 package connect 2 3 import ( 4 "crypto/tls" 5 "log" 6 "net" 7 "net/http" 8 9 "github.com/hashicorp/consul/api" 10 ) 11 12 type apiHandler struct{} 13 14 func (apiHandler) ServeHTTP(http.ResponseWriter, *http.Request) {} 15 16 // Note: this assumes a suitable Consul ACL token with 'service:write' for 17 // service 'web' is set in CONSUL_HTTP_TOKEN ENV var. 18 func ExampleService_ServerTLSConfig_hTTP() { 19 client, _ := api.NewClient(api.DefaultConfig()) 20 svc, _ := NewService("web", client) 21 server := &http.Server{ 22 Addr: ":8080", 23 Handler: apiHandler{}, 24 TLSConfig: svc.ServerTLSConfig(), 25 } 26 // Cert and key files are blank since the tls.Config will handle providing 27 // those dynamically. 28 log.Fatal(server.ListenAndServeTLS("", "")) 29 } 30 31 func acceptLoop(l net.Listener) {} 32 33 // Note: this assumes a suitable Consul ACL token with 'service:write' for 34 // service 'web' is set in CONSUL_HTTP_TOKEN ENV var. 35 func ExampleService_ServerTLSConfig_tLS() { 36 client, _ := api.NewClient(api.DefaultConfig()) 37 svc, _ := NewService("web", client) 38 l, _ := tls.Listen("tcp", ":8080", svc.ServerTLSConfig()) 39 acceptLoop(l) 40 } 41 42 func handleResponse(r *http.Response) {} 43 44 // Note: this assumes a suitable Consul ACL token with 'service:write' for 45 // service 'web' is set in CONSUL_HTTP_TOKEN ENV var. 46 func ExampleService_HTTPClient() { 47 client, _ := api.NewClient(api.DefaultConfig()) 48 svc, _ := NewService("web", client) 49 50 httpClient := svc.HTTPClient() 51 resp, _ := httpClient.Get("https://web.service.consul/foo/bar") 52 handleResponse(resp) 53 }