Esempio n. 1
0
// registerRoutes registers HTTP route handlers
func (d *daemon) registerRoutes(router *mux.Router) {
	// register web ui handlers
	d.registerWebuiHandler(router)

	// Add REST routes
	s := router.Headers("Content-Type", "application/json").Methods("Post").Subrouter()

	s.HandleFunc("/plugin/allocAddress", makeHTTPHandler(master.AllocAddressHandler))
	s.HandleFunc("/plugin/releaseAddress", makeHTTPHandler(master.ReleaseAddressHandler))
	s.HandleFunc("/plugin/createEndpoint", makeHTTPHandler(master.CreateEndpointHandler))
	s.HandleFunc("/plugin/deleteEndpoint", makeHTTPHandler(master.DeleteEndpointHandler))

	s = router.Methods("Get").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetEndpointRESTEndpoint, "{id}"),
		get(false, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetEndpointsRESTEndpoint),
		get(true, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetNetworkRESTEndpoint, "{id}"),
		get(false, d.networks))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetNetworksRESTEndpoint),
		get(true, d.networks))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetVersionRESTEndpoint), getVersion)

	// See if we need to create the default tenant
	go objApi.CreateDefaultTenant()
}
Esempio n. 2
0
func (d *daemon) ListenAndServe() {
	router := mux.NewRouter()

	// Create a new api controller
	d.apiController = objApi.NewAPIController(router)

	// initialize policy manager
	mastercfg.InitPolicyMgr(d.stateDriver)

	// Register netmaster service
	d.registerService()

	// register web ui handlers
	registerWebuiHandler(router)

	// Add REST routes
	s := router.Headers("Content-Type", "application/json").Methods("Post").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s", master.DesiredConfigRESTEndpoint),
		post(d.desiredConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.AddConfigRESTEndpoint),
		post(d.addConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.DelConfigRESTEndpoint),
		post(d.delConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.HostBindingConfigRESTEndpoint),
		post(d.hostBindingsConfig))

	s.HandleFunc("/plugin/allocAddress", makeHTTPHandler(master.AllocAddressHandler))
	s.HandleFunc("/plugin/releaseAddress", makeHTTPHandler(master.ReleaseAddressHandler))
	s.HandleFunc("/plugin/createEndpoint", makeHTTPHandler(master.CreateEndpointHandler))
	s.HandleFunc("/plugin/deleteEndpoint", makeHTTPHandler(master.DeleteEndpointHandler))

	s = router.Methods("Get").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetEndpointRESTEndpoint, "{id}"),
		get(false, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetEndpointsRESTEndpoint),
		get(true, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetNetworkRESTEndpoint, "{id}"),
		get(false, d.networks))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetNetworksRESTEndpoint),
		get(true, d.networks))

	log.Infof("Netmaster listening on %s", d.opts.listenURL)

	go objApi.CreateDefaultTenant()

	if err := http.ListenAndServe(d.opts.listenURL, router); err != nil {
		log.Fatalf("Error listening for http requests. Error: %s", err)
	}

}
Esempio n. 3
0
func (d *daemon) ListenAndServe() {
	router := mux.NewRouter()

	// Create a new api controller
	d.apiController = objApi.NewAPIController(router)

	// Setup the router to serve the web UI
	goPath := os.Getenv("GOPATH")
	if goPath != "" {
		webPath := goPath + "/src/github.com/contiv/objmodel/contivModel/www/"

		// Make sure we have the web UI files
		_, err := os.Stat(webPath)
		if err != nil {
			webPath = goPath + "/src/github.com/contiv/netplugin/" +
				"Godeps/_workspace/src/github.com/contiv/objmodel/contivModel/www/"
			_, err := os.Stat(webPath)
			if err != nil {
				log.Errorf("Can not find the web UI directory")
			}
		}

		log.Infof("Using webPath: %s", webPath)

		// serve static files
		router.PathPrefix("/web/").Handler(http.StripPrefix("/web/", http.FileServer(http.Dir(webPath))))

		// Special case to serve main index.html
		router.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
			http.ServeFile(rw, req, webPath+"index.html")
		})
	}

	// proxy Handler
	router.PathPrefix("/proxy/").HandlerFunc(proxyHandler)

	// Add REST routes
	s := router.Headers("Content-Type", "application/json").Methods("Post").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s", master.DesiredConfigRESTEndpoint),
		post(d.desiredConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.AddConfigRESTEndpoint),
		post(d.addConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.DelConfigRESTEndpoint),
		post(d.delConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.HostBindingConfigRESTEndpoint),
		post(d.hostBindingsConfig))

	s.HandleFunc("/plugin/allocAddress", makeHTTPHandler(master.AllocAddressHandler))
	s.HandleFunc("/plugin/releaseAddress", makeHTTPHandler(master.ReleaseAddressHandler))
	s.HandleFunc("/plugin/createEndpoint", makeHTTPHandler(master.CreateEndpointHandler))
	s.HandleFunc("/plugin/deleteEndpoint", makeHTTPHandler(master.DeleteEndpointHandler))

	s = router.Methods("Get").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetEndpointRESTEndpoint, "{id}"),
		get(false, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetEndpointsRESTEndpoint),
		get(true, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetNetworkRESTEndpoint, "{id}"),
		get(false, d.networks))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetNetworksRESTEndpoint),
		get(true, d.networks))

	log.Infof("Netmaster listening on %s", d.opts.listenURL)

	go objApi.CreateDefaultTenant()

	if err := http.ListenAndServe(d.opts.listenURL, router); err != nil {
		log.Fatalf("Error listening for http requests. Error: %s", err)
	}

}