Пример #1
0
//Add api, returning existing Api if already defined
func (c *Core) addApi(api *api.Api) *api.Api {
	existingApi := c.GetApi(api.GetJson())
	if existingApi == nil {
		c.ApiMap[*api.GetJson()] = api
		existingApi = api
	}
	return existingApi
}
Пример #2
0
func (c *Core) RegisterApiServiceProvider(api *api.Api, serviceIn *ServiceProvider) error {
	//Register the API if it doesn't already exist
	actualApi := c.addApi(api)
	if actualApi != api {
		return errors.New("Assert failure: Service Provider has different Api instance!")
	}

	//Service can only provide for one Api once
	serviceList, gotList := c.ApiServiceMap[api]
	if !gotList {
		serviceList = make([]*ServiceProvider, 1)
		serviceList[0] = serviceIn
		c.ApiServiceMap[api] = serviceList
	} else {
		c.ApiServiceMap[api] = append(serviceList, serviceIn)
	}
	for _, curService := range serviceList {
		if curService == serviceIn {
			//TODO: Just silently fail instead?
			return errors.New("Assert failure: " + serviceIn.GetName() + " already registered for api: " + *api.GetJson())
		}
	}
	serviceIn.GetHandler().OnAddApi(api)

	return nil
}