Commit 189b30fd authored by Bat's avatar Bat
Browse files

Merge branch 'master' into 'master'

Fix #48 + bugfix in {% tag %}

See merge request !19
parents 59fa7f23 9fe2d6d3
Pipeline #9457 passed with stage
in 7 minutes and 51 seconds
......@@ -6,8 +6,8 @@ using Valse;
* It contains manual tests.
*/
void main (string[] args) {
Valse.log_level = DebugLevel.ERRORS;
Router r = new Router (9000);
Router.options.process_count = 150;
r.register ("user", new UserController ());
r.register ("home", new HomeController ());
......
......@@ -101,7 +101,7 @@ namespace Valse {
Object? v = others.arg<Object> ();
if (v == null) {
print ("[WARNING] Using the model method with a key (%s), but no associated value.\n", k);
log_warning ("[WARNING] Using the model method with a key (%s), but no associated value.\n".printf (k));
break;
}
......@@ -141,7 +141,7 @@ namespace Valse {
result += line + "\n";
}
} catch (Error e) {
print ("[ERROR] %s\n", e.message);
log_error (e.message);
}
return result;
......
......@@ -22,11 +22,7 @@ namespace Valse {
*/
private int handle_sql_errors (int ec, string message = "An error occured") {
if (ec != Sqlite.OK) {
if (message != null) {
stderr.printf (message + ": %d: %s\n", db.errcode (), db.errmsg ());
} else {
stderr.printf (message + ": %d:\n", db.errcode ());
}
log_error ("%s (code %d)".printf (message, db.errcode ()));
return EXIT_ON_ERROR;
}
return EXIT_SUCCESS;
......
......@@ -113,7 +113,7 @@ namespace Valse {
is_multipart = true;
}
} catch (Error err) {
print ("[ERROR] %s\n", err.message);
log_error (err.message);
}
}
return true;
......@@ -160,7 +160,7 @@ namespace Valse {
}
}
} catch (Error err) {
print ("[ERROR] %s\n", err.message);
log_error (err.message);
}
}
}
......
......@@ -100,7 +100,7 @@ namespace Valse {
file_info = enumerator.next_file (null);
}
} catch (Error err) {
print ("[STATIC] The \"static/\" folder doesn't exist or it's empty\n");
log_warning ("The \"static/\" folder doesn't exist or it's empty");
}
return res;
}
......@@ -178,7 +178,7 @@ namespace Valse {
req.output.write (res.raw_data);
}
} catch (Error err) {
print ("[ERROR] %s\n", err.message);
log_error (err.message);
}
}
......@@ -187,10 +187,8 @@ namespace Valse {
res.error_code = 500;
string debug_message = "";
if (DEBUG) {
DateTime now = new DateTime.now_local ();
debug_message = "[%s] Request on \"%s\" -> ".printf (now.format (DEBUG_DATE_FORMAT), req.path);
}
DateTime now = new DateTime.now_local ();
debug_message = "[%s] Request on \"%s\" -> ".printf (now.format (DEBUG_DATE_FORMAT), req.path);
string[] clean_path = split_path (req.path);
bool is_static = false;
......@@ -214,7 +212,7 @@ namespace Valse {
res.headers["Expires"] = new DateTime.now_utc ().add_years (1).format (http_date_format);
res.headers["Last-Modified"] = new DateTime.now_utc ().add_seconds (-10.0).format (http_date_format);
} catch (Error err) {
print ("[ERROR] \"static%s\" not found.\n", req.path);
log_error ("\"static%s\" not found.".printf (req.path));
res.error_code = 404;
}
}
......@@ -266,19 +264,11 @@ namespace Valse {
req.options = clean_path;
res = ctrl.run_action (act, req);
if (DEBUG) {
debug_message += "%s %s %d\n".printf (req.method, res.mime_type, res.error_code);
}
debug_message += "%s %s %d\n".printf (req.method, res.mime_type, res.error_code);
}
}
if (DEBUG && is_static) {
debug_message += "%s %s %u\n".printf (req.method, res.mime_type, res.error_code);
}
if (DEBUG) {
print (debug_message);
}
debug_message += "%s %s %u\n".printf (req.method, res.mime_type, res.error_code);
log_debug (debug_message);
return res;
}
......@@ -296,7 +286,7 @@ namespace Valse {
}
this.routes[route] = controller;
} else {
print ("[ERROR] \"/%s\" has already a controller.\n", route);
log_error ("\"/%s\" has already a controller.".printf (route));
}
}
......@@ -304,18 +294,16 @@ namespace Valse {
* Run the server.
*/
public void listen () {
if (DEBUG) {
DateTime now = new DateTime.now_local ();
print ("%s\n", now.format (DEBUG_DATE_FORMAT));
print ("%s version %s\n", NAME, VERSION);
print ("Development server available at http://127.0.0.1\n");
print ("SimpleCGI listening on port %u\n", this.port);
print ("%d static files found\n", this.static_files.length);
foreach (string file in this.static_files) {
print (" -%s\n", file);
}
print ("Quit the server with CTRL-C\n");
DateTime now = new DateTime.now_local ();
log (now.format (DEBUG_DATE_FORMAT));
log ("%s version %s".printf (NAME, VERSION));
log ("Development server available at http://127.0.0.1");
log ("SimpleCGI listening on port %u".printf (this.port));
log ("%d static files found".printf (this.static_files.length));
foreach (string file in this.static_files) {
log_debug (" - %s".printf (file));
}
log ("Quit the server with CTRL-C");
MainLoop ml = new MainLoop ();
new SCGI.Server (this.port, options.process_count, scgi_handler);
ml.run ();
......
......@@ -2,7 +2,7 @@ namespace Valse {
const string NAME = "Valse";
const string VERSION = "-dev";
const bool DEBUG = true;
public DebugLevel log_level = DebugLevel.DEBUG;
const string DEBUG_DATE_FORMAT = "%d/%b/%Y %T";
const int EXIT_SUCCESS = 1;
const int EXIT_ON_ERROR = -1;
......@@ -25,9 +25,27 @@ namespace Valse {
return result;
}
private void log_debug (string message) {
if (DEBUG) {
print (@"[DEBUG] $(message)\n\n");
internal void log_debug (string message) {
if (log_level == DebugLevel.DEBUG) {
print (@"[DEBUG] $(message)\n");
}
}
internal void log_warning (string warn) {
if (log_level >= DebugLevel.WARNINGS) {
print (@"[WARNING] $(warn)\n");
}
}
internal void log_error (string error) {
if (log_level >= DebugLevel.ERRORS) {
print (@"[ERROR] $(error)\n");
}
}
internal void log (string message) {
if (log_level >= DebugLevel.BASIC) {
print (@"$(message)\n");
}
}
......@@ -38,4 +56,12 @@ namespace Valse {
NOT_FOUND,
SERVER_ERROR
}
public enum DebugLevel {
NONE,
BASIC,
ERRORS,
WARNINGS,
DEBUG
}
}
......@@ -174,8 +174,6 @@ namespace Valse {
model_dump += "</p>";
}
print (model_dump.replace ("</p>", "\n").replace("<p>", ""));
Regex for_re = new Regex ("{% *for (?P<item>[\\w_.]+?)(, *(?P<index>[\\w]+))? in (?P<coll>" + model_keyword + "(\\.([[:graph:]]*?))?) *%}(?P<content>(.|\\R)+?){% *endfor *%}");
MatchInfo for_info;
bool for_go = for_re.match (result, RegexMatchFlags.NEWLINE_ANY, out for_info);
......@@ -184,7 +182,6 @@ namespace Valse {
while (for_go) {
string item = for_info.fetch_named ("item");
string coll = for_info.fetch_named ("coll");
print (@"coll : $coll\n");
string content = for_info.fetch_named ("content");
string? index_var = for_info.fetch_named ("index");
......@@ -204,7 +201,6 @@ namespace Valse {
});
if (index_var != null && index_var.length != 0) {
print (@"index var is '$index_var' $(index_var.length)\n");
// interpreting index variables
Regex index_re = new Regex ("{{ %s(?P<filters>.*?) }}".printf (index_var));
generated = index_re.replace_eval (generated, generated.length, 0, 0, (info, res) => {
......@@ -228,8 +224,6 @@ namespace Valse {
for_count++;
}
print (result);
// Model variables
Regex var_re = new Regex ("{{ *" + model_keyword + "(?<prop>[\\w.\\[\\]]*)(\\|(?<filter>[[:graph:]]*?)( (?<params>\".*?\"))?)? *}}");
MatchInfo var_info;
......@@ -238,7 +232,7 @@ namespace Valse {
while (go_var) {
string prop_name = var_info.fetch_named ("prop");
string prop_val = mdl [prop_name];
if (prop_val == null && DEBUG) {
if (prop_val == null && log_level == DebugLevel.DEBUG) {
prop_val = "[WARNING] %s%s is null.".printf (model_keyword, prop_name.has_prefix ("[") ? prop_name : prop_name);
}
string? var_filters = var_info.fetch_named ("filter");
......@@ -265,9 +259,8 @@ namespace Valse {
}
prop_val = filters[filter].cb (ref prop_val, filter_args);
} else {
print ("[ERROR] Filter %s doesn't exists.", filter);
log_error ("Filter %s doesn't exists.".printf (filter));
}
}
}
......@@ -371,12 +364,18 @@ namespace Valse {
bool tag_go = tag_re.match (result, 0, out tag_info);
while (tag_go) {
bool found = false;
foreach (string tag in view.tags) {
if (tag_info.fetch_named ("cond") == tag) {
result = tag_re.replace (result, result.length, 0, tag_info.fetch_named ("content"));
found = true;
}
tag_go = tag_info.next ();
}
if (!found) {
result = tag_re.replace (result, result.length, 0, "");
}
tag_go = tag_info.next ();
}
result = result.replace ("\n", "\r\n");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment