github.com/Venafi/vcert/v5@v5.10.2/examples/server/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"time"
    10  
    11  	"github.com/Venafi/vcert/v5"
    12  	"github.com/Venafi/vcert/v5/pkg/endpoint"
    13  	"github.com/Venafi/vcert/v5/pkg/util"
    14  )
    15  
    16  const (
    17  	name    = "example-auto-certificate-server"
    18  	version = "v0.0.1"
    19  )
    20  
    21  func main() {
    22  	conf := initConfig()
    23  	mux := http.NewServeMux()
    24  	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    25  		_, err := fmt.Fprintf(w, "It works! %v\n", r.Host)
    26  		if err != nil {
    27  			return
    28  		}
    29  	})
    30  	server := &http.Server{
    31  		Addr:         ":8080",
    32  		Handler:      mux,
    33  		ReadTimeout:  5 * time.Second,
    34  		WriteTimeout: 10 * time.Second,
    35  		IdleTimeout:  120 * time.Second,
    36  	}
    37  	listener := conf.NewListener("test.example.com:8443", "example.com")
    38  	log.Fatal(server.Serve(listener))
    39  
    40  }
    41  
    42  func initConfig() *vcert.Config {
    43  	userAgent := fmt.Sprintf("%s/%s %s", name, version, util.DefaultUserAgent)
    44  	conf := &vcert.Config{
    45  		ConnectorType: endpoint.ConnectorTypeTPP,
    46  		BaseUrl:       os.Getenv("TPP_URL"),
    47  		Credentials: &endpoint.Authentication{
    48  			User:     os.Getenv("TPP_USER"),
    49  			Password: os.Getenv("TPP_PASSWORD")},
    50  		Zone:      os.Getenv("TPP_ZONE"),
    51  		UserAgent: &userAgent,
    52  	}
    53  	trustBundleFilePath := os.Getenv("TRUST_BUNDLE_PATH")
    54  	if trustBundleFilePath != "" {
    55  		buf, err := ioutil.ReadFile(trustBundleFilePath)
    56  		if err != nil {
    57  			panic(err)
    58  		}
    59  		conf.ConnectionTrust = string(buf)
    60  	}
    61  	return conf
    62  }