dub init helloVibe
"libs-posix": ["dl"], "versions": ["VibeCustomMain"], "dependencies": { "vibe-d": "~master" }
dub run
Congratulations, your first program on D earned. %APPDATA%/dub/packages/vibe-d-master/source
Now autocompletion is our assistant. listenHTTP(HTTPServerSettings,URLRouter)
runEventLoop()
respectively. You also need to create server settings and configure the router. auto settings = new HTTPServerSettings; settings.bindAddresses = ["127.0.0.1"]; settings.port = 80;
import vibe.d;
auto router = new URLRouter; router.get("/", &index);
void index(HTTPServerRequest req, HTTPServerResponse res) { res.writeBody("Hello World!"); }
import std.stdio; import vibe.d; void main() { writeln("Edit source/app.d to start your project."); void index(HTTPServerRequest req, HTTPServerResponse res) { res.writeBody("Hello World!"); } auto settings = new HTTPServerSettings; settings.bindAddresses = ["127.0.0.1"]; settings.port = 80; auto router = new URLRouter; router.get("/", &index); listenHTTP(settings, router); runEventLoop(); }
dub run
. Congratulations, your first server on vibe.d is ready. interface OpenWeather { Weather getWeather(string q); }
struct Weather { @Label("City identification") long id; @Label("Data receiving time, unix time, GMT") ulong dt; @Label("City name") string name; WCoord coord; WSys sys; WMain main; WWind wind; WClouds clouds; //@optional() //WConditions[] weather; @optional() WRain rain; @optional() WSnow snow; } struct WCoord { @Label("City geo location, lat") double lat; @Label("City geo location, lon") double lon; } struct WSys { @Label("System parameter, do not use it") double message; @Label("Country (GB, JP etc.)") string country; @Label("Sunrise time, unix, UTC") ulong sunrise; @Label("Sunset time, unix, UTC") ulong sunset; } struct WMain { @Label("Temperature, Kelvin (subtract 273.15 to convert to Celsius)") double temp; @Label("Humidity, %") double humidity; @Label("Minimum temperature at the moment. This is deviation from current temp that is possible for large cities and megalopolises geographically expanded (use these parameter optionally)") double temp_min; @Label("Maximum temperature at the moment. This is deviation from current temp that is possible for large cities and megalopolises geographically expanded (use these parameter optionally)") double temp_max; @Label("Atmospheric pressure, hPa ") double pressure; @Label("Atmospheric pressure on the sea level, hPa") @optional() double sea_level; @Label("Atmospheric pressure on the ground level, hPa") @optional() double grnd_level; } struct WWind { @Label("Wind speed, mps") double speed; @Label("Wind direction, degrees (meteorological)") double deg; @Label("Wind gust, mps") @optional() double gust; } struct WClouds { @Label("Cloudiness, %") double all; } struct WConditions { @Label("Weather condition id") long id; @Label("Group of weather parameters (Rain, Snow, Extreme etc.)") Weather main; @Label("Weather condition within the group") string description; @Label("Weather icon id") string icon; } struct WRain { @Label("Precipitation volume for last 3 hours, mm") @optional() double _3h; } struct WSnow { @Label("Snow volume for last 3 hours, mm") @optional() double _3h; }
Label
is a structure: struct Label { string text; }
auto client = new RestInterfaceClient!OpenWeather("http://api.openweathermap.org/data/2.5/");
void index(HTTPServerRequest req, HTTPServerResponse res) { auto client = new RestInterfaceClient!OpenWeather("http://api.openweathermap.org/data/2.5/"); auto weather = client.getWeather("Moscow"); string result = "<html>"; foreach(each; respond(weather)) { auto writer = res.bodyWriter(); if (!each.label.empty) { result ~=format("<b>%s</b>:<br/>\"%s\" = %s<br/>", each.label, each.name, each.value); } else { result ~= format("<i>%s</i><br/>", each.name); } } result ~= "</html>"; res.writeBody(result); }
import std.traits; import std.conv; struct A { string name; string value; string label; } string getLabel(alias ST, alias mem)() { foreach (attr; __traits(getAttributes, __traits(getMember,ST,mem))) { if (is(typeof(attr) == Label)) { return attr.text; } } return ""; } A[] respond(ST)(ST st) { A[] ret = new A[0]; foreach(mem; __traits(derivedMembers, ST)) { static if(is(typeof(__traits(getMember,ST,mem)) == struct)) { ret ~= A(mem,"",""); ret ~= respond!(typeof(__traits(getMember,ST,mem)))(__traits(getMember,st, mem)); } else { ret ~= A(mem, to!string(__traits(getMember,st, mem)), getLabel!(ST,mem) ); } } return ret; }
class OpenWeatherImpl:OpenWeather { Weather getWeather(string q) { auto client = new RestInterfaceClient!OpenWeather("http://api.openweathermap.org/data/2.5/"); return client.getWeather(q); } }
router.registerRestInterface(new OpenWeatherImpl);
import std.traits; import std.conv; struct Label { string text; } struct A { string name; string value; string label; } string getLabel(alias ST, alias mem)() { foreach (attr; __traits(getAttributes, __traits(getMember,ST,mem))) { if (is(typeof(attr) == Label)) { return attr.text; } } return ""; } A[] respond(ST)(ST st) { A[] ret = new A[0]; foreach(mem; __traits(derivedMembers, ST)) { static if(is(typeof(__traits(getMember,ST,mem)) == struct)) { ret ~= A(mem,"",""); ret ~= respond!(typeof(__traits(getMember,ST,mem)))(__traits(getMember,st, mem)); } else { ret ~= A(mem, to!string(__traits(getMember,st, mem)), getLabel!(ST,mem) ); } } return ret; } struct Weather { @Label("City identification") long id; @Label("Data receiving time, unix time, GMT") ulong dt; @Label("City name") string name; WCoord coord; WSys sys; WMain main; WWind wind; WClouds clouds; //@optional() //WConditions[] weather; @optional() WRain rain; @optional() WSnow snow; } struct WCoord { @Label("City geo location, lat") double lat; @Label("City geo location, lon") double lon; } struct WSys { @Label("System parameter, do not use it") double message; @Label("Country (GB, JP etc.)") string country; @Label("Sunrise time, unix, UTC") ulong sunrise; @Label("Sunset time, unix, UTC") ulong sunset; } struct WMain { @Label("Temperature, Kelvin (subtract 273.15 to convert to Celsius)") double temp; @Label("Humidity, %") double humidity; @Label("Minimum temperature at the moment. This is deviation from current temp that is possible for large cities and megalopolises geographically expanded (use these parameter optionally)") double temp_min; @Label("Maximum temperature at the moment. This is deviation from current temp that is possible for large cities and megalopolises geographically expanded (use these parameter optionally)") double temp_max; @Label("Atmospheric pressure, hPa ") double pressure; @Label("Atmospheric pressure on the sea level, hPa") @optional() double sea_level; @Label("Atmospheric pressure on the ground level, hPa") @optional() double grnd_level; } struct WWind { @Label("Wind speed, mps") double speed; @Label("Wind direction, degrees (meteorological)") double deg; @Label("Wind gust, mps") @optional() double gust; } struct WClouds { @Label("Cloudiness, %") double all; } struct WConditions { @Label("Weather condition id") long id; @Label("Group of weather parameters (Rain, Snow, Extreme etc.)") Weather main; @Label("Weather condition within the group") string description; @Label("Weather icon id") string icon; } struct WRain { @Label("Precipitation volume for last 3 hours, mm") @optional() double _3h; } struct WSnow { @Label("Snow volume for last 3 hours, mm") @optional() double _3h; } interface OpenWeather { Weather getWeather(string q); } class OpenWeatherImpl:OpenWeather { Weather getWeather(string q) { auto client = new RestInterfaceClient!OpenWeather("http://api.openweathermap.org/data/2.5/"); return client.getWeather(q); } } import std.stdio; import vibe.d; import std.string; void main() { writeln("Edit source/app.d to start your project."); void index(HTTPServerRequest req, HTTPServerResponse res) { auto client = new RestInterfaceClient!OpenWeather("http://api.openweathermap.org/data/2.5/"); auto weather = client.getWeather("Moscow"); string result = "<html>"; foreach(each; respond(weather)) { auto writer = res.bodyWriter(); if (!each.label.empty) { result ~=format("<b>%s</b>:<br/>\"%s\" = %s<br/>", each.label, each.name, each.value); } else { result ~= format("<i>%s</i><br/>", each.name); } } result ~= "</html>"; res.writeBody(result); } auto settings = new HTTPServerSettings; settings.bindAddresses = ["127.0.0.1"]; settings.port = 80; auto router = new URLRouter; router.get("/", &index); router.registerRestInterface(new OpenWeatherImpl); listenHTTP(settings, router); runEventLoop(); }
Source: https://habr.com/ru/post/231581/
All Articles