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

Serving one static file with router #227

Closed
michal-minich opened this issue May 18, 2013 · 0 comments
Closed

Serving one static file with router #227

michal-minich opened this issue May 18, 2013 · 0 comments

Comments

@michal-minich
Copy link

Currently there is staticTemplate for serving one .dt file, but functionality for routing to specific other static file is missing. Only multiple files can be routed with serveStaticFiles but they must correspond to physical layout. I implemented simple function (in text below) for serving static file with router. It is extremely basic, and repurposing serveStaticFiles for this functionality would be better. (but this works for very simple use case)

static this()
{
    auto router = new UrlRouter;
    router
        .get("/", staticFile("views\\index.html", "text/html; charset=UTF-8"))
        .get("*", serveStaticFiles("./public/"));

    auto settings = new HttpServerSettings;
    settings.port = 777;

    listenHttp(settings, router);
}

HTTPServerRequestDelegate staticFile(string path, string contentType)
{
    return (HTTPServerRequest req, HTTPServerResponse res){
        auto fil = openFile(path);
        scope(exit) fil.close();
                res.contentType = contentType;
        res.bodyWriter.write(fil);
    };
}
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

1 participant