Skip to content

Commit

Permalink
vibe.web.web: Add more convenience methods (fullPath, session, json)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilzbach committed Oct 10, 2017
1 parent 714e6bc commit 6e1269c
Showing 1 changed file with 112 additions and 7 deletions.
119 changes: 112 additions & 7 deletions web/vibe/web/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import vibe.http.common;
import vibe.http.router;
import vibe.http.server;
import vibe.http.websockets;
import vibe.data.json : Json;
import vibe.web.auth : AuthInfo, handleAuthentication, handleAuthorization, isAuthenticated;

import std.encoding : sanitize;
Expand Down Expand Up @@ -380,8 +381,6 @@ void redirect(string url)

///
@safe unittest {
import vibe.data.json : Json;

class WebService {
// POST /item
void postItem() {
Expand Down Expand Up @@ -417,8 +416,6 @@ void header(string name, string value)

///
@safe unittest {
import vibe.data.json : Json;

class WebService {
// POST /item
Json postItem() {
Expand Down Expand Up @@ -459,8 +456,6 @@ body

///
@safe unittest {
import vibe.data.json : Json;

class WebService {
// POST /item
Json postItem() {
Expand All @@ -480,6 +475,115 @@ 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 full URL that corresponds to the current request.
Note that this may only be called from a function/method
registered using registerWebInterface.
*/
URL fullURL() @safe
{
return request.fullURL;
}

///
@safe unittest {
class WebService {
// GET /path
Json getPath() {
return Json(fullURL.pathString);
}
}

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

auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, router);
}
}

/**
Parses and returns the Json body that corresponds to the current request.
Note that this may only be called from a function/method
registered using registerWebInterface.
*/
Json json() @safe
{
return request.json;
}

///
@safe unittest {
class WebService {
// POST /login
Json postLogin() {
return Json(["user": json["username"]]);
}
}

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

auto settings = new HTTPServerSettings;
settings.port = 8080;
listenHTTP(settings, router);
}
}

/**
Returns the agreed upon language.
Expand Down Expand Up @@ -574,6 +678,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 Expand Up @@ -869,7 +975,6 @@ private void handleRequest(string M, alias overload, C, ERROR...)(HTTPServerRequ
import std.traits;
import std.typetuple : Filter, staticIndexOf;
import vibe.core.stream;
import vibe.data.json;
import vibe.internal.meta.funcattr;
import vibe.internal.meta.uda : findFirstUDA;

Expand Down

0 comments on commit 6e1269c

Please sign in to comment.