Example #1
0
func (this *YamlFileLoader) Load(file string) *RouteCollection {
	fullPaths := this.locator.LocateFirst(file)
	fullPath := fullPaths[0]
	routeConfigs := yaml.Parse(fullPath)
	collection := NewRouteCollection()
	//TODO: Add resource to route collection

	for routeName, value := range routeConfigs {
		routeConfig := value.(map[interface{}]interface{})
		routeConfig = this.normalizeRouteConfig(routeConfig)

		if res, ok := routeConfig[ROUTE_RESOURCE]; ok {
			prefix, ok := routeConfig[ROUTE_PREFIX]
			if !ok {
				prefix = ""
			}

			currDir, _ := path.Split(fullPath)
			coll := this.importResource(config.ToString(res), file, currDir)
			collection.AddCollectionWithPrefix(coll, config.ToString(prefix))
			// TODO: Support for ROUTE TYPE
		} else {
			this.parseRoute(collection, config.ToString(routeName), routeConfig)
		}
	}
	return collection
}
Example #2
0
func (this *YamlFileLoader) parseRoute(collection *RouteCollection, routeName string, routeConfigs map[interface{}]interface{}) {

	pattern, ok := routeConfigs[ROUTE_PATTERN]

	// must have 'ROUTE_PATTERN' section
	if !ok {
		panic(fmt.Sprintf("You must define a pattern for the %v route.", routeName))
	}

	tempMap, ok := routeConfigs[ROUTE_DEFAULTS]
	if !ok {
		tempMap = make(map[string]interface{})
	}
	defaults := tempMap.(map[string]interface{})

	tempMap, ok = routeConfigs[ROUTE_REQUIREMENTS]
	if !ok {
		tempMap = make(map[string]interface{})
	}
	requirements := tempMap.(map[string]interface{})

	tempMap, ok = routeConfigs[ROUTE_OPTIONS]
	if ok {
		tempMap = make(map[string]interface{})
	}

	options := tempMap.(map[string]interface{})

	route := NewRoute(config.ToString(pattern), defaults, requirements, options)
	collection.Add(routeName, route)
}
Example #3
0
func compileSchemes(route *Route) {
	if scheme, ok := route.GetRequirement(ROUTE_REQUIREMENTS_SCHEME); ok {
		schemes := strings.Split(config.ToString(scheme), "|")
		if len(schemes) > 0 {
			route.SetRequirement(ROUTE_REQUIREMENTS_SCHEME, config.TrimSpacesFromArray(schemes))
		}
	}
}
Example #4
0
func compileMethods(route *Route) {
	if method, ok := route.GetRequirement(ROUTE_REQUIREMENTS_METHOD); ok {
		methods := strings.Split(config.ToString(method), "|")
		if len(methods) > 0 {
			route.SetRequirement(ROUTE_REQUIREMENTS_METHOD, config.TrimSpacesFromArray(methods))
		}
	}
}
Example #5
0
func sanitizeRequirements(key string, val interface{}) string {
	// TODO check for an array requirements

	regex := config.ToString(val)
	if strings.HasPrefix(regex, "^") {
		regex = regex[1:]
	}

	if strings.HasSuffix(regex, "$") {
		regex = regex[0 : len(regex)-1]
	}
	return regex
}