RejectedSoftware Forums

Sign up

Retrieve session id via before in rest interface

Hey,

the method to retrieve the session id in the rest interface does not appear to work as described here.

Is there any way I can troubleshoot this case and find out what's going wrong?

app.d:

import vibe.d;

interface IMyAPI
{
    string test(Session session);
}

class API : IMyAPI
{
    @before!getSession("session")
    string test(Session session) { return "test complete"~session.id;}
}

Session getSession(HTTPServerRequest req, HTTPServerResponse res)
{
    return req.session ? req.session : res.startSession();
}

shared static this()
{
    auto router = new URLRouter;
    router.get("/", &index);
	
    auto restSettings = new RestInterfaceSettings;
    restSettings.baseURL = URL("http://127.0.0.1:8080/api");
    router.registerRestInterface(new API(), restSettings);
    router.get("/test.js", serveRestJSClient!IMyAPI(restSettings));
	
    auto settings = new HTTPServerSettings;
    settings.port = 8080;
    settings.sessionStore = new MemorySessionStore;
    listenHTTP(settings, router);
}

void index(HTTPServerRequest req, HTTPServerResponse res)
{
    res.render!("index.dt", req);
}

index.dt:

p hello
script(src="test.js")
:javascript
    console.log("test script");
    IMyAPI.test({}, function(r) { console.log(r);});

Browser console result:

test script
test complete

Re: Retrieve session id via before in rest interface

Also the dub.json:

{
	"name": "testdir",
	"authors": [
		"t1"
	],
	"dependencies": {
		"vibe-d": "~>0.7.31"
	},
	"description": "A simple vibe.d server application.",
	"copyright": "Copyright © 2017, t1",
	"license": "proprietary",

	"versions": ["VibeDefaultMain"]
}

Re: Retrieve session id via before in rest interface

Trying to redirect before executing rest function.
However, nothing happens. Does it mean that the @before is not even being executed?

interface IMyAPI
{
    void test(string testi);
}

class API : IMyAPI
{
    @before!redi("testi")
    void test(string testi) { string test = "test complete";}
}

string redi(HTTPServerRequest req, HTTPServerResponse res)
{
    res.redirect("http://google.com");
    return "teststring";
}

Re: Retrieve session id via before in rest interface

On Wed, 03 May 2017 06:15:49 GMT, Timoses wrote:

Trying to redirect before executing rest function.
However, nothing happens. Does it mean that the @before is not even being executed?

interface IMyAPI
{
    void test(string testi);
}

class API : IMyAPI
{
    @before!redi("testi")
    void test(string testi) { string test = "test complete";}
}

string redi(HTTPServerRequest req, HTTPServerResponse res)
{
    res.redirect("http://google.com");
    return "teststring";
}

Bug confirmed: #1753

I'll open a PR with a fix shortly.

Re: Retrieve session id via before in rest interface

Awesome!