func()

in internal/pkg/core/router/service.go [75:118]


func (rs *RouterService) RegisterAPI(ctx context.Context, api artifacts.API) error {
	// Determine base path based on context and version
	basePath := api.Context

	// Remove trailing slash from context if present
	if len(basePath) > 1 && basePath[len(basePath)-1] == '/' {
		basePath = basePath[:len(basePath)-1]
	}

	// Handle versioning based on versionType
	if api.Version != "" && api.VersionType != "" {
		switch api.VersionType {
		case "url":
			// For URL type, add version as a path segment
			basePath = basePath + "/" + api.Version
		case "context":
			// For context type, replace {version} placeholder if it exists
			versionPattern := "{version}"
			basePath = strings.Replace(basePath, versionPattern, api.Version, 1)
		}
	}

	// Create a subrouter for this API
	apiHandler := http.NewServeMux()

	// Register each resource in the API
	for _, resource := range api.Resources {
		// Register a handler for each HTTP method in the resource
		for _, method := range resource.Methods {
			// Construct the full pattern: "METHOD /path/to/resource"
			pattern := method + " " + resource.URITemplate.PathTemplate
			// Create a wrapper handler that checks query parameters before forwarding to the resource handler
			queryParamHandler := rs.createQueryParamMiddleware(resource, rs.createResourceHandler(resource))
			apiHandler.HandleFunc(pattern, queryParamHandler)
			rs.logger.Info("Registered route for API",
				slog.String("api_name", api.Name),
				slog.String("pattern", pattern))
		}
	}

	// Register the API handler with the main router
	rs.router.Handle(basePath+"/", http.StripPrefix(basePath, apiHandler))
	return nil
}