func initServer()

in funcframework/framework.go [122:158]


func initServer() (*http.ServeMux, error) {
	server := http.NewServeMux()

	// If FUNCTION_TARGET is set, only serve this target function at path "/".
	// If not set, serve all functions at the registered paths.
	if target := os.Getenv("FUNCTION_TARGET"); len(target) > 0 {
		var targetFn *registry.RegisteredFunction

		fn, ok := registry.Default().GetRegisteredFunction(target)
		if ok {
			targetFn = fn
		} else if lastFnWithoutName := registry.Default().GetLastFunctionWithoutName(); lastFnWithoutName != nil {
			// If no function was found with the target name, assume the last function that's not registered declaratively
			// should be served at '/'.
			targetFn = lastFnWithoutName
		} else {
			return nil, fmt.Errorf("no matching function found with name: %q", target)
		}

		h, err := wrapFunction(targetFn)
		if err != nil {
			return nil, fmt.Errorf("failed to serve function %q: %v", target, err)
		}
		server.Handle("/", h)
		return server, nil
	}

	fns := registry.Default().GetAllFunctions()
	for _, fn := range fns {
		h, err := wrapFunction(fn)
		if err != nil {
			return nil, fmt.Errorf("failed to serve function at path %q: %v", fn.Path, err)
		}
		server.Handle(fn.Path, h)
	}
	return server, nil
}