コード例 #1
0
ファイル: http.go プロジェクト: magicmonkey/ServiceDirector
func getHandleCreateService(w http.ResponseWriter, r *http.Request, sr *ServiceRegistry.ServiceRegistry, vars map[string]string) {
	if svc, created := sr.GetServiceWithName(vars["service"], true); created {
		w.WriteHeader(201)
		fmt.Fprintf(w, "Created resource %v\n", svc.Name)
	} else {
		w.WriteHeader(200)
		fmt.Fprintf(w, `Resource %v already exists`, svc.Name)
	}
}
コード例 #2
0
ファイル: http.go プロジェクト: magicmonkey/ServiceDirector
func getHandleGetVersions(w http.ResponseWriter, r *http.Request, sr *ServiceRegistry.ServiceRegistry, vars map[string]string) {
	svc, _ := sr.GetServiceWithName(vars["service"], false)
	if svc == nil {
		http.Error(w, fmt.Sprintf(`No service found with name %v`, vars["service"]), 400)
		return
	}
	type EncodedService struct {
		Version string
		URI     string
	}
	var thingToEncode []EncodedService
	enc := json.NewEncoder(w)
	for _, value := range svc.Versions {
		thingToEncode = append(thingToEncode, EncodedService{string(value.Version), fmt.Sprintf("/services/%v/%v", svc.Name, value.Version)})
	}
	enc.Encode(thingToEncode)
}
コード例 #3
0
ファイル: listen.go プロジェクト: magicmonkey/ServiceDirector
func (p *Persistor) LoadServiceRegistry(name string) *ServiceRegistry.ServiceRegistry {
	log.Printf("[Persistor] Loading a service registry called [%v]\n", name)
	c := p.getRedis()
	srBytes := c.Get(fmt.Sprintf("serviceregistry-%v", name))
	buf := bytes.NewBuffer([]byte(srBytes.Val()))
	dec := gob.NewDecoder(buf)
	sr := new(ServiceRegistry.ServiceRegistry)
	dec.Decode(sr)
	sr.Name = name

	// Reconnect the up-tree references
	for _, value := range sr.Services {
		value.SetServiceRegistry(sr)
	}

	return sr
}
コード例 #4
0
ファイル: http.go プロジェクト: magicmonkey/ServiceDirector
func getHandleAddServiceInstance(w http.ResponseWriter, r *http.Request, sr *ServiceRegistry.ServiceRegistry, vars map[string]string) {
	svc, _ := sr.GetServiceWithName(vars["service"], false)
	if svc == nil {
		http.Error(w, `Cannot find service, hav you created it via a PUT request?`, 404)
		return
	}
	if r.Header.Get(`Content-Type`) != `application/json` {
		http.Error(w, `Server only understands application/json as the content-type`, 400)
		return
	}
	type SubmittedInstance struct {
		Version  string
		Location string
	}
	req := new(SubmittedInstance)
	json.NewDecoder(r.Body).Decode(req)
	svc.AddServiceInstance(ServiceRegistry.NewVersion(req.Version), ServiceRegistry.NewLocation(req.Location))
	w.WriteHeader(201)
	fmt.Fprintf(w, "Added location %v to service %v version %v\n", req.Location, svc.Name, req.Version)
}
コード例 #5
0
ファイル: http.go プロジェクト: magicmonkey/ServiceDirector
func getHandleGetServiceInstance(w http.ResponseWriter, r *http.Request, sr *ServiceRegistry.ServiceRegistry, vars map[string]string) {
	svc, _ := sr.GetServiceWithName(vars["service"], false)
	if svc == nil {
		http.Error(w, fmt.Sprintf(`No service found with name %v`, vars["service"]), 400)
		return
	}
	svs := svc.GetLocationsForVersionString(vars["version"])
	if svs == nil {
		http.Error(w, fmt.Sprintf(`Found service with name %v but could not find an instance with version %v`, vars["service"], vars["version"]), 400)
		return
	}
	type EncodedServiceInstance struct {
		Name    string
		Version string
		URI     *[]string
	}
	thingToEncode := EncodedServiceInstance{svc.Name, vars["version"], new([]string)}
	enc := json.NewEncoder(w)
	for _, value := range svs {
		*thingToEncode.URI = append(*thingToEncode.URI, value.Location)
	}
	enc.Encode(thingToEncode)
}
コード例 #6
0
ファイル: main.go プロジェクト: magicmonkey/ServiceDirector
// The Master is the thing which allows slaves to connect and get updates
func runMaster(httpAddr string, httpUpdateAddr string) {

	var sr *ServiceRegistry.ServiceRegistry

	// The Persistor is the thing which saves any updates to Redis
	// Also the initial ServiceRegistry is loaded from it
	p := persistor.NewPersistor()
	sru1 := p.Listen()

	sr = p.LoadServiceRegistry("FirstRegistry")

	rep := replication.NewMaster()
	sru2 := rep.StartListener()

	h := http.NewBalancer()
	finished1, requestUpdate, sru3 := h.RunHTTP(httpAddr)

	u := update.NewUpdater()
	finished2 := u.RunHTTP(httpUpdateAddr, sr)

	sr.RegisterUpdateChannel(sru1)
	sr.RegisterUpdateChannel(sru2)
	sr.RegisterUpdateChannel(sru3)

	for {
		select {
		case <-finished1:
			return
		case <-finished2:
			return
		case <-requestUpdate:
			log.Println("[Main] Someone has requested an update")
			sr.SendRegistryUpdate()
		case <-time.After(30 * time.Second):
			sru1 <- *sr
		}
	}

}