I am trying to create a simple rest server that responds to scripts that I attach to pages I visit(using Greasemonkey).
I cannot get the vibe.d project to respond to requests.
I have used the example project
import std.typecons : Nullable;
import vibe.d;
import std.stdio;
@path("/")
interface IMyAPI
{
// GET /api/greeting
@property string greeting();
// PUT /api/greeting
@property void greeting(string text);
// POST /api/users
@path("/users")
void addNewUser(string name);
// GET /api/users
@property string[] users();
// GET /api/:id/name
string getName(int id);
// GET /some_custom_json
Json getSomeCustomJson();
}
// vibe.d takes care of all JSON encoding/decoding
// and actual API implementation can work directly
// with native types
class API : IMyAPI
{
private {
string m_greeting;
string[] m_users;
}
@property string greeting() { return m_greeting; }
@property void greeting(string text) { m_greeting = text; }
void addNewUser(string name) { m_users ~= name; }
@property string[] users() { return m_users; }
string getName(int id) { return m_users[id]; }
Json getSomeCustomJson()
{
Json ret = Json.emptyObject;
ret["somefield"] = "Hello, World!";
return ret;
}
}
// actual usage, this is usually done in app.d module
// constructor
void static_this()
{
import vibe.http.server, vibe.http.router;
auto settings = new HTTPServerSettings;
auto router = new URLRouter;
router.registerRestInterface(new API());
settings.options |= HTTPServerOption.parseJsonBody;
settings.bindAddresses = ["::1", "127.0.0.1"];
settings.port = 80;
listenHTTP(settings, router);
}
I run this then try to make a request either using js or soapUI but they always fail with connection error.
The javascript request is
$.ajax({
url: 'http://127.0.0.1/',
type: 'GET',
//contentType: "application/json; charset=utf-8",
contentType: "application/json"
dataType: "json",
data: 'greeting', // or $('#myform').serializeArray()
success: function() { alert('GET completed'); },
error: function (qXHR, exception) { alert(JSON.stringify(qXHR)); },
done: function(msg) { alert("DONE: " + msg ); }
});
I am new to using rest so I do not know if I am doing something wrong, if it is a configuration issue, or what.
I have tried various things. I ultimately simply want to send the url to the vibe.d rest app.
Any ideas? And any ideas to get some type of feedback so I know if at least the request is making it in to the vibe d framework?
Thanks.