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

[RFC] vibe.web.web: Add more convenience methods (fullPath, session, json) #1945

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions web/vibe/web/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,52 @@ body
}
}

/**
Returns the current session object.
If no session exists, a new session is started.

Note that this may only be called from a function/method
registered using registerWebInterface.

See_Also: $(LREF terminateSession)
*/
auto session() @safe
{
auto ctx = getRequestContext();
assert(request !is null, "session() used outside of a web interface request!");
auto _session = ctx.req.session;
if (!_session)
_session = ctx.res.startSession;
return _session;
}

///
@safe unittest {
class WebService {
// GET /me
Json getMe() {
enforceHTTP(session.isKeySet("username"), HTTPStatus.forbidden, "Not authorized to perform this action!");

return Json(["username": session.get!string("username").Json]);
}

// POST
void postLogin(string username) {
session.set("username", username);
}
}

void run()
{
auto router = new URLRouter;
router.registerWebInterface(new WebService);

auto settings = new HTTPServerSettings;
settings.sessionStore = new MemorySessionStore();
settings.port = 8080;
listenHTTP(settings, router);
}
}
/**
Returns the agreed upon language.

Expand Down Expand Up @@ -574,6 +620,8 @@ body

Note that this may only be called from a function/method
registered using registerWebInterface.

See_Also: $(LREF session)
*/
void terminateSession()
@safe {
Expand Down