github.com/letsencrypt/boulder@v0.20251208.0/test/zendesk-test-srv/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/letsencrypt/boulder/cmd"
    11  	"github.com/letsencrypt/boulder/test/zendeskfake"
    12  )
    13  
    14  type config struct {
    15  	// Addr is the address (e.g. IP:port) on which the server will listen.
    16  	Addr string
    17  
    18  	// ExpectedTokenEmail is the email address expected in the Authorization
    19  	// header of each request.
    20  	ExpectedTokenEmail string `validate:"required"`
    21  
    22  	// ExpectedAPIToken is the API token expected in the Authorization header of
    23  	// each request.
    24  	ExpectedAPIToken string `validate:"required"`
    25  
    26  	// TicketCapacity sets the in-memory store capacity. This is optional and
    27  	// defaults to 200.
    28  	TicketCapacity int
    29  }
    30  
    31  func main() {
    32  	addr := flag.String("addr", "", "listen address override (host:port)")
    33  	configFile := flag.String("config", "", "path to JSON config file")
    34  	flag.Parse()
    35  
    36  	if *configFile == "" {
    37  		flag.Usage()
    38  		os.Exit(1)
    39  	}
    40  
    41  	var c config
    42  	err := cmd.ReadConfigFile(*configFile, &c)
    43  	if err != nil {
    44  		cmd.FailOnError(err, "Reading JSON config file")
    45  	}
    46  
    47  	if *addr != "" {
    48  		c.Addr = *addr
    49  	}
    50  	if c.Addr == "" {
    51  		log.Fatalf("listen address must be provided via -addr or config file")
    52  	}
    53  	if c.ExpectedTokenEmail == "" || c.ExpectedAPIToken == "" {
    54  		log.Fatalf("both ExpectedTokenEmail and ExpectedAPIToken are required (see config)")
    55  	}
    56  
    57  	var store *zendeskfake.Store
    58  	if c.TicketCapacity > 0 {
    59  		store = zendeskfake.NewStore(c.TicketCapacity)
    60  	}
    61  	server := zendeskfake.NewServer(c.ExpectedTokenEmail, c.ExpectedAPIToken, store)
    62  
    63  	srv := &http.Server{
    64  		Addr:         c.Addr,
    65  		Handler:      server.Handler(),
    66  		ReadTimeout:  30 * time.Second,
    67  		WriteTimeout: 30 * time.Second,
    68  		IdleTimeout:  60 * time.Second,
    69  	}
    70  	log.Printf("zendesk-test-srv listening at %s", c.Addr)
    71  	go func() {
    72  		err := srv.ListenAndServe()
    73  		if err != nil {
    74  			log.Fatalf("Failed to start zendesk-test-srv: %s", err)
    75  		}
    76  	}()
    77  
    78  	cmd.WaitForSignal()
    79  }