Skip to content

Commit

Permalink
Stand-alone support
Browse files Browse the repository at this point in the history
Added stand-alone support for Diamond, to be used without vibe.d
  • Loading branch information
bausshf committed May 21, 2016
1 parent 92d4e7f commit 259e277
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 128 deletions.
23 changes: 16 additions & 7 deletions src/configurations/serversettings.d
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
module diamond.configurations.serversettings;

struct ServerSettings {
/// The addresses to bind
string[] bindAddresses;
/// The port to bind
ushort port;
/// Collection of default headers
string[string] defaultHeaders;
version (WebServer) {
version = WebServer_Or_WebService;
}
else version (WebService) {
version = WebServer_Or_WebService;
}

version (WebServer_Or_WebService) {
struct ServerSettings {
/// The addresses to bind
string[] bindAddresses;
/// The port to bind
ushort port;
/// Collection of default headers
string[string] defaultHeaders;
}
}
27 changes: 20 additions & 7 deletions src/configurations/sitesettings.d
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
module diamond.configurations.sitesettings;

version (WebServer) {
version (WebService) {
// N/A
}
else {
version = Not_WebService;
}

version (Not_WebService) {
struct SiteSettings {
/// The name of the web site / web server.
string name;
version (WebServer) {
/// The name of the web site / web server.
string name;
}

/// Collection of all views
string[string] views;
/// The home route
string homeRoute;
/// The static file route
string staticFileRoute;

version (WebServer) {
/// The home route
string homeRoute;
/// The static file route
string staticFileRoute;
}
}
}
33 changes: 33 additions & 0 deletions src/diamondapp.d
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,36 @@ version (WebServer_Or_WebService) {
}
}
}
else {
import std.json;
import std.string : format;

import diamond.configurations;
import diamond.views;

private enum viewBuildJson = import("view-build.json");

private string jsonObjectToAA() {
auto viewsJson = parseJSON(viewBuildJson)["views"].object;

string str = "enum string[string] views = [";

foreach (key,value; viewsJson) {
str ~= format("\"%s\" : \"%s\",", key, value.str);
}

str.length -= 1;
str ~= "];";

return str;
}

mixin(jsonObjectToAA);

// This applies all views and parses them.
mixin applyViews!(views);

shared static this() {
// ...
}
}
9 changes: 8 additions & 1 deletion src/exceptions/viewexception.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module diamond.exceptions.viewexception;

version (WebServer) {
version (WebService) {
// N/A
}
else {
version = Not_WebService;
}

version (Not_WebService) {
/// Exception thrown upon encountering an error thrown by a view.
class ViewException : Exception
{
Expand Down
36 changes: 36 additions & 0 deletions src/views/getview.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,39 @@ version (WebServer) {
mixin(generateGetView);
}
}
else version (WebService) {
// N/A
}
else {
/// Generates the getView function.
mixin template GetView() {
auto generateGetView() {
string getViewMixin = "
View getView(string pageName) {
if (!pageName || !pageName.length) {
return null;
}
switch (pageName) {
";

foreach (pageName,pageContent; viewInitCollection) {
getViewMixin ~= format(q{
case "%s": {
return new view_%s("%s");
}
}, pageName, pageName, pageName);
}

getViewMixin ~= "
default: return null;
}
}
";

return getViewMixin;
}

mixin(generateGetView);
}
}
22 changes: 18 additions & 4 deletions src/views/package.d
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
module diamond.views;

version (WebServer) {
version (WebService) {
// N/A
}
else {
version = Not_WebService;
}

version (Not_WebService) {
/// Mixin template for applying views.
mixin template applyViews(string[string] views) {
import std.string : format;

import vibe.d;
version (WebServer) {
import vibe.d;
}

import diamond.templates;
import diamond.views.view;
import diamond.controllers : Status;

import diamond.exceptions : ViewException;

import controllers;
version (WebServer) {
import diamond.controllers : Status;

import controllers;
}

import models;

import diamond.views.viewimports;
Expand Down
Loading

0 comments on commit 259e277

Please sign in to comment.