Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Multi Path Static File Mapper #2228

Closed
lixiaoqiang opened this issue Aug 28, 2023 · 3 comments
Closed

No Multi Path Static File Mapper #2228

lixiaoqiang opened this issue Aug 28, 2023 · 3 comments

Comments

@lixiaoqiang
Copy link

lixiaoqiang commented Aug 28, 2023

          Doc so we can close it

Originally posted by @samsp-msft in #187 (comment)

I want to use yarp in some senaros like :

path MapTo
/{**catch-all} a static file folder with some static files, if provider "/" only map to index.html file
/apps/{**catch-all} a static file folder with some static files, if provider "/apps" only map to index.html file.
eg.
/app map to index.html under the folder
/app/index.html to index.html too.
/api/{**catch-all} a backend service may be http://192.168.0.1:8080/
/api-mobile/{**catch-all} a backend service may be http://192.168.0.2:8080/

The backend proxy is easy to config. But I've no idea for two static folder map ("/" and "/apps").

@karelz
Copy link
Member

karelz commented Aug 29, 2023

Triage: This feature / ask does not make sense without #187. Closing as dupe.

@karelz
Copy link
Member

karelz commented Aug 29, 2023

Duplicate of #187

@karelz karelz marked this as a duplicate of #187 Aug 29, 2023
@karelz karelz closed this as completed Aug 29, 2023
@lixiaoqiang
Copy link
Author

For first aid to my project. I provider a rough temp solution to this problem. May be usefull for other users.

  • Provider a composite file provider. for each detail file provider in this composite provider a url prefix to match the Folder.
    And use this file provider fo UseStaticFile method.

  • Provider a DefaultFileMiddleware. I also provider a url and default file mapping. When a GET request hit no endpoint use the mapping to match , if match file return file to response.

    public Task Invoke(HttpContext context)
    {
        if (context.GetEndpoint()?.RequestDelegate is null &&
            IsGetOrHeadMethod(context.Request.Method) &&
            MatchFile(context, out var matchFile))
        {
            context.Request.Path = matchFile;
        }
        return _next(context);
    }

    private bool MatchFile(HttpContext context, out PathString matchFile)
    {
        var path = context.Request.Path;
        if (!(path.HasValue && path.Value!.EndsWith("/")))
        {
            path += new PathString("/");
        }
        
        var config = _configs.FirstOrDefault(it => it.RequestPath == path);
        if (config == null)
        {
            matchFile = PathString.Empty;
            return false;
        }
        var relativePath = path.ToString()[config.RequestPath.ToString().Length..];
        var dictContents = config.FileProvider.GetDirectoryContents(relativePath);
        if (dictContents.Exists)
        {
            for (int matchIndex = 0; matchIndex < config.Files.Count; matchIndex++)
            {
                var defaultFile = config.Files[matchIndex];
                var file = config.FileProvider.GetFileInfo(relativePath + defaultFile);
                if (file.Exists)
                {
                    matchFile = path + defaultFile;
                    return true;
                }
            }
        }
        matchFile = PathString.Empty;
        return false;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants