Example #1
0
func main() {
	var opts Options

	_, err := flags.Parse(&opts)
	if err != nil {
		os.Exit(1)
	}

	if opts.Debug {
		log.SetLevel(log.DebugLevel)
	}

	if opts.LogFile != "" {
		logFp, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
		checkError(fmt.Sprintf("error opening %s", opts.LogFile), err)

		defer logFp.Close()

		// ensure panic output goes to log file
		syscall.Dup2(int(logFp.Fd()), 1)
		syscall.Dup2(int(logFp.Fd()), 2)

		// log as JSON
		log.SetFormatter(&log.JSONFormatter{})

		// send output to file
		log.SetOutput(logFp)
	}

	log.Debug("hi there! (tickertape tickertape)")
	log.Infof("version: %s", version)

	vaultClient, err := helpers.NewVaultClient(opts.VaultAddr, opts.VaultToken)
	checkError("creating Vault client", err)

	consulClient, err := consulapi.NewClient(consulapi.DefaultConfig())
	checkError("creating Consul client", err)

	router := mux.NewRouter()

	registrar := instance.NewRegistrar(vaultClient)
	v1 := v1.NewCentralBooking(
		registrar,
		consulClient.Catalog(),
		vaultClient.GetEndpoint(),
	)
	v1.InstallHandlers(router.PathPrefix("/v1").Subrouter())

	httpServer := &http.Server{
		Addr:    fmt.Sprintf(":%d", opts.HttpPort),
		Handler: Log(router),
	}

	checkError("launching HTTP server", httpServer.ListenAndServe())
}
	var mockConsulCatalog interfaces.MockConsulCatalog
	var mockVaultClientTemp interfaces.MockVaultClient

	BeforeEach(func() {
		router = mux.NewRouter()

		resp = httptest.NewRecorder()

		mockVaultClient = interfaces.MockVaultClient{}
		mockConsulCatalog = interfaces.MockConsulCatalog{}

		mockVaultClientTemp = interfaces.MockVaultClient{}

		cb = v1.NewCentralBooking(
			instance.NewRegistrar(&mockVaultClient),
			&mockConsulCatalog,
			"https://vault.example.com/",
		)
		cb.InstallHandlers(router.PathPrefix("/v1").Subrouter())
	})

	Describe("instance registration", func() {
		endpoint := "http://example.com/v1/register/instance"

		It("should fail with invalid GET verb", func() {
			req, err := http.NewRequest("GET", endpoint, nil)
			Expect(err).To(BeNil())

			router.ServeHTTP(resp, req)
			Expect(resp.Code).To(Equal(404))
		})