示例#1
0
文件: run.go 项目: kisom/hockeypuck
func (c *runCmd) Main() {
	c.configuredCmd.Main()
	InitLog()
	// Create an HTTP request router
	r := mux.NewRouter()
	// Add common static routes
	NewStaticRouter(r)
	// Create HKP router
	hkpRouter := hkp.NewRouter(r)
	// Create SKS peer
	sksPeer, err := openpgp.NewSksPeer(hkpRouter.Service)
	if err != nil {
		die(err)
	}
	// Launch the OpenPGP workers
	for i := 0; i < openpgp.Config().NumWorkers(); i++ {
		w, err := openpgp.NewWorker(hkpRouter.Service, sksPeer)
		if err != nil {
			die(err)
		}
		// Subscribe SKS to worker's key changes
		w.SubKeyChanges(sksPeer.KeyChanges)
		go w.Run()
	}
	sksPeer.Start()
	// Bind the router to the built-in webserver root
	http.Handle("/", r)
	// Start the built-in webserver, run forever
	err = http.ListenAndServe(hkp.Config().HttpBind(), nil)
	die(err)
}
示例#2
0
文件: run.go 项目: cmars/hockeypuck
func (c *runCmd) Main() {
	c.configuredCmd.Main()
	InitLog()
	// Create an HTTP request router
	r := mux.NewRouter()
	// Add common static routes
	NewStaticRouter(r)
	// Create HKP router
	hkpRouter := hkp.NewRouter(r)
	// Create SKS peer
	sksPeer, err := openpgp.NewSksPeer(hkpRouter.Service)
	if err != nil {
		die(err)
	}
	// Launch the OpenPGP workers
	for i := 0; i < openpgp.Config().NumWorkers(); i++ {
		w, err := openpgp.NewWorker(hkpRouter.Service, sksPeer)
		if err != nil {
			die(err)
		}
		// Subscribe SKS to worker's key changes
		w.SubKeyChanges(sksPeer.KeyChanges)
		go w.Run()
	}
	sksPeer.Start()
	// Bind the router to the built-in webserver root
	http.Handle("/", r)

	var hkpsConfigured bool
	var tlsCertPath, tlsKeyPath string
	if hkp.Config().HttpsBind() != "" {
		if hkp.Config().TLSCertificate() == "" {
			err = fmt.Errorf("no TLS certificate provided")
		} else if hkp.Config().TLSKey() == "" {
			err = fmt.Errorf("no TLS private key provided")
		}

		if err != nil {
			die(err)
		}

		if filepath.IsAbs(hkp.Config().TLSCertificate()) {
			tlsCertPath = hkp.Config().TLSCertificate()
		} else {
			tlsCertPath = filepath.Join(c.configDir, hkp.Config().TLSCertificate())
		}

		if filepath.IsAbs(hkp.Config().TLSKey()) {
			tlsKeyPath = hkp.Config().TLSKey()
		} else {
			tlsKeyPath = filepath.Join(c.configDir, hkp.Config().TLSKey())
		}
		hkpsConfigured = true
	}

	if hkpsConfigured {
		if hkp.Config().HttpBind() != "" {
			go func() {
				// Start the built-in webserver, run forever
				err = http.ListenAndServe(hkp.Config().HttpBind(), nil)
				die(err)
			}()
		}
		err = http.ListenAndServeTLS(hkp.Config().HttpsBind(),
			tlsCertPath, tlsKeyPath, nil)
		die(err)
	} else {
		// Start the built-in webserver, run forever
		err = http.ListenAndServe(hkp.Config().HttpBind(), nil)
		die(err)
	}
}