Texture UV Mapping

I have a problem regarding UV mapping. I created a geometry starting from a spline and to this geometry I apply a texture that is repeated along its entire length.
This is the wireframe geometry:

And this is the texture I want to apply:

texture

To be able to create the UVs at this moment I calculate the length from the beginning of the line to the current segment (then obviously it will be normalized). The result is this:

The calculated UVs (as can be seen from the result) are incorrect because if you look at the outside part of the curve, the texture is stretched and the inside is compressed. The mapping is correct only at the center of the line.

How would it be possible to set the UVs correctly when this line makes even very sharp curves? Is it necessary to set different UVs inside and outside the line?